assets
3 weeks ago
theme-hooks
4 years ago
views
1 year ago
activator.php
2 years ago
cpt-api.php
4 years ago
cpt-hooks.php
2 years ago
cpt.php
1 year ago
init.php
3 weeks ago
activator.php
199 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Modules\Header_Footer; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | class Activator { |
| 7 | public static $instance = null; |
| 8 | |
| 9 | protected $templates; |
| 10 | public $header_template; |
| 11 | public $footer_template; |
| 12 | |
| 13 | protected $current_theme; |
| 14 | protected $current_template; |
| 15 | |
| 16 | protected $post_type = 'elementskit_template'; |
| 17 | |
| 18 | public function __construct() { |
| 19 | add_action( 'wp', array( $this, 'hooks' ) ); |
| 20 | } |
| 21 | |
| 22 | public function hooks() { |
| 23 | $this->current_template = basename( get_page_template_slug() ); |
| 24 | if ( $this->current_template == 'elementor_canvas' ) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | $this->current_theme = get_template(); |
| 29 | |
| 30 | switch ( $this->current_theme ) { |
| 31 | case 'astra': |
| 32 | new Theme_Hooks\Astra( self::template_ids() ); |
| 33 | break; |
| 34 | |
| 35 | case 'neve': |
| 36 | new Theme_Hooks\Neve( self::template_ids() ); |
| 37 | break; |
| 38 | |
| 39 | case 'generatepress': |
| 40 | case 'generatepress-child': |
| 41 | new Theme_Hooks\Generatepress( self::template_ids() ); |
| 42 | break; |
| 43 | |
| 44 | case 'oceanwp': |
| 45 | case 'oceanwp-child': |
| 46 | new Theme_Hooks\Oceanwp( self::template_ids() ); |
| 47 | break; |
| 48 | |
| 49 | case 'bb-theme': |
| 50 | case 'bb-theme-child': |
| 51 | new Theme_Hooks\Bbtheme( self::template_ids() ); |
| 52 | break; |
| 53 | |
| 54 | case 'genesis': |
| 55 | case 'genesis-child': |
| 56 | new Theme_Hooks\Genesis( self::template_ids() ); |
| 57 | break; |
| 58 | |
| 59 | case 'twentynineteen': |
| 60 | new Theme_Hooks\TwentyNineteen( self::template_ids() ); |
| 61 | break; |
| 62 | |
| 63 | case 'my-listing': |
| 64 | case 'my-listing-child': |
| 65 | new Theme_Hooks\MyListing( self::template_ids() ); |
| 66 | break; |
| 67 | |
| 68 | default: |
| 69 | new Theme_Hooks\Theme_Support( self::template_ids() ); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public static function template_ids() { |
| 75 | $cached = wp_cache_get( 'elementskit_template_ids' ); |
| 76 | if ( false !== $cached ) { |
| 77 | return $cached; |
| 78 | } |
| 79 | |
| 80 | $instance = self::instance(); |
| 81 | $instance->the_filter(); |
| 82 | |
| 83 | $ids = array( |
| 84 | $instance->header_template, |
| 85 | $instance->footer_template, |
| 86 | ); |
| 87 | |
| 88 | if ( $instance->header_template != null ) { |
| 89 | \ElementsKit_Lite\Utils::render_elementor_content_css( $instance->header_template ); |
| 90 | } |
| 91 | |
| 92 | if ( $instance->footer_template != null ) { |
| 93 | \ElementsKit_Lite\Utils::render_elementor_content_css( $instance->footer_template ); |
| 94 | } |
| 95 | |
| 96 | wp_cache_set( 'elementskit_template_ids', $ids ); |
| 97 | return $ids; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | protected function the_filter() { |
| 102 | $arg = array( |
| 103 | 'posts_per_page' => -1, |
| 104 | 'orderby' => 'id', |
| 105 | 'order' => 'DESC', |
| 106 | 'post_status' => 'publish', |
| 107 | 'post_type' => $this->post_type, |
| 108 | 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 109 | array( |
| 110 | 'key' => 'elementskit_template_activation', |
| 111 | 'value' => 'yes', |
| 112 | 'compare' => '=', |
| 113 | ), |
| 114 | ), |
| 115 | ); |
| 116 | $this->templates = get_posts( $arg ); |
| 117 | |
| 118 | // more conditions can be triggered at once |
| 119 | // don't use switch case |
| 120 | // may impliment and callable by dynamic class in future |
| 121 | |
| 122 | // entire site |
| 123 | if ( ! is_admin() ) { |
| 124 | $filters = array( |
| 125 | array( |
| 126 | 'key' => 'condition_a', |
| 127 | 'value' => 'entire_site', |
| 128 | ), |
| 129 | ); |
| 130 | $this->get_header_footer( $filters ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | protected function get_header_footer( $filters ) { |
| 135 | $template_id = array(); |
| 136 | |
| 137 | if ( $this->templates != null ) { |
| 138 | foreach ( $this->templates as $template ) { |
| 139 | $template = $this->get_full_data( $template ); |
| 140 | $match_found = true; |
| 141 | |
| 142 | // WPML Language Check |
| 143 | if ( defined( 'ICL_LANGUAGE_CODE' ) ) : |
| 144 | $current_lang = apply_filters( 'wpml_post_language_details', null, $template['ID'] ); |
| 145 | |
| 146 | if ( ! empty( $current_lang ) && ! $current_lang['different_language'] && ( $current_lang['language_code'] == ICL_LANGUAGE_CODE ) ) : |
| 147 | $template_id[ $template['type'] ] = $template['ID']; |
| 148 | endif; |
| 149 | endif; |
| 150 | |
| 151 | foreach ( $filters as $filter ) { |
| 152 | if ( $filter['key'] == 'condition_singular_id' ) { |
| 153 | $ids = explode( ',', $template[ $filter['key'] ] ); |
| 154 | if ( ! in_array( $filter['value'], $ids ) ) { |
| 155 | $match_found = false; |
| 156 | } |
| 157 | } elseif ( $template[ $filter['key'] ] != $filter['value'] ) { |
| 158 | $match_found = false; |
| 159 | } |
| 160 | if ( $filter['key'] == 'condition_a' && $template[ $filter['key'] ] == 'singular' && count( $filters ) < 2 ) { |
| 161 | $match_found = false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ( $match_found == true ) { |
| 166 | if ( $template['type'] == 'header' ) { |
| 167 | $this->header_template = isset( $template_id['header'] ) ? $template_id['header'] : $template['ID']; |
| 168 | } |
| 169 | if ( $template['type'] == 'footer' ) { |
| 170 | $this->footer_template = isset( $template_id['footer'] ) ? $template_id['footer'] : $template['ID']; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | protected function get_full_data( $post ) { |
| 178 | if ( $post != null ) { |
| 179 | return array_merge( |
| 180 | (array) $post, |
| 181 | array( |
| 182 | 'type' => get_post_meta( $post->ID, 'elementskit_template_type', true ), |
| 183 | 'condition_a' => get_post_meta( $post->ID, 'elementskit_template_condition_a', true ), |
| 184 | 'condition_singular' => get_post_meta( $post->ID, 'elementskit_template_condition_singular', true ), |
| 185 | 'condition_singular_id' => get_post_meta( $post->ID, 'elementskit_template_condition_singular_id', true ), |
| 186 | ) |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public static function instance() { |
| 192 | if ( is_null( self::$instance ) ) { |
| 193 | self::$instance = new self(); |
| 194 | } |
| 195 | |
| 196 | return self::$instance; |
| 197 | } |
| 198 | } |
| 199 |