1: <?php
2: /**
3: * EPL Admin Functions
4: *
5: * @package EPL
6: * @subpackage Admin/Functions
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: if ( ! function_exists( 'cal_days_in_month' ) ) {
18: /**
19: * Fallback in case the calendar extension is not loaded in PHP
20: *
21: * @param string $calendar Calendar type.
22: * @param int $month The month.
23: * @param int $year The year.
24: *
25: * @return string
26: * @since 3.3.3
27: */
28: function cal_days_in_month( $calendar, $month, $year ) {
29: return gmdate( 't', mktime( 0, 0, 0, $month, 1, $year ) );
30: }
31: }
32:
33: /**
34: * Flush Rewrite Rules
35: *
36: * @since 1.0
37: */
38: function epl_flush_rewrite_rules() {
39: $epl_rewrite_rules = get_option( 'epl_rewrite_rules' );
40: if ( ! $epl_rewrite_rules ) {
41: flush_rewrite_rules();
42: update_option( 'epl_rewrite_rules', true );
43: }
44: }
45: add_action( 'admin_init', 'epl_flush_rewrite_rules' );
46: add_action( 'init', 'epl_flush_rewrite_rules' );
47:
48: /**
49: * Notice on Dashboard Widget
50: *
51: * @since 3.1.6
52: */
53: function epl_dashboard_widget_offer_rough() {
54:
55: if ( has_filter( 'epl_extensions_options_filter_new' ) || get_transient( 'epl_admin_notices_display' ) === '1' ) {
56: return;
57: }
58:
59: echo '<div class="epl-internal-ad">';
60: echo '<div>' . esc_html__( 'Remove these ads?', 'easy-property-listings' ) . '</div>';
61:
62: echo '<div><a href="https://easypropertylistings.com.au/extensions/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_store">' . esc_html__( 'Purchase an extension remove me', 'easy-property-listings' ) . '</a></div>';
63: echo '</div>';
64: }
65:
66: /**
67: * Notice presented on settings page, removed if extension is present and activated
68: *
69: * @since 3.1.6
70: */
71: function epl_admin_sidebar() {
72:
73: if ( has_filter( 'epl_extensions_options_filter_new' ) || get_transient( 'epl_admin_notices_display' ) === '1' ) {
74: return;
75: }
76:
77: $service_banners = array(
78: array(
79: 'url' => 'https://easypropertylistings.com.au/extensions/core-bundle/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_core',
80: 'img' => 'banner-dash-wide.png',
81: 'alt' => esc_html__( 'Core bundle for Easy Property Listings', 'easy-property-listings' ),
82: 'width' => 261,
83: ),
84: array(
85: 'url' => 'https://easypropertylistings.com.au/extensions/advanced-mapping/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_advanced_map',
86: 'img' => 'offer-advanced-mapping.png',
87: 'alt' => esc_html__( 'Advanced Mapping', 'easy-property-listings' ),
88: 'width' => 261,
89: ),
90: array(
91: 'url' => 'https://easypropertylistings.com.au/extensions/sliders/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_sliders',
92: 'img' => 'offer-sliders.png',
93: 'alt' => esc_html__( 'Sliders extension', 'easy-property-listings' ),
94: 'width' => 261,
95: ),
96: array(
97: 'url' => 'https://easypropertylistings.com.au/extensions/staff-directory/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_staff_directory',
98: 'img' => 'offer-staff-directory.png',
99: 'alt' => esc_html__( 'Staff Directory', 'easy-property-listings' ),
100: 'width' => 261,
101: ),
102: array(
103: 'url' => 'https://easypropertylistings.com.au/extensions/listing-templates/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_listing_templates',
104: 'img' => 'offer-templates.png',
105: 'alt' => esc_html__( 'Listing Templates', 'easy-property-listings' ),
106: 'width' => 261,
107: ),
108: );
109: $i = 0;
110: foreach ( $service_banners as $banner ) {
111: echo '<a target="_blank" href="' . esc_url( $banner['url'] ) . '"><img width="' . esc_attr( $banner['width'] ) . '" src="' . esc_url( plugins_url( 'lib/assets/images/' . $banner['img'], EPL_PLUGIN_FILE ) ) . '" alt="' . esc_attr( $banner['alt'] ) . '"/></a><br/><br/>';
112: ++$i;
113: }
114: ?>
115: <div class="epl-admin-offer" style="margin-bottom: 1em;">
116: <div><span><?php esc_html_e( 'Remove these ads?', 'easy-property-listings' ); ?><span></div>
117: <div><a href="http://easypropertylistings.com.au/extensions/?utm_source=offers&utm_medium=description_tab&utm_content=settings_link&utm_campaign=epl_extension_store"><?php esc_html_e( 'Purchase and activate any extension', 'easy-property-listings' ); ?> &#x27F6;</a></div>
118: </div>
119: <?php
120: }
121:
122: /**
123: * Notice presented on dashboard widget, removed if extension is present and activated
124: *
125: * @since 3.1.6
126: */
127: function epl_dashboard_widget_offer() {
128: if ( has_filter( 'epl_extensions_options_filter_new' ) || get_transient( 'epl_admin_notices_display' ) === '1' ) {
129: return;
130: }
131:
132: $service_banners = array(
133: array(
134: 'url' => 'https://easypropertylistings.com.au/extensions/core-bundle/?utm_source=offers&utm_medium=description_tab&utm_content=extensions_link&utm_campaign=epl_extension_core',
135: 'img' => 'banner-dash-wide.png',
136: 'alt' => esc_html__( 'Check out the premium extensions', 'easy-property-listings' ),
137: 'width' => 261,
138: ),
139: array(
140: 'url' => 'https://easypropertylistings.com.au/extensions/?utm_source=offers&utm_medium=description_tab&utm_content=extensions_link&utm_campaign=epl_extension_store',
141: 'img' => 'banner-dash-wide-store.png',
142: 'alt' => esc_html__( 'Browse the extension store', 'easy-property-listings' ),
143: 'width' => 125,
144: ),
145: );
146: $i = 0;
147: foreach ( $service_banners as $banner ) {
148: echo '<a target="_blank" href="' . esc_url( $banner['url'] ) . '">
149: <img style="margin-right: 0.5em" width="' . esc_attr( $banner['width'] ) . '" src="' . esc_url( plugins_url( 'lib/assets/images/' . $banner['img'], EPL_PLUGIN_FILE ) ) . '" alt="' . esc_attr( $banner['alt'] ) . '"/>
150: </a>';
151: ++$i;
152: }
153: ?>
154:
155: <div class="epl-admin-offer" style="margin-bottom: 1em;">
156: <div><span><?php esc_html_e( 'Remove these ads?', 'easy-property-listings' ); ?><span></div>
157: <div><a href="http://easypropertylistings.com.au/extensions/"><?php esc_html_e( 'Purchase and activate any extension', 'easy-property-listings' ); ?> &#x27F6;</a></div>
158: </div>
159: <?php
160: }
161:
162: /**
163: * Notice presented on post types, removed if extension is present and activated
164: *
165: * @since 3.1.6
166: */
167: function epl_dashboard_widget_offer_post_types() {
168: if ( has_filter( 'epl_extensions_options_filter_new' ) || get_transient( 'epl_admin_notices_display' ) === '1' ) {
169: return;
170: }
171:
172: $service_banners = array(
173: array(
174: 'url' => 'https://easypropertylistings.com.au/extensions/?utm_source=offers&utm_medium=description_tab&utm_content=post_link&utm_campaign=epl_extensions',
175: 'img' => 'banner-dash-wide-store.png',
176: 'alt' => esc_html__( 'Browse the extension store', 'easy-property-listings' ),
177: 'width' => 125,
178: ),
179: array(
180: 'url' => 'https://easypropertylistings.com.au/extensions/advanced-mapping/?utm_source=offers&utm_medium=description_tab&utm_content=post_link&utm_campaign=epl_extension_advanced_map',
181: 'img' => 'offer-advanced-mapping.png',
182: 'alt' => esc_html__( 'Advanced Mapping', 'easy-property-listings' ),
183: 'width' => 175,
184: ),
185: array(
186: 'url' => 'https://easypropertylistings.com.au/extensions/sliders/?utm_source=offers&utm_medium=description_tab&utm_content=post_link&utm_campaign=epl_extension_sliders',
187: 'img' => 'offer-sliders.png',
188: 'alt' => esc_html__( 'Sliders extension', 'easy-property-listings' ),
189: 'width' => 175,
190: ),
191: array(
192: 'url' => 'https://easypropertylistings.com.au/extensions/staff-directory/?utm_source=offers&utm_medium=description_tab&utm_content=post_link&utm_campaign=epl_extension_staff_directory',
193: 'img' => 'offer-staff-directory.png',
194: 'alt' => esc_html__( 'Staff Directory', 'easy-property-listings' ),
195: 'width' => 175,
196: ),
197: array(
198: 'url' => 'https://easypropertylistings.com.au/extensions/listing-templates/?utm_source=offers&utm_medium=description_tab&utm_content=post_link&utm_campaign=epl_extension_listing_templates',
199: 'img' => 'offer-templates.png',
200: 'alt' => esc_html__( 'Listing Templates', 'easy-property-listings' ),
201: 'width' => 175,
202: ),
203: );
204: $i = 0;
205: foreach ( $service_banners as $banner ) {
206: echo '<a target="_blank" href="' . esc_url( $banner['url'] ) . '">
207: <img style="display: block; float: left; margin: 0.5em 0.5em 0.5em 0" width="' . esc_attr( $banner['width'] ) . '" src="' . esc_url( plugins_url( 'lib/assets/images/' . $banner['img'], EPL_PLUGIN_FILE ) ) . '" alt="' . esc_attr( $banner['alt'] ) . '"/>
208: </a>';
209: ++$i;
210: }
211: ?>
212: <?php
213: }
214:
215: /**
216: * Notice presented on post types, removed if extension is present and activated
217: *
218: * @since 3.1.6
219: */
220: function epl_dashboard_widget_offer_post_types_last() {
221: if ( has_filter( 'epl_extensions_options_filter_new' ) || get_transient( 'epl_admin_notices_display' ) === '1' ) {
222: return;
223: }
224:
225: echo '<a target="_blank" href="https://easypropertylistings.com.au/extensions/?utm_source=offers&utm_medium=description_tab&utm_content=extensions_link&utm_campaign=epl_extension_store">
226: <img style="display: block; float: right; margin: 0.5em 0 0.5em 0.5em" width="125" src="' . esc_url( plugins_url( 'lib/assets/images/banner-dash-wide-store-reverse.png', EPL_PLUGIN_FILE ) ) . '" alt="' . esc_html__( 'Purchase and activate any extension to remove these ads', 'easy-property-listings' ) . '"/>
227: </a>';
228: ?>
229: <?php
230: }
231:
232: /**
233: * Admin notices
234: *
235: * @since 3.1.6
236: */
237: function epl_extension_notice_offer() {
238:
239: if ( has_filter( 'epl_extensions_options_filter_new' ) || get_transient( 'epl_admin_notices_display' ) === '1' ) {
240: return;
241: }
242:
243: $screen = get_current_screen();
244:
245: if ( is_epl_core_post() === true || 'easy-property-listings_page_epl-reports' === $screen->id || 'easy-property-listings_page_epl-contacts' === $screen->id || 'toplevel_page_epl-general' === $screen->id ) {
246:
247: echo '<div class="notice notice-success" style="max-height: 130px; overflow: hidden;">';
248: echo '<div class="epl-offers epl-offers-left" style="box-sizing: border-box; width: 85%; float: left; height: 125px">';
249: epl_dashboard_widget_offer_post_types();
250: echo '</div>';
251:
252: echo '<div class="epl-offers epl-offers-right" style="box-sizing: border-box; width: 15%; float: left; height: 125px">';
253: epl_dashboard_widget_offer_post_types_last();
254: echo '</div>';
255: echo '</div>';
256: }
257: }
258: add_action( 'admin_notices', 'epl_extension_notice_offer' );
259:
260: /**
261: * Check if a given extensions has beta support enabled
262: *
263: * @since 3.3.0
264: * @param string $slug The slug of the extension to check.
265: * @return bool True if enabled, false otherwise.
266: */
267: function epl_extension_has_beta_support( $slug ) {
268:
269: $enabled_betas = epl_get_option( 'enabled_betas', array() );
270: $return = false;
271: if ( in_array( $slug, $enabled_betas, true ) ) {
272: $return = true;
273: }
274: return $return;
275: }
276:
277: /**
278: * EPL Tools Tabs
279: *
280: * @since 3.3.0
281: */
282: function epl_get_tools_tab() {
283:
284: $default_tabs = array(
285: 'tools' => array(
286: 'label' => esc_html__( 'Import/Export', 'easy-property-listings' ),
287: 'callback' => 'epl_settings_import_export',
288: ),
289: );
290:
291: if ( epl_show_reset_tab() ) :
292:
293: $default_tabs['reset'] = array(
294: 'label' => esc_html__( 'Reset Settings', 'easy-property-listings' ),
295: 'callback' => 'epl_settings_reset',
296: );
297:
298: endif;
299:
300: if ( epl_show_upgrade_tab() ) :
301:
302: $default_tabs['upgrade'] = array(
303: 'label' => esc_html__( 'Upgrade', 'easy-property-listings' ),
304: 'callback' => 'epl_settings_upgrade_tab',
305: );
306:
307: endif;
308:
309: return apply_filters( 'epl_get_tools_tab', $default_tabs );
310: }
311:
312: /**
313: * Display Upgrade Tab
314: *
315: * @since 3.3
316: */
317: function epl_show_upgrade_tab() {
318: // phpcs:disable WordPress.Security.NonceVerification
319: $upgraded_to = get_option( 'epl_db_upgraded_to' );
320:
321: if ( ! isset( $_GET['dev'] ) && empty( $upgraded_to ) ) {
322: return false;
323: }
324:
325: $upgraded = $upgraded_to < 3.3 ? true : false;
326:
327: $upgraded = isset( $_GET['dev'] ) ? true : $upgraded;
328:
329: return $upgraded;
330: }
331:
332: /**
333: * Display Upgrade Tab
334: *
335: * @since 3.3.5
336: */
337: function epl_show_reset_tab() {
338: $show = false;
339:
340: $show = isset( $_GET['dev'] ) ? true : $show;
341:
342: return $show;
343: }
344:
345: /**
346: * Serialize Variable
347: *
348: * @since 3.3.0
349: * @param string $data String of data to serialize.
350: * @return string.
351: */
352: function epl_serialize( $data ) {
353: return base64_encode( serialize( $data ) ); //phpcs:ignore
354: }
355:
356: /**
357: * Safely unserialize base64 encoded data.
358: *
359: * This helper decodes a base64 encoded string and attempts to unserialize it
360: * while applying several validation steps to reduce security risks.
361: *
362: * Security improvements:
363: * - Uses strict base64 decoding to prevent malformed input.
364: * - Validates that the decoded value is actually a serialized string before
365: * attempting to unserialize it.
366: * - Prevents object injection by disabling object instantiation via the
367: * `allowed_classes => false` option.
368: *
369: * If the input cannot be decoded or is not a valid serialized value, the
370: * function safely returns false instead of attempting to unserialize it.
371: *
372: * @since 3.3.0
373: * @since 3.5.21 Hardened unserialize handling by enforcing strict base64 decoding, validating serialized input, and disabling object instantiation.
374: *
375: * @param string $data Base64 encoded serialized data.
376: * @return mixed|false Returns the unserialized value on success, or false if the
377: * input is invalid or cannot be safely unserialized.
378: */
379: function epl_unserialize( $data ) {
380: $decoded_data = base64_decode( trim( (string) $data ), true );
381:
382: if ( false === $decoded_data || ! is_serialized( $decoded_data ) ) {
383: return false;
384: }
385:
386: return unserialize( $decoded_data, array( 'allowed_classes' => false ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
387: }
388:
389: /**
390: * Import/Export Tools Settings Screen
391: *
392: * @since 3.3
393: * @since 3.5.21 Added nonce protection to the export request to prevent CSRF.
394: */
395: function epl_settings_import_export() {
396:
397: do_action( 'epl_pre_import_fields' );
398:
399: echo '<h2>' . esc_html__( 'Import Options', 'easy-property-listings' ) . '</h2>';
400: ?>
401:
402: <div class="epl-field">
403: <div class="epl-label-wrap">
404: <label class="epl-label epl-label-epl_import" for="epl_import">
405: <?php
406: esc_html_e( 'Import data', 'easy-property-listings' );
407: ?>
408: </label>
409: </div>
410: <div class="epl-input-wrap">
411: <input type="file" name="epl_import" id="epl_import" />
412: <span class="epl-help-text">
413: <?php
414: esc_html_e( 'Import exported file here. Warning! This will override all existing settings to default values.', 'easy-property-listings' );
415: ?>
416: </span>
417: </div>
418: </div>
419:
420: <input type="hidden" name="action" value="import">
421: <div class="">
422: <input type="submit" name="epl_tools_submit" value="<?php esc_html_e( 'Import', 'easy-property-listings' ); ?>" class="epl-tools-submit button button-primary"/>
423: <span style="color:#f00"><?php esc_html_e( 'WARNING! This will overwrite all existing option values, please proceed with caution.', 'easy-property-listings' ); ?></span>
424: </div>
425:
426:
427: <?php
428:
429: do_action( 'epl_post_import_fields' );
430:
431: echo '<h2>' . esc_html__( 'Export Options', 'easy-property-listings' ) . '</h2>';
432:
433: $tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'tools';
434:
435: $export_url = add_query_arg(
436: array(
437: 'page' => 'epl-tools',
438: 'tab' => $tab,
439: 'action' => 'export',
440: 'epl_tools_submit' => 'true',
441: ),
442: admin_url( 'admin.php' )
443: );
444: $export_url = wp_nonce_url( $export_url, 'epl_tools_export', 'epl_tools_export_nonce' );
445:
446: echo "<a class='button button-primary' href='" . esc_url( $export_url ) . "'>" . esc_html__( 'Download File', 'easy-property-listings' ) . '</a>';
447: ?>
448: <span style="color:#f00"><?php esc_html_e( 'The following settings are exported. Easy Property Listings settings screen and any Extension settings', 'easy-property-listings' ); ?></span>
449: <?php
450:
451: do_action( 'epl_pre_export_fields' );
452:
453: do_action( 'epl_post_export_fields' );
454: }
455:
456: /**
457: * Reset EPL Settings : Tools Settings Screen
458: *
459: * @since 3.3.5
460: */
461: function epl_settings_reset() {
462:
463: do_action( 'epl_pre_settings_reset_fields' );
464:
465: if ( isset( $_GET['epl_tools_submit'] ) && 'true' === sanitize_text_field( wp_unslash( $_GET['epl_tools_submit'] ) ) ) {
466: ?>
467: <br>
468: <span style="color:#080"><?php esc_html_e( 'Settings reset successfully!', 'easy-property-listings' ); ?></span>
469: <?php
470: }
471:
472: $url = '?page=epl-tools&tab=reset&action=reset&epl_tools_submit=true';
473: $url = add_query_arg( array( 'dev' => 'true' ), $url );
474:
475: $url = wp_nonce_url( $url, $action = 'epl_reset_settings', $name = '_reset_wpnonce' );
476: $confirm = esc_html__( 'Are you sure you want to reset all Easy Property Listings settings?', 'easy-property-listings' );
477: echo '<h2>' . esc_html__( 'Reset Easy Property Listings to installation defaults', 'easy-property-listings' ) . '</h2>';
478:
479: echo "<a class='button button-primary' href='" . esc_url( $url ) . "' onclick='return confirm(\"" . esc_html( $confirm ) . "\")'>" . esc_html__( 'Reset', 'easy-property-listings' ) . '</a>';
480: ?>
481: <br>
482: <span style="color:#f00"><?php esc_html_e( 'Warning! This will reset all your Easy Property Listings settings to their default values including extension settings.', 'easy-property-listings' ); ?></span>
483: <?php
484:
485: do_action( 'epl_post_settings_reset_fields' );
486: }
487:
488: /**
489: * Settings upgrade tab screen
490: *
491: * @since 3.3.0
492: */
493: function epl_settings_upgrade_tab() {
494:
495: echo '<h2>' . esc_html__( 'Upgrade Options', 'easy-property-listings' ) . '</h2>';
496:
497: echo '<div style="color:red">' . esc_html__( 'Upgrading the database will copy all the listing pricing info into a unified price data column for searching and ordering. We recommend taking a database backup before performing this action.', 'easy-property-listings' ) . '</div>';
498:
499: echo "<div><br><a class='button button-primary epl-upgrade-btn' data-upgrade='3.3' href='#'>" . esc_html__( 'Upgrade Database', 'easy-property-listings' ) . '</a></div>';
500:
501: echo '<div style="display:none;" class="epl-ajax-notice">' . esc_html__( 'Processing...', 'easy-property-listings' ) . '</div>';
502: }
503:
504: /**
505: * Import and Export Form
506: *
507: * @since 3.3.0
508: * @since 3.5 Fixed import function.
509: * @since 3.5.10 Fix: Tools Import function adjusted with more checked before performing the settings import.
510: * @since 3.5.21 Hardened tools request handling with capability checks, stricter sanitization, action allowlisting, and export nonce verification.
511: */
512: function epl_handle_tools_form() {
513: $page = isset( $_REQUEST['page'] ) ? sanitize_key( wp_unslash( $_REQUEST['page'] ) ) : '';
514: if ( 'epl-tools' !== $page || ! isset( $_REQUEST['epl_tools_submit'] ) ) {
515: return;
516: }
517:
518: if ( ! current_user_can( 'manage_options' ) ) {
519: return;
520: }
521:
522: if ( ! isset( $_REQUEST['action'] ) ) {
523: return;
524: }
525:
526: $action = sanitize_key( wp_unslash( $_REQUEST['action'] ) );
527: if ( ! in_array( $action, array( 'export', 'import', 'reset' ), true ) ) {
528: return;
529: }
530:
531: if ( 'export' === $action ) {
532: epl_verify_export_nonce();
533: }
534:
535: if ( 'import' === $action ) {
536: epl_verify_nonce();
537: epl_validate_import_file();
538: }
539:
540: switch ( $action ) {
541: case 'export':
542: epl_export_settings();
543: break;
544:
545: case 'import':
546: epl_import_settings();
547: break;
548:
549: case 'reset':
550: epl_reset_settings();
551: break;
552: }
553: }
554: add_action( 'admin_init', 'epl_handle_tools_form' );
555:
556: /**
557: * Verify nonce for export tools action.
558: *
559: * @since 3.5.20
560: */
561: function epl_verify_export_nonce() {
562: if (
563: ! isset( $_GET['epl_tools_export_nonce'] ) ||
564: ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['epl_tools_export_nonce'] ) ), 'epl_tools_export' )
565: ) {
566: wp_die( esc_html__( 'Sorry, your nonce did not verify.', 'easy-property-listings' ) );
567: }
568: }
569:
570: /**
571: * Verify nonce for the tools form.
572: *
573: * @since 3.5.10
574: */
575: function epl_verify_nonce() {
576: if (
577: ! isset( $_POST['epl_nonce_tools_form'] ) ||
578: ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['epl_nonce_tools_form'] ) ), 'epl_nonce_tools_form' )
579: ) {
580: wp_die( esc_html__( 'Sorry, your nonce did not verify.', 'easy-property-listings' ) );
581: }
582: }
583:
584: /**
585: * Validate the import file.
586: *
587: * @since 3.5.10
588: * @since 3.5.21 Added stricter import upload validation (uploaded file checks, size limits, and file type/extension verification).
589: */
590: function epl_validate_import_file() {
591:
592: if ( empty( $_FILES['epl_import'] ) || empty( $_FILES['epl_import']['name'] ) ) {
593: wp_die( esc_html__( 'Missing import file. Please provide an import file.', 'easy-property-listings' ) );
594: }
595:
596: $file_name = sanitize_file_name( wp_unslash( $_FILES['epl_import']['name'] ) );
597: $tmp_name = isset( $_FILES['epl_import']['tmp_name'] ) ? wp_unslash( $_FILES['epl_import']['tmp_name'] ) : '';
598: $file_size = isset( $_FILES['epl_import']['size'] ) ? (int) $_FILES['epl_import']['size'] : 0;
599:
600: if ( isset( $_FILES['epl_import']['error'] ) && $_FILES['epl_import']['error'] > 0 ) {
601: wp_die( esc_html__( 'Error uploading the import file.', 'easy-property-listings' ) );
602: }
603:
604: if ( empty( $tmp_name ) || ! is_uploaded_file( $tmp_name ) || ! is_readable( $tmp_name ) ) {
605: wp_die( esc_html__( 'Invalid import upload.', 'easy-property-listings' ) );
606: }
607:
608: if ( $file_size <= 0 || $file_size > wp_max_upload_size() ) {
609: wp_die( esc_html__( 'The selected import file size is not valid.', 'easy-property-listings' ) );
610: }
611:
612: $file_check = wp_check_filetype_and_ext( $tmp_name, $file_name, array( 'txt' => 'text/plain' ) );
613: if ( empty( $file_check['ext'] ) || 'txt' !== strtolower( $file_check['ext'] ) ) {
614: wp_die( esc_html__( 'The file you uploaded does not appear to be a valid import file.', 'easy-property-listings' ) );
615: }
616: }
617:
618: /**
619: * Export the settings.
620: *
621: * @since 3.5.10
622: */
623: function epl_export_settings() {
624: $export = get_option( 'epl_settings' );
625:
626: header( 'Content-Description: File Transfer' );
627: header( 'Content-Type: application/octet-stream' );
628: header( 'Content-Disposition: attachment; filename=epl-settings-export.txt' );
629: header( 'Content-Transfer-Encoding: binary' );
630: header( 'Expires: 0' );
631: header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
632: header( 'Pragma: public' );
633: ob_clean();
634: flush();
635: echo epl_serialize( $export ); // phpcs:ignore
636: die;
637: }
638:
639: /**
640: * Import the settings.
641: *
642: * @since 3.5.10
643: * @since 3.5.18 Check for data before continue.
644: * @since 3.5.21 Hardened import processing by reading from the uploaded temp file, validating file contents, and verifying unserialized data before updating options.
645: */
646: function epl_import_settings() {
647: if ( ! isset( $_FILES['epl_import']['tmp_name'] ) ) {
648: return;
649: }
650:
651: $tmp_name = wp_unslash( $_FILES['epl_import']['tmp_name'] );
652: if ( ! is_readable( $tmp_name ) ) {
653: wp_die( esc_html__( 'Unable to read import file.', 'easy-property-listings' ) );
654: }
655:
656: $imported_raw_data = file_get_contents( $tmp_name ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
657: if ( false === $imported_raw_data || '' === $imported_raw_data ) {
658: wp_die( esc_html__( 'Unable to read import file.', 'easy-property-listings' ) );
659: }
660:
661: $imported_data = epl_unserialize( $imported_raw_data );
662: if ( ! is_array( $imported_data ) ) {
663: wp_die( esc_html__( 'The import file data is invalid.', 'easy-property-listings' ) );
664: }
665:
666: $options_backup = get_option( 'epl_settings' );
667: update_option( 'epl_settings_backup', $options_backup );
668: update_option( 'epl_settings', $imported_data );
669: }
670:
671: /**
672: * Reset the settings.
673: *
674: * @since 3.5.10
675: */
676: function epl_reset_settings() {
677: if ( ! isset( $_GET['_reset_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_reset_wpnonce'] ) ), 'epl_reset_settings' ) ) {
678: wp_die( esc_html__( 'Sorry, your nonce did not verify.', 'easy-property-listings' ) );
679: } else {
680: $epl_settings = epl_get_default_settings();
681: update_option( 'epl_settings', $epl_settings );
682: }
683: }
684:
685: /**
686: * Upgrade Database Notice
687: *
688: * @since 3.3.0
689: * @since 3.5.18 Check user can manage options.
690: */
691: function epl_upgrade_admin_notice() {
692:
693: $upgraded_to = get_option( 'epl_db_upgraded_to' );
694:
695: if ( ! empty( $upgraded_to ) && $upgraded_to < 3.3 && current_user_can( 'manage_options' ) ) :
696:
697: $head = esc_html__( 'It looks like you upgraded to latest version of Easy Property Listings', 'easy-property-listings' );
698:
699: $msg = esc_html__( 'For better performance, we need to upgrade the database. You can upgrade the database by visiting tools page', 'easy-property-listings' );
700:
701: echo '<div class="notice notice-warning epl-upgrade-notice is-dismissible">
702: <p><strong>' . esc_html( $head ) . '</strong></p>
703: <p>' . esc_html( $msg ) . '</p>
704: <p><a class="button" href="' . esc_url( admin_url( 'admin.php?page=epl-tools&tab=upgrade&dev=true' ) ) . '">' . esc_html__( 'Take me to the upgrade tool', 'easy-property-listings' ) . '</a></p>
705: </div>';
706: endif;
707: }
708: add_action( 'admin_notices', 'epl_upgrade_admin_notice' );
709:
710: /**
711: * Upgrade EPL Database to 3.3
712: *
713: * @since 3.3.0
714: * @since 3.5.18 Added nonce check.
715: */
716: function epl_upgrade_db() {
717:
718: if ( ! current_user_can( 'manage_options' ) ) {
719: wp_die(
720: wp_json_encode(
721: array(
722: 'status' => 'fail',
723: 'msg' => esc_html__(
724: 'Unauthorized',
725: 'easy-property-listings'
726: ),
727: )
728: )
729: );
730: }
731:
732: check_ajax_referer( 'epl_ajax_nonce', '_epl_nonce' );
733:
734: if ( ! isset( $_POST['upgrade_to'] ) ) {
735: wp_die(
736: wp_json_encode(
737: array(
738: 'status' => 'fail',
739: 'msg' => esc_html__(
740: 'Some error occured',
741: 'easy-property-listings'
742: ),
743: )
744: )
745: );
746: }
747:
748: $ver = abs( floatval( $_POST['upgrade_to'] ) );
749:
750: switch ( $ver ) {
751:
752: case '3.3':
753: epl_upgrade_db_to_3_3();
754: break;
755: }
756: }
757: add_action( 'wp_ajax_epl_upgrade_db', 'epl_upgrade_db' );
758:
759: /**
760: * Update user note comment type to note
761: *
762: * @since 3.3.0
763: */
764: function epl_update_user_note_type() {
765:
766: $comments_query = new WP_Comment_Query();
767:
768: $comments = $comments_query->query(
769: array(
770: 'type__in' => array( 'epl_user_note' ),
771: )
772: );
773:
774: if ( ! empty( $comments ) ) {
775: foreach ( $comments as $comment ) {
776: wp_update_comment(
777: array(
778: 'comment_ID' => $comment->comment_ID,
779: 'comment_type' => 'note',
780: )
781: );
782: }
783: }
784: }
785:
786: /**
787: * Check if need to upgrade EPL Database to 3.3
788: *
789: * @since 3.3.0
790: */
791: function epl_upgrade_db_to_3_3() {
792:
793: $updated_listings = (array) get_option( 'epl_updated_global_price' );
794: $current_batch = array();
795:
796: $args = array(
797: 'post_type' => epl_get_core_post_types(),
798: 'post_status' => 'any',
799: 'numberposts' => 200, // phpcs:ignore
800: // 200 listings in single batch.
801: 'exclude' => $updated_listings,
802: );
803:
804: $all_posts = get_posts( $args );
805:
806: if ( ! empty( $all_posts ) ) {
807:
808: foreach ( $all_posts as $single ) {
809:
810: switch ( $single->post_type ) {
811:
812: case 'rental':
813: $price = get_post_meta( $single->ID, 'property_rent', true );
814: update_post_meta( $single->ID, 'property_price_global', $price );
815: break;
816:
817: case 'commercial':
818: $price = get_post_meta( $single->ID, 'property_price', true );
819: if ( empty( $price ) ) {
820: $price = get_post_meta( $single->ID, 'property_com_rent', true );
821: }
822: update_post_meta( $single->ID, 'property_price_global', $price );
823: break;
824:
825: default:
826: $price = get_post_meta( $single->ID, 'property_price', true );
827: update_post_meta( $single->ID, 'property_price_global', $price );
828: break;
829: }
830: $current_batch[] = $single->ID;
831: $updated_listings[] = $single->ID;
832: }
833:
834: update_option( 'epl_updated_global_price', $updated_listings );
835: wp_die(
836: wp_json_encode(
837: array(
838: 'status' => 'success',
839: 'buffer' => 'processing',
840: /* Translators: %s is a string of listing post ids. */
841: 'msg' => sprintf( esc_html__( 'Database upgrade in process, following listings updated : %s. Please wait...', 'easy-property-listings' ), implode( ', ', $current_batch ) ),
842: )
843: )
844: );
845:
846: } else {
847:
848: update_option( 'epl_db_upgraded_to', '3.3' );
849: wp_die(
850: wp_json_encode(
851: array(
852: 'status' => 'success',
853: 'buffer' => 'complete',
854: 'msg' => esc_html__( 'Database upgraded successfully', 'easy-property-listings' ),
855: )
856: )
857: );
858: }
859: }
860:
861: /**
862: * Update the property_price_global when saving or updating an EPL post
863: *
864: * @param int $post_id The post id.
865: * @param array $post The post object.
866: * @param array $_update Update.
867: *
868: * @since 3.3.0
869: * @since 3.5.18 Private variable $_update.
870: */
871: function epl_sync_property_price_global( $post_id, $post, $_update ) {
872:
873: if ( is_epl_post() ) {
874:
875: if ( 'rental' === $post->post_type ) {
876: $price = get_post_meta( $post_id, 'property_rent', true );
877: update_post_meta( $post_id, 'property_price_global', $price );
878:
879: } elseif ( 'commercial' === $post->post_type ) {
880:
881: $price = get_post_meta( $post_id, 'property_price', true );
882: if ( empty( $price ) ) {
883: $price = get_post_meta( $post_id, 'property_com_rent', true );
884: }
885: update_post_meta( $post_id, 'property_price_global', $price );
886:
887: } else {
888:
889: $price = get_post_meta( $post_id, 'property_price', true );
890: update_post_meta( $post_id, 'property_price_global', $price );
891: }
892: }
893: }
894:
895: add_action( 'save_post', 'epl_sync_property_price_global', 40, 3 );
896:
897: /**
898: * Filter the contacts comments
899: *
900: * @param array $avatar Update.
901: * @param string $id_or_email User ID or email address.
902: * @param array $_args Arguments.
903: *
904: * @return array|string $avatar
905: *
906: * @since 3.3.0
907: * @since 3.5.18 Private variable $_args.
908: */
909: function epl_get_avatar_filter( $avatar, $id_or_email, $_args ) {
910:
911: if ( ! is_object( $id_or_email ) ) {
912: return $avatar;
913: }
914:
915: if ( 'easy-property-listings' !== $id_or_email->comment_agent ) {
916: return $avatar;
917: }
918:
919: $label = esc_html__( 'Note', 'easy-property-listings' );
920:
921: switch ( $id_or_email->comment_type ) {
922:
923: case 'call':
924: $label = esc_html__( 'Call', 'easy-property-listings' );
925: break;
926:
927: case 'email':
928: $label = esc_html__( 'Mail', 'easy-property-listings' );
929: break;
930:
931: case 'note':
932: $label = esc_html__( 'Note', 'easy-property-listings' );
933: break;
934:
935: case 'listing_alert':
936: $label = esc_html__( 'Alert', 'easy-property-listings' );
937: break;
938:
939: default:
940: $label = esc_html__( 'Note', 'easy-property-listings' );
941: break;
942: }
943:
944: return '<div class="avatar epl-notes-grav">
945: <div class="epl-notes-icon">
946: ' . substr( $label, 0, 1 ) . '
947: </div>
948: <div class="epl-notes-label">
949: ' . $label . '
950: </div>
951: </div>';
952: }
953: add_filter( 'pre_get_avatar', 'epl_get_avatar_filter', 10, 5 );
954:
955: /**
956: * Update a featured listing when pressing the star icon
957: *
958: * @since 3.3.0
959: * @since 3.5.23 Fix: Broken Access Control in epl_update_featured_listing (CVE-2025-64242).
960: */
961: function epl_update_featured_listing() {
962:
963: check_ajax_referer( 'epl_ajax_nonce', '_epl_nonce' );
964:
965: $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
966:
967: if ( $id <= 0 ) {
968: return;
969: }
970:
971: if ( ! current_user_can( 'edit_post', $id ) ) {
972: wp_die(
973: wp_json_encode(
974: array(
975: 'status' => 'unauthorized',
976: 'featured' => 'no',
977: )
978: )
979: );
980: }
981:
982: $featured = get_post_meta( $id, 'property_featured', true );
983: $is_featured = 'no';
984:
985: if ( 'yes' === $featured ) {
986: update_post_meta( $id, 'property_featured', '' );
987: $is_featured = 'no';
988: } else {
989: update_post_meta( $id, 'property_featured', 'yes' );
990: $is_featured = 'yes';
991: }
992:
993: wp_die(
994: wp_json_encode(
995: array(
996: 'status' => 'successful',
997: 'featured' => $is_featured,
998: )
999: )
1000: );
1001: }
1002: add_action( 'wp_ajax_epl_update_featured_listing', 'epl_update_featured_listing' );
1003: