PluginProbe
Property Hive / 1.4.53
Property Hive v1.4.53
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-install.php

class-ph-install.php in Property Hive 1.4.53, at includes/class-ph-install.php

794 lines 23.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Installation related functions and actions.
4 *
5 * @author PropertyHive
6 * @category Admin
7 * @package PropertyHive/Classes
8 * @version 1.0.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
12
13 if ( ! class_exists( 'PH_Install' ) ) :
14
15 /**
16 * PH_Install Class
17 */
18 class PH_Install {
19
20 /**
21 * Hook in tabs.
22 */
23 public function __construct() {
24 register_activation_hook( PH_PLUGIN_FILE, array( $this, 'install' ) );
25 register_deactivation_hook( PH_PLUGIN_FILE, array( $this, 'deactivate' ) );
26
27 add_action( 'admin_init', array( $this, 'install_actions' ) );
28 add_action( 'admin_init', array( $this, 'check_version' ), 5 );
29
30 add_filter( 'cron_schedules', array( $this, 'custom_cron_recurrence' ) );
31 //add_action( 'in_plugin_update_message-propertyhive/propertyhive.php', array( $this, 'in_plugin_update_message' ) );
32 }
33
34 /**
35 * check_version function.
36 *
37 * @access public
38 * @return void
39 */
40 public function check_version() {
41 if ( ! defined( 'IFRAME_REQUEST' ) && ( get_option( 'propertyhive_version' ) != PH()->version || get_option( 'propertyhive_db_version' ) != PH()->version ) ) {
42 $this->install();
43
44 do_action( 'propertyhive_updated' );
45 }
46 }
47
48 /**
49 * Install actions such as installing pages when a button is clicked.
50 */
51 public function install_actions() {
52
53
54 }
55
56 /**
57 * Install Property Hive
58 */
59 public function install() {
60
61 $this->create_options();
62 $this->create_tables();
63 $this->create_roles();
64
65 // Register post types
66 include_once( 'class-ph-post-types.php' );
67 PH_Post_types::register_post_types();
68 PH_Post_types::register_taxonomies();
69
70 $this->create_primary_office();
71 $this->create_cron_jobs();
72
73 // Clear transient cache
74
75 // Queue upgrades
76 $current_version = get_option( 'propertyhive_version', null );
77 $current_db_version = get_option( 'propertyhive_db_version', null );
78
79 // No existing version set. This must be a new fresh install
80 if ( is_null( $current_version ) && is_null( $current_db_version ) )
81 {
82 $this->create_terms();
83 set_transient( '_ph_activation_redirect', 1, 30 );
84 }
85
86 update_option( 'propertyhive_db_version', PH()->version );
87
88 // Check if pages are needed
89 if ( ph_get_page_id( 'search_results' ) < 1 ) {
90 update_option( '_ph_needs_pages', 1 );
91 }
92
93 // Update version
94 update_option( 'propertyhive_version', PH()->version );
95
96 // Flush rules after install
97 flush_rewrite_rules();
98 }
99
100 /**
101 * Deactivate Property Hive
102 */
103 public function deactivate() {
104 // Cron jobs
105 wp_clear_scheduled_hook( 'propertyhive_update_currency_exchange_rates' );
106 wp_clear_scheduled_hook( 'propertyhive_process_email_log' );
107 wp_clear_scheduled_hook( 'propertyhive_auto_email_match' );
108 wp_clear_scheduled_hook( 'propertyhive_check_licenses' );
109 }
110
111 /**
112 * Handle updates
113 */
114 public function update() {
115 // Do updates
116 $current_db_version = get_option( 'propertyhive_db_version' );
117
118 /*if ( version_compare( $current_db_version, '1.4', '<' ) ) {
119 include( 'updates/propertyhive-update-1.4.php' );
120 update_option( 'propertyhive_db_version', '1.4' );
121 }
122
123 if ( version_compare( $current_db_version, '2.1.0', '<' ) || PH_VERSION == '2.1-bleeding' ) {
124 include( 'updates/propertyhive-update-2.1.php' );
125 update_option( 'propertyhive_db_version', '2.1.0' );
126 }*/
127
128 update_option( 'propertyhive_db_version', PH()->version );
129 }
130
131 /**
132 * Create cron jobs (clear them first)
133 */
134 private function create_cron_jobs() {
135
136 // Cron jobs
137 wp_clear_scheduled_hook( 'propertyhive_update_currency_exchange_rates' );
138 wp_clear_scheduled_hook( 'propertyhive_process_email_log' );
139 wp_clear_scheduled_hook( 'propertyhive_auto_email_match' );
140 wp_clear_scheduled_hook( 'propertyhive_check_licenses' );
141
142 $ve = get_option( 'gmt_offset' ) > 0 ? '+' : '-';
143
144 // Schedule for midnight as it's likely traffic will be quieter at that time
145 wp_schedule_event( strtotime( '00:00 tomorrow ' . $ve . get_option( 'gmt_offset' ) . ' HOURS' ), 'daily', 'propertyhive_update_currency_exchange_rates' );
146
147 wp_schedule_event( time(), 'every_fifteen_minutes', 'propertyhive_process_email_log' );
148
149 $auto_property_match_enabled = get_option( 'propertyhive_auto_property_match', '' );
150
151 if ( $auto_property_match_enabled == 'yes' )
152 {
153 wp_schedule_event( strtotime( '02:00 tomorrow ' . $ve . get_option( 'gmt_offset' ) . ' HOURS' ), 'daily', 'propertyhive_auto_email_match' );
154 }
155
156 // Schedule for 1am as it's likely traffic will be quieter at that time
157 // 1am so it doesn't run at exactly the same time as the exchange rate cron
158 wp_schedule_event( strtotime( '01:00 tomorrow ' . $ve . get_option( 'gmt_offset' ) . ' HOURS' ), 'daily', 'propertyhive_check_licenses' );
159 }
160
161 public function custom_cron_recurrence( $schedules )
162 {
163 $schedules['every_fifteen_minutes'] = array(
164 'interval' => 900,
165 'display' => __( 'Every 15 Minutes', 'textdomain' )
166 );
167
168 return $schedules;
169 }
170
171 /**
172 * Create pages that the plugin relies on
173 *
174 * @access public
175 * @return void
176 */
177 public static function create_pages() {
178
179 // Create page object
180 $my_post = array(
181 'post_title' => __( 'Property Search', 'propertyhive' ),
182 'post_content' => '',
183 'post_status' => 'publish',
184 'post_type' => 'page',
185 'comment_status' => 'closed',
186 'ping_status' => 'closed',
187 );
188
189 // Insert the post into the database
190 $page_id = wp_insert_post( $my_post );
191
192 update_option( 'propertyhive_search_results_page_id', $page_id );
193 }
194
195 /**
196 * Add the default terms for PH taxonomies - property types, tenures etc. Modify this at your own risk.
197 *
198 * @access public
199 * @return void
200 */
201 private function create_terms() {
202
203 $taxonomies = array(
204 'availability' => array(
205 array(
206 'name' => 'For Sale'
207 ),
208 array(
209 'name' => 'Under Offer'
210 ),
211 array(
212 'name' => 'Sold STC'
213 ),
214 array(
215 'name' => 'Sold'
216 ),
217 array(
218 'name' => 'To Let'
219 ),
220 array(
221 'name' => 'Let Agreed'
222 ),
223 array(
224 'name' => 'Let'
225 ),
226 ),
227 'property_type' => array(
228 // HOUSE
229 array(
230 'name' => 'House'
231 ),
232 array(
233 'name' => 'Detached House',
234 'child_of_previous' => true
235 ),
236 array(
237 'name' => 'Semi-Detached House',
238 'child_of_previous' => true
239 ),
240 array(
241 'name' => 'Terraced House',
242 'child_of_previous' => true
243 ),
244 array(
245 'name' => 'End of Terrace House',
246 'child_of_previous' => true
247 ),
248 array(
249 'name' => 'Mews',
250 'child_of_previous' => true
251 ),
252 array(
253 'name' => 'Link Detached House',
254 'child_of_previous' => true
255 ),
256 array(
257 'name' => 'Town House',
258 'child_of_previous' => true
259 ),
260 array(
261 'name' => 'Cottage',
262 'child_of_previous' => true
263 ),
264 // BUNGALOW
265 array(
266 'name' => 'Bungalow'
267 ),
268 array(
269 'name' => 'Detached Bungalow',
270 'child_of_previous' => true
271 ),
272 array(
273 'name' => 'Semi-Detached Bungalow',
274 'child_of_previous' => true
275 ),
276 array(
277 'name' => 'Terraced Bungalow',
278 'child_of_previous' => true
279 ),
280 array(
281 'name' => 'End of Terrace House',
282 'child_of_previous' => true
283 ),
284 // FLATS
285 array(
286 'name' => 'Flat / Apartment'
287 ),
288 array(
289 'name' => 'Flat',
290 'child_of_previous' => true
291 ),
292 array(
293 'name' => 'Apartment',
294 'child_of_previous' => true
295 ),
296 array(
297 'name' => 'Maisonette',
298 'child_of_previous' => true
299 ),
300 array(
301 'name' => 'Studio',
302 'child_of_previous' => true
303 ),
304 array(
305 'name' => 'Penthouse',
306 'child_of_previous' => true
307 ),
308 array(
309 'name' => 'Duplex',
310 'child_of_previous' => true
311 ),
312 array(
313 'name' => 'Triplex',
314 'child_of_previous' => true
315 ),
316 // OTHER
317 array(
318 'name' => 'Other'
319 ),
320 array(
321 'name' => 'Commercial',
322 'child_of_previous' => true
323 ),
324 array(
325 'name' => 'Land',
326 'child_of_previous' => true
327 ),
328 array(
329 'name' => 'Garage',
330 'child_of_previous' => true
331 ),
332 ),
333 'commercial_property_type' => array(
334 array(
335 'name' => 'Office'
336 ),
337 array(
338 'name' => 'Industrial'
339 ),
340 array(
341 'name' => 'Retail'
342 ),
343 array(
344 'name' => 'Land'
345 ),
346 array(
347 'name' => 'Health'
348 ),
349 array(
350 'name' => 'Motoring'
351 ),
352 array(
353 'name' => 'Leisure'
354 ),
355 array(
356 'name' => 'Investment'
357 ),
358 ),
359 'outside_space' => array(
360 array(
361 'name' => 'Balcony'
362 ),
363 array(
364 'name' => 'South Facing Garden'
365 ),
366 ),
367 'parking' => array(
368 array(
369 'name' => 'On Road Parking'
370 ),
371 array(
372 'name' => 'Off Road Parking'
373 ),
374 array(
375 'name' => 'Driveway'
376 ),
377 array(
378 'name' => 'Single Garage'
379 ),
380 array(
381 'name' => 'Double Garage'
382 ),
383 array(
384 'name' => 'Triple Garage'
385 ),
386 array(
387 'name' => 'Carport'
388 ),
389 ),
390 'price_qualifier' => array(
391 array(
392 'name' => 'Guide Price'
393 ),
394 array(
395 'name' => 'Fixed Price'
396 ),
397 array(
398 'name' => 'Offers Over'
399 ),
400 array(
401 'name' => 'OIRO'
402 )
403 ),
404 'sale_by' => array(
405 array(
406 'name' => 'Tender'
407 ),
408 array(
409 'name' => 'Private Treaty'
410 ),
411 array(
412 'name' => 'Auction'
413 ),
414 ),
415 'tenure' => array(
416 array(
417 'name' => 'Freehold'
418 ),
419 array(
420 'name' => 'Leasehold'
421 )
422 ),
423 'commercial_tenure' => array(
424 array(
425 'name' => 'Freehold'
426 ),
427 array(
428 'name' => 'Leasehold'
429 )
430 ),
431 'furnished' => array(
432 array(
433 'name' => 'Furnished'
434 ),
435 array(
436 'name' => 'Part Furnished'
437 ),
438 array(
439 'name' => 'Unfurnished'
440 )
441 ),
442 'marketing_flag' => array(
443 array(
444 'name' => 'New Instruction'
445 ),
446 array(
447 'name' => 'Chain Free'
448 )
449 ),
450 );
451
452 $previous_term_id = '';
453 foreach ( $taxonomies as $taxonomy => $terms )
454 {
455 // Make sure no terms for this taxonomy exist already
456 $existing_terms = get_terms( $taxonomy );
457 if ( is_wp_error( $existing_terms ) || (!is_wp_error( $existing_terms ) && empty( $existing_terms ) ) )
458 {
459 foreach ( $terms as $term )
460 {
461 if ( ! get_term_by( 'slug', sanitize_title( $term['name'] ), $taxonomy ) )
462 {
463 $args = array();
464 if ( isset($term['child_of_previous']) && $term['child_of_previous'] === true )
465 {
466 $args = array('parent' => $previous_term_id);
467 }
468 $return = wp_insert_term( $term['name'], $taxonomy, $args );
469 if ( !is_wp_error($return) )
470 {
471 if ( !isset($term['child_of_previous']) || (isset($term['child_of_previous']) && !$term['child_of_previous']) )
472 {
473 $previous_term_id = $return['term_id'];
474 }
475 }
476 else
477 {
478 // Hmm... an error occurred
479 // $error_string = $result->get_error_message(); // do something with this?
480 }
481 }
482 }
483 }
484 }
485 }
486
487 /**
488 * Add the first office so at least one exists
489 *
490 * @access public
491 * @return void
492 */
493 function create_primary_office() {
494
495 $args = array(
496 'post_type' => 'office'
497 );
498 $office_query = new WP_Query($args);
499 if (!$office_query->have_posts())
500 {
501 // Insert office
502 $office_post = array(
503 'post_title' => 'My Office',
504 'post_content' => '',
505 'post_status' => 'publish',
506 'post_type' => 'office',
507 );
508
509 // Insert the post into the database
510 $office_post_id = wp_insert_post( $office_post );
511
512 update_post_meta($office_post_id, 'primary', '1');
513 }
514 }
515
516 /**
517 * Default options
518 *
519 * Sets up the default options used on the settings page
520 *
521 * @access public
522 */
523 function create_options() {
524
525 add_option( 'propertyhive_active_departments_sales', 'yes', '', 'yes' );
526 add_option( 'propertyhive_active_departments_lettings', 'yes', '', 'yes' );
527 add_option( 'propertyhive_primary_department', 'residential-sales', '', 'yes' );
528
529 add_option( 'propertyhive_default_country', 'GB', '', 'yes' );
530 add_option( 'propertyhive_countries', array('GB'), '', 'yes' );
531
532 add_option( 'propertyhive_applicant_match_price_range_percentage_lower', 20, '', 'no' );
533 add_option( 'propertyhive_applicant_match_price_range_percentage_higher', 5, '', 'no' );
534
535 add_option( 'propertyhive_install_timestamp', time(), '', 'no' );
536 add_option( 'propertyhive_review_prompt_due_timestamp', strtotime('+30 days'), '', 'no' );
537
538 add_option( 'propertyhive_enquiry_auto_responder_email_subject', __( 'Thank you for your enquiry', 'propertyhive' ), '', 'no' );
539 add_option( 'propertyhive_enquiry_auto_responder_email_body', __( "Thank you for your recent property enquiry about [property_address_hyperlinked]. A member of our team will be in touch shortly.
540
541 Kind regards,
542
543 " . get_bloginfo('name') . "
544
545 [similar_properties]", 'propertyhive' ), '', 'no' );
546
547 add_option( 'propertyhive_property_match_default_email_subject', __( 'We found [property_count] that might be of interest to you', 'propertyhive' ), '', 'no' );
548 add_option( 'propertyhive_property_match_default_email_body', __( "Hi [contact_name],
549
550 Based on your requirements, we've found [property_count] below that we believe might be suitable for you:
551
552 [properties]
553
554 If you have any questions or require more information about any of the properties shown above please let me know.
555
556 Kind regards,
557
558 " . get_bloginfo('name'), 'propertyhive' ), '', 'no' );
559
560 add_option( 'propertyhive_viewing_applicant_booking_confirmation_email_subject', 'Your Viewing On [property_address]', '', 'no' );
561 add_option( 'propertyhive_viewing_applicant_booking_confirmation_email_body', "Dear [applicant_name],
562
563 This is confirmation that your viewing on [property_address] has been booked for [viewing_time] on [viewing_date].
564
565 Should you need to cancel or amend your booking please do not hesitate to contact us.
566
567 " . get_bloginfo('name'), '', 'no' );
568
569 add_option( 'propertyhive_viewing_owner_booking_confirmation_email_subject', 'Viewing Booked On [property_address]', '', 'no' );
570 add_option( 'propertyhive_viewing_owner_booking_confirmation_email_body', "Dear [owner_name],
571
572 This is confirmation that a viewing has been booked at your property, [property_address], for [viewing_time] on [viewing_date].
573
574 Should you need to cancel or amend this viewing please do not hesitate to contact us.
575
576 " . get_bloginfo('name'), '', 'no' );
577
578 }
579
580 /**
581 * Set up the database tables which the plugin needs to function.
582 *
583 * Tables:
584 * ph_email_log - Queueing table for emails
585 *
586 * @access public
587 * @return void
588 */
589 private function create_tables() {
590 global $wpdb, $propertyhive;
591
592 $wpdb->hide_errors();
593
594 $collate = '';
595
596 if ( $wpdb->has_cap( 'collation' ) ) {
597 $collate = $wpdb->get_charset_collate();
598 }
599
600 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
601
602 $tables = "
603 CREATE TABLE {$wpdb->prefix}ph_email_log (
604 email_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
605 contact_id bigint(20) unsigned NOT NULL,
606 property_ids text NOT NULL,
607 applicant_profile_id tinyint(3) unsigned NOT NULL,
608 to_email_address varchar(255) NOT NULL,
609 from_name varchar(255) NOT NULL,
610 from_email_address varchar(255) NOT NULL,
611 subject varchar(255) NOT NULL,
612 body longtext NOT NULL,
613 lock_id varchar(23) NOT NULL,
614 locked_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
615 status varchar(5) NOT NULL,
616 send_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
617 sent_by bigint(20) unsigned NOT NULL,
618 PRIMARY KEY (email_id)
619 ) $collate;";
620
621 dbDelta( $tables );
622 }
623
624 /**
625 * Create roles and capabilities
626 */
627 public function create_roles() {
628 global $wp_roles;
629
630 if ( ! class_exists( 'WP_Roles' ) ) {
631 return;
632 }
633
634 if ( ! isset( $wp_roles ) ) {
635 $wp_roles = new WP_Roles();
636 }
637
638 // Property Hive Contact role
639 add_role( 'property_hive_contact', __( 'Property Hive Contact', 'propertyhive' ), array(
640 'read' => true,
641 ) );
642
643 if ( is_object( $wp_roles ) ) {
644
645 $capabilities = $this->get_core_capabilities();
646
647 foreach ( $capabilities as $cap_group ) {
648 foreach ( $cap_group as $cap ) {
649 $wp_roles->add_cap( 'administrator', $cap );
650 $wp_roles->add_cap( 'editor', $cap );
651 }
652 }
653 }
654 }
655
656 /**
657 * Get capabilities for PropertyHive - these are assigned to admin/shop manager during installation or reset
658 *
659 * @access public
660 * @return array
661 */
662 public function get_core_capabilities() {
663 $capabilities = array();
664
665 $capabilities['core'] = array(
666 'manage_propertyhive'/*,
667 'view_propertyhive_reports'*/
668 );
669
670 return $capabilities;
671 }
672
673 /**
674 * propertyhive_remove_roles function.
675 *
676 * @access public
677 * @return void
678 */
679 public function remove_roles() {
680 /*global $wp_roles;
681
682 if ( class_exists( 'WP_Roles' ) ) {
683 if ( ! isset( $wp_roles ) ) {
684 $wp_roles = new WP_Roles();
685 }
686 }*/
687 }
688
689 /**
690 * Create files/directories
691 */
692 private function create_files() {
693 // Install files and folders for uploading files and prevent hotlinking
694 $upload_dir = wp_upload_dir();
695
696 /*$files = array(
697 array(
698 'base' => $upload_dir['basedir'] . '/propertyhive_uploads',
699 'file' => '.htaccess',
700 'content' => 'deny from all'
701 ),
702 array(
703 'base' => $upload_dir['basedir'] . '/propertyhive_uploads',
704 'file' => 'index.html',
705 'content' => ''
706 ),
707 array(
708 'base' => WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/logs',
709 'file' => '.htaccess',
710 'content' => 'deny from all'
711 ),
712 array(
713 'base' => WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/logs',
714 'file' => 'index.html',
715 'content' => ''
716 )
717 );
718
719 foreach ( $files as $file ) {
720 if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
721 if ( $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ) ) {
722 fwrite( $file_handle, $file['content'] );
723 fclose( $file_handle );
724 }
725 }
726 }*/
727 }
728
729 /**
730 * Active plugins pre update option filter
731 *
732 * @param string $new_value
733 * @return string
734 */
735 function pre_update_option_active_plugins( $new_value ) {
736 /*$old_value = (array) get_option( 'active_plugins' );
737
738 if ( $new_value !== $old_value && in_array( W3TC_FILE, (array) $new_value ) && in_array( W3TC_FILE, (array) $old_value ) ) {
739 $this->_config->set( 'notes.plugins_updated', true );
740 try {
741 $this->_config->save();
742 } catch( Exception $ex ) {}
743 }
744
745 return $new_value;*/
746 }
747
748 /**
749 * Show plugin changes. Code adapted from W3 Total Cache.
750 *
751 * @return void
752 */
753 function in_plugin_update_message( $args ) {
754 /*$transient_name = 'ph_upgrade_notice_' . $args['Version'];
755
756 if ( false === ( $upgrade_notice = get_transient( $transient_name ) ) ) {
757
758 $response = wp_remote_get( 'https://plugins.svn.wordpress.org/propertyhive/trunk/readme.txt' );
759
760 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
761
762 // Output Upgrade Notice
763 $matches = null;
764 $regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( PH_VERSION ) . '\s*=|$)~Uis';
765 $upgrade_notice = '';
766
767 if ( preg_match( $regexp, $response['body'], $matches ) ) {
768 $version = trim( $matches[1] );
769 $notices = (array) preg_split('~[\r\n]+~', trim( $matches[2] ) );
770
771 if ( version_compare( PH_VERSION, $version, '<' ) ) {
772
773 $upgrade_notice .= '<div class="ph_plugin_upgrade_notice">';
774
775 foreach ( $notices as $index => $line ) {
776 $upgrade_notice .= wp_kses_post( preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line ) );
777 }
778
779 $upgrade_notice .= '</div> ';
780 }
781 }
782
783 set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS );
784 }
785 }
786
787 echo wp_kses_post( $upgrade_notice );*/
788 }
789 }
790
791 endif;
792
793 return new PH_Install();
794