compatibility
1 year ago
js
3 years ago
lib
1 year ago
widgets-css
2 years ago
widgets-manager
1 year ago
class-header-footer-elementor.php
1 year ago
class-hfe-elementor-canvas-compat.php
1 year ago
class-hfe-settings-page.php
1 year ago
class-hfe-update.php
1 year ago
hfe-functions.php
1 year ago
class-hfe-update.php
177 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Theme Update |
| 4 | * |
| 5 | * @package Header Footer Elementor |
| 6 | * @author Nikhil Chavan <email@nikhilchavan.com> |
| 7 | * @copyright Copyright (c) 2019, Header Footer Elementor |
| 8 | * @link https://github.com/Nikschavan/header-footer-elementor/ |
| 9 | * @since HFE 1.1.4 |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | |
| 16 | if ( ! class_exists( 'HFE_Update' ) ) { |
| 17 | |
| 18 | /** |
| 19 | * HFE_Update initial setup |
| 20 | * |
| 21 | * @since 1.1.4 |
| 22 | */ |
| 23 | class HFE_Update { |
| 24 | |
| 25 | /** |
| 26 | * Option key for stored version number. |
| 27 | * |
| 28 | * @since 1.1.4 |
| 29 | * @var string |
| 30 | */ |
| 31 | // phpcs:ignore |
| 32 | private string $db_option_key = '_hfe_db_version'; |
| 33 | |
| 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * @since 1.1.4 |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | |
| 41 | // Theme Updates. |
| 42 | if ( is_admin() ) { |
| 43 | add_action( 'admin_init', [ $this, 'init' ], 5 ); |
| 44 | } else { |
| 45 | add_action( 'wp', [ $this, 'init' ], 5 ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Implement theme update logic. |
| 51 | * |
| 52 | * @since 1.1.4 |
| 53 | * @return void |
| 54 | */ |
| 55 | public function init() { |
| 56 | do_action( 'hfe_update_before' ); |
| 57 | |
| 58 | if ( ! $this->needs_db_update() ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $db_version = get_option( $this->db_option_key, false ); |
| 63 | |
| 64 | if ( version_compare( $db_version, '1.2.0-beta.2', '<' ) ) { |
| 65 | $this->setup_default_terget_rules(); |
| 66 | } |
| 67 | |
| 68 | // flush rewrite rules on plugin update. |
| 69 | flush_rewrite_rules(); // PHPCS:Ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules |
| 70 | |
| 71 | $this->update_db_version(); |
| 72 | |
| 73 | do_action( 'hfe_update_after' ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set default target rules for header, footer, before footers being used before target rules were added to the plugin. |
| 78 | * |
| 79 | * @since 1.2.0-beta.1 |
| 80 | * @return void |
| 81 | */ |
| 82 | private function setup_default_terget_rules() { |
| 83 | $default_include_locations = [ |
| 84 | 'rule' => [ 0 => 'basic-global' ], |
| 85 | 'specific' => [], |
| 86 | ]; |
| 87 | |
| 88 | $header_id = $this->get_legacy_template_id( 'type_header' ); |
| 89 | $footer_id = $this->get_legacy_template_id( 'type_footer' ); |
| 90 | $before_footer_id = $this->get_legacy_template_id( 'type_before_footer' ); |
| 91 | |
| 92 | // Header. |
| 93 | if ( ! empty( $header_id ) ) { |
| 94 | update_post_meta( $header_id, 'ehf_target_include_locations', $default_include_locations ); |
| 95 | } |
| 96 | |
| 97 | // Footer. |
| 98 | if ( ! empty( $footer_id ) ) { |
| 99 | update_post_meta( $footer_id, 'ehf_target_include_locations', $default_include_locations ); |
| 100 | } |
| 101 | |
| 102 | // Before Footer. |
| 103 | if ( ! empty( $before_footer_id ) ) { |
| 104 | update_post_meta( $before_footer_id, 'ehf_target_include_locations', $default_include_locations ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get header or footer template id based on the meta query. |
| 110 | * |
| 111 | * @param string $type Type of the template header/footer. |
| 112 | * |
| 113 | * @return mixed Returns the header or footer template id if found, else returns string ''. |
| 114 | */ |
| 115 | public function get_legacy_template_id( $type ) { |
| 116 | $args = [ |
| 117 | 'post_type' => 'elementor-hf', |
| 118 | 'meta_key' => 'ehf_template_type', |
| 119 | 'meta_value' => $type, // PHPCS:Ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 120 | 'meta_type' => 'post', |
| 121 | 'meta_compare' => '>=', |
| 122 | 'orderby' => 'meta_value', |
| 123 | 'order' => 'ASC', |
| 124 | 'meta_query' => [ // PHPCS:Ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 125 | 'relation' => 'OR', |
| 126 | [ |
| 127 | 'key' => 'ehf_template_type', |
| 128 | 'value' => $type, |
| 129 | 'compare' => '==', |
| 130 | 'type' => 'post', |
| 131 | ], |
| 132 | ], |
| 133 | ]; |
| 134 | |
| 135 | $args = apply_filters( 'hfe_get_template_id_args', $args ); |
| 136 | $template = new WP_Query( |
| 137 | $args |
| 138 | ); |
| 139 | |
| 140 | if ( $template->have_posts() ) { |
| 141 | $posts = wp_list_pluck( $template->posts, 'ID' ); |
| 142 | return $posts[0]; |
| 143 | } |
| 144 | |
| 145 | return ''; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Check if db upgrade is required. |
| 150 | * |
| 151 | * @since 1.1.4 |
| 152 | * @return bool True if stored database version is lower than constant; false if otherwise. |
| 153 | */ |
| 154 | private function needs_db_update() { |
| 155 | $db_version = get_option( $this->db_option_key, false ); |
| 156 | |
| 157 | if ( false === $db_version || version_compare( $db_version, HFE_VER ) ) { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Update DB version. |
| 166 | * |
| 167 | * @since 1.1.4 |
| 168 | * @return void |
| 169 | */ |
| 170 | private function update_db_version() { |
| 171 | update_option( $this->db_option_key, HFE_VER ); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | new HFE_Update(); |
| 177 |