PluginProbe
Property Hive / 1.4.6
Property Hive v1.4.6
2.3.1 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 All 261 releases
propertyhive / includes / class-ph-install.php

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

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