-
Notifications
You must be signed in to change notification settings - Fork 1
/
cherry-real-estate.php
executable file
·694 lines (593 loc) · 17.5 KB
/
cherry-real-estate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
<?php
/**
* Plugin Name: Cherry Real Estate
* Plugin URI:
* Description: Plugin for adding real estate functionality to the site.
* Version: 1.1.6
* Author: Zemez
* Author URI: https://zemez.io/wordpress/
* Text Domain: cherry-real-estate
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Domain Path: /languages
*
* @package Cherry_Real_Estate
* @author Template Monster
* @license GPL-3.0+
* @copyright 2002-2016, Template Monster
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// If class `Cherry_Real_Estate` not exists.
if ( ! class_exists( 'Cherry_Real_Estate' ) ) {
/**
* Sets up and initializes the plugin.
*
* @since 1.0.0
*/
class Cherry_Real_Estate {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* A reference to an instance of cherry framework core class.
*
* @since 1.0.0
* @var object
*/
private $core = null;
/**
* The post type name.
*
* @since 1.0.0
* @var string
*/
private $post_type_name = 'tm-property';
/**
* The prefix for metadata.
*
* @since 1.0.0
* @var string
*/
private $meta_prefix = '_tm_property_';
/**
* The prefix for shortcodes.
*
* @since 1.0.0
* @var string
*/
private $shortcode_prefix = 'tm_re_';
/**
* Constructor method.
*
* @since 1.0.0
*/
private function __construct() {}
/**
* Sets up initial actions.
*
* @since 1.0.0
*/
private function actions() {
// Internationalize the text strings used.
add_action( 'plugins_loaded', array( $this, 'i18n' ) );
// Set up a Cherry core.
add_action( 'after_setup_theme', require( trailingslashit( dirname( __FILE__ ) ) . 'cherry-framework/setup.php' ), 0 );
add_action( 'after_setup_theme', array( $this, 'get_core' ), 1 );
add_action( 'after_setup_theme', array( 'Cherry_Core', 'load_all_modules' ), 2 );
// Adds supported features.
add_action( 'after_setup_theme', array( $this, 'support' ), 9 );
// Initialization of Cherry's modules.
add_action( 'after_setup_theme', array( $this, 'launch' ), 10 );
// Pluggable functions by plugins and themes.
add_action( 'after_setup_theme', array( $this, 'template_functions' ), 11 );
// Shortcodes.
add_action( 'init', array( $this, 'register_shortcodes' ) );
// TinyMCE.
add_action( 'admin_init', array( 'Cherry_RE_Shortcodes_Data', 'add_buttons' ) );
// Title on search properties page.
add_filter( 'document_title_parts', array( $this, 'search_title' ), 11 );
// Breacrumbs on search properties page.
add_filter( 'cherry_breadcrumbs_items', array( $this, 'search_breadcrumbs' ), 11, 2 );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ), 10, 4 );
// Enable use shortcodes in text widget.
add_filter( 'widget_text', 'do_shortcode', 11 );
// Register activation and deactivation hook.
register_activation_hook( __FILE__, array( $this, 'activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
}
/**
* Defines constants for the plugin.
*
* @since 1.0.0
*/
public function constants() {
/**
* Set constant path to the main file.
*
* @since 1.0.0
*/
define( 'CHERRY_REAL_ESTATE_MAIN_FILE', __FILE__ );
/**
* Set constant path to the plugin directory.
*
* @since 1.0.0
*/
define( 'CHERRY_REAL_ESTATE_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
/**
* Set constant path to the plugin URI.
*
* @since 1.0.0
*/
define( 'CHERRY_REAL_ESTATE_URI', trailingslashit( plugin_dir_url( __FILE__ ) ) );
/**
* Set the slug of the plugin.
*
* @since 1.0.0
*/
define( 'CHERRY_REAL_ESTATE_SLUG', basename( dirname( __FILE__ ) ) );
/**
* Set the version number of the plugin.
*
* @since 1.0.0
*/
define( 'CHERRY_REAL_ESTATE_VERSION', '1.1.6' );
}
/**
* Include required files used in admin and on the frontend.
*
* @since 1.0.0
*/
public function includes() {
// Models.
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/models/model-properties.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/models/model-agents.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/models/model-submit-form.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/models/model-settings.php' );
// Classes.
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-registration.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-assets.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-agent-data.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-property-data.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-shortcodes-data.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-template-loader.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-tools.php' );
// Functions.
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/core-functions.php' );
// Frontend.
if ( ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) ) {
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/template-hooks.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/class-cherry-re-template-callbacks.php' );
}
// Admin.
if ( is_admin() ) {
require_once( CHERRY_REAL_ESTATE_DIR . 'admin/class-cherry-re-admin.php' );
}
}
/**
* Loads the core functions. These files are needed before loading anything else in the
* theme because they have required functions for use.
*
* @since 1.0.0
*/
public function get_core() {
/**
* Fires before loads the core.
*
* @since 1.0.0
*/
do_action( 'cherry_re_core_before' );
global $chery_core_version;
if ( null !== $this->core ) {
return $this->core;
}
if ( 0 < sizeof( $chery_core_version ) ) {
$core_paths = array_values( $chery_core_version );
require_once( $core_paths[0] );
} else {
die( 'Class Cherry_Core not found' );
}
$this->core = new Cherry_Core( array(
'base_dir' => CHERRY_REAL_ESTATE_DIR . 'cherry-framework',
'base_url' => CHERRY_REAL_ESTATE_URI . 'cherry-framework',
'modules' => array(
'cherry-js-core' => array(
'autoload' => false,
),
'cherry-ui-elements' => array(
'autoload' => false,
),
'cherry-interface-builder' => array(
'autoload' => false,
),
'cherry-post-meta' => array(
'autoload' => false,
),
'cherry-widget-factory' => array(
'autoload' => false,
),
),
) );
return $this->core;
}
/**
* Adds supported features.
*
* @since 1.1.0
*/
public function support() {
add_theme_support( 'cherry-real-estate-geodata' );
}
/**
* Run initialization of modules.
*
* @since 1.0.0
*/
public function launch() {
$this->get_core()->init_module( 'cherry-js-core' );
$this->get_core()->init_module( 'cherry-widget-factory' );
if ( is_admin() ) {
$this->get_core()->init_module( 'cherry-ui-elements', array(
'ui_elements' => array(
'text',
'textarea',
'select',
'media',
'stepper',
'checkbox',
'switcher',
'repeater',
'iconpicker',
),
) );
}
/**
* Fire when all modules already loaded and ready for to use.
*
* @since 1.0.0
*/
do_action( 'cherry_re_modules_ready' );
// Load widgets.
$this->add_widgets();
}
/**
* Loads the translation files.
*
* @since 1.0.0
*/
public function i18n() {
load_plugin_textdomain( 'cherry-real-estate', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Function used to Init Template Functions.
*
* @since 1.0.0
*/
public function template_functions() {
require_once( CHERRY_REAL_ESTATE_DIR . 'includes/template-functions.php' );
}
/**
* Register shortcodes.
*
* @since 1.0.0
*/
public function register_shortcodes() {
$shortcoder = Cherry_RE_Shortcodes_Data::get_instance();
foreach ( (array) Cherry_RE_Shortcodes_Data::shortcodes() as $id => $data ) {
if ( isset( $data['function'] ) && is_callable( $data['function'] ) ) {
$func = $data['function'];
} elseif ( is_callable( array( $shortcoder, $id ) ) ) {
$func = array( $shortcoder, $id );
} else {
continue;
}
// Register shortcode.
add_shortcode( $this->get_shortcode_prefix() . $id, $func );
}
}
/**
* Add a plugin widgets.
*
* @since 1.0.0
*/
public function add_widgets() {
require_once( CHERRY_REAL_ESTATE_DIR . 'widgets/class-cherry-re-search-widget.php' );
require_once( CHERRY_REAL_ESTATE_DIR . 'widgets/class-cherry-re-properties-widget.php' );
}
/**
* Add `RE Agent` and `RE Contributor` roles.
*
* @since 1.0.0
*/
public function add_user_roles() {
$capability_type = $this->get_post_type_name();
// Define `RE Agent` capabilities.
$cap_agent = $this->get_agent_caps();
// Define `RE Contributor` capabilities.
$cap_contributor = $this->get_contributor_caps();
// Create `RE Agent` and `RE Contributor` roles.
add_role( 're_agent', esc_html__( 'RE Agent', 'cherry-real-estate' ), $cap_agent );
add_role( 're_contributor', esc_html__( 'RE Contributor', 'cherry-real-estate' ), $cap_contributor );
}
/**
* Remove `RE Agent` and `RE Contributor` roles.
*
* @since 1.0.0
*/
public function remove_user_roles() {
remove_role( 're_agent' );
remove_role( 're_contributor' );
}
/**
* Added a custom capabilities.
*
* @since 1.0.0
*/
public function update_caps() {
$capability_type = $this->get_post_type_name();
$cap_agent = $this->get_agent_caps();
// Update property capabilities to Admin Role.
$roles = apply_filters( 'cherry_re_update_roles_list', array( 'administrator' ) );
$capabilities = wp_parse_args( $cap_agent, array(
"delete_others_{$capability_type}s" => true,
"edit_others_{$capability_type}s" => true,
"publish_{$capability_type}s" => true,
"read_private_{$capability_type}s" => true,
"read_{$capability_type}_type" => true,
"edit_{$capability_type}_type" => true,
"read_{$capability_type}_tag" => true,
"edit_{$capability_type}_tag" => true,
"read_{$capability_type}_feature" => true,
"edit_{$capability_type}_feature" => true,
) );
$capabilities = apply_filters( 'cherry_re_custom_property_capabilities', $capabilities );
foreach ( (array) $roles as $name ) {
$role = get_role( $name );
if ( is_null( $role ) ) {
continue;
}
foreach ( $capabilities as $capability => $enabled ) {
if ( ! $enabled ) {
continue;
}
$role->add_cap( $capability );
}
}
}
/**
* Retrieve a `RE Agent` capabilities.
*
* @since 1.0.0
* @return array
*/
public function get_agent_caps() {
$capability_type = $this->get_post_type_name();
return apply_filters( 'cherry_re_agent_capabilities', array(
"delete_private_{$capability_type}s" => true,
"delete_published_{$capability_type}s" => true,
"delete_{$capability_type}s" => true,
"edit_private_{$capability_type}s" => true,
"edit_published_{$capability_type}s" => true,
"edit_{$capability_type}s" => true,
"read_{$capability_type}_type" => true,
"edit_{$capability_type}_type" => true,
"read_{$capability_type}_tag" => true,
"edit_{$capability_type}_tag" => true,
"read_{$capability_type}_feature" => true,
"edit_{$capability_type}_feature" => true,
'edit_posts' => true, // `save_post` action-callback check `edit_posts` capability.
'read' => true,
'upload_files' => true,
) );
}
/**
* Retrieve a `RE Contributor` capabilities.
*
* @since 1.0.0
* @return array
*/
public function get_contributor_caps() {
$capability_type = $this->get_post_type_name();
return apply_filters( 'cherry_re_contributor_capabilities', array(
"edit_published_{$capability_type}s" => true,
"edit_{$capability_type}s" => true,
'edit_posts' => true, // `save_post` action-callback check `edit_posts` capability.
'read' => true,
'upload_files' => true,
"read_{$capability_type}_type" => true,
"read_{$capability_type}_tag" => true,
"read_{$capability_type}_feature" => true,
) );
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*/
public function activation() {
/**
* Call CPT registration function.
*
* @link https://codex.wordpress.org/Function_Reference/flush_rewrite_rules#Examples
*/
Cherry_RE_Registration::register_post_type();
Cherry_RE_Registration::register_taxonomies();
$this->add_user_roles();
$this->update_caps();
flush_rewrite_rules();
do_action( 'cherry_re_plugin_activation' );
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*/
public function deactivation() {
$this->remove_user_roles();
flush_rewrite_rules();
do_action( 'cherry_re_plugin_deactivation' );
}
/**
* Fired when the plugin is uninstall.
*
* @since 1.0.0
*/
public static function uninstall() {
Model_Settings::remove_all_settings();
}
/**
* Customize the title on search properties page.
*
* @since 1.0.0
* @param array $title The document title parts.
* @return string
*/
public function search_title( $title ) {
if ( cherry_re_is_property_search() ) {
$title['title'] = esc_html__( 'Properties search', 'cherry-real-estate' );
}
return $title;
}
/**
* Customize the breadcrumbs on search properties page.
*
* @since 1.0.0
* @param array $items Indexed array of breadcrumb trail items.
* @param array $args Breadcrumb arguments.
* @return array
*/
public function search_breadcrumbs( $items, $args ) {
if ( cherry_re_is_property_search() ) {
$defaults = array(
'css_namespace' => array(
'item' => 'breadcrumbs__item',
'target' => 'breadcrumbs__item-target',
),
);
$args = wp_parse_args( $args, $defaults );
return array(
$items[0],
sprintf(
'<div class="%s"><span class="%s">%s</span></div>',
esc_attr( $args['css_namespace']['item'] ),
esc_attr( $args['css_namespace']['target'] ),
esc_html__( 'Properties search results', 'cherry-real-estate' )
),
);
}
return $items;
}
/**
* Added a link to the plugin settings page.
*
* @since 1.0.0
* @param array $actions An array of plugin action links.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @param array $plugin_data An array of plugin data.
* @param string $context The plugin context.
* @return array
*/
public function settings_link( $actions, $plugin_file, $plugin_data, $context ) {
$options_page = Cherry_RE_Options_Page::get_instance();
$path = sprintf(
'edit.php?post_type=%s&page=%s">',
$this->get_post_type_name(),
$options_page->get_page_slug()
);
$actions[] = sprintf(
'<a href="%s">%s</a>',
esc_url( get_admin_url( null, $path ) ),
esc_html__( 'Settings', 'cherry-real-estate' )
);
return $actions;
}
/**
* Get the template path.
*
* @since 1.0.0
* @return string
*/
public function template_path( $dir = '' ) {
$path = 'real-estate';
if ( $dir && is_string( $dir ) ) {
$path = trailingslashit( $path );
$path .= ltrim( $dir, '/' );
}
return apply_filters( 'cherry_re_template_path', trailingslashit( $path ) );
}
/**
* Get the plugin path.
*
* @since 1.0.0
* @param string $dir Path inside plugin dir.
* @return string
*/
public function plugin_path( $dir = '' ) {
$path = plugin_dir_path( __FILE__ );
if ( $dir && is_string( $dir ) ) {
$path = trailingslashit( $path );
$path .= ltrim( $dir, '/' );
}
return apply_filters( 'cherry_re_plugin_path', trailingslashit( $path ) );
}
/**
* Retrieve a post type name.
*
* @since 1.0.0
* @return string
*/
public function get_post_type_name() {
return apply_filters( 'cherry_re_get_post_type_name', $this->post_type_name );
}
/**
* Retrieve a prefix for metadata.
*
* @since 1.0.0
* @return string
*/
public function get_meta_prefix() {
return apply_filters( 'cherry_re_get_meta_prefix', $this->meta_prefix );
}
/**
* Retrieve a prefix for shortcodes.
*
* @since 1.0.0
* @return string
*/
public function get_shortcode_prefix() {
return apply_filters( 'cherry_re_get_shortcode_prefix', $this->shortcode_prefix );
}
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
self::$instance->constants();
self::$instance->includes();
self::$instance->actions();
}
return self::$instance;
}
}
}
/**
* Returns instance of main class.
*
* @since 1.0.0
* @return Cherry_Real_Estate
*/
function cherry_real_estate() {
return Cherry_Real_Estate::get_instance();
}
cherry_real_estate();