assets
1 year ago
bsf-analytics
1 year ago
class-hfe-addons-actions.php
1 year ago
class-hfe-admin.php
1 year ago
class-hfe-admin.php
677 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Entry point for the plugin. Checks if Elementor is installed and activated and loads it's own files and actions. |
| 4 | * |
| 5 | * @package header-footer-elementor |
| 6 | */ |
| 7 | |
| 8 | use HFE\Lib\Astra_Target_Rules_Fields; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * HFE_Admin setup |
| 14 | * |
| 15 | * @since 1.0 |
| 16 | */ |
| 17 | class HFE_Admin { |
| 18 | |
| 19 | /** |
| 20 | * Instance of HFE_Admin |
| 21 | * |
| 22 | * @var HFE_Admin |
| 23 | */ |
| 24 | private static $_instance = null; |
| 25 | |
| 26 | /** |
| 27 | * Instance of HFE_Admin |
| 28 | * |
| 29 | * @return HFE_Admin Instance of HFE_Admin |
| 30 | */ |
| 31 | public static function instance() { |
| 32 | if ( ! isset( self::$_instance ) ) { |
| 33 | self::$_instance = new self(); |
| 34 | } |
| 35 | |
| 36 | add_action( 'elementor/init', __CLASS__ . '::load_admin', 0 ); |
| 37 | |
| 38 | return self::$_instance; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Load the icons style in editor. |
| 43 | * |
| 44 | * @since 1.3.0 |
| 45 | * @access public |
| 46 | * @return void |
| 47 | */ |
| 48 | public static function load_admin() { |
| 49 | add_action( 'elementor/editor/after_enqueue_styles', __CLASS__ . '::hfe_admin_enqueue_scripts' ); |
| 50 | add_action( 'admin_head', __CLASS__ . '::hfe_admin_enqueue_scripts' ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Enqueue admin scripts |
| 55 | * |
| 56 | * @since 1.3.0 |
| 57 | * @param string $hook Current page hook. |
| 58 | * @access public |
| 59 | * @return void |
| 60 | */ |
| 61 | public static function hfe_admin_enqueue_scripts( $hook ) { |
| 62 | |
| 63 | // Register the icons styles. |
| 64 | wp_register_style( |
| 65 | 'hfe-style', |
| 66 | HFE_URL . 'assets/css/style.css', |
| 67 | [], |
| 68 | HFE_VER |
| 69 | ); |
| 70 | |
| 71 | wp_enqueue_style( 'hfe-style' ); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Constructor |
| 78 | * |
| 79 | * @return void |
| 80 | */ |
| 81 | private function __construct() { |
| 82 | add_action( 'init', [ $this, 'header_footer_posttype' ] ); |
| 83 | if ( is_admin() && current_user_can( 'manage_options' ) ) { |
| 84 | add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 50 ); |
| 85 | } |
| 86 | add_action( 'add_meta_boxes', [ $this, 'ehf_register_metabox' ] ); |
| 87 | add_action( 'save_post', [ $this, 'ehf_save_meta' ] ); |
| 88 | add_action( 'admin_notices', [ $this, 'location_notice' ] ); |
| 89 | add_action( 'template_redirect', [ $this, 'block_template_frontend' ] ); |
| 90 | add_filter( 'single_template', [ $this, 'load_canvas_template' ] ); |
| 91 | add_filter( 'manage_elementor-hf_posts_columns', [ $this, 'set_shortcode_columns' ] ); |
| 92 | add_action( 'manage_elementor-hf_posts_custom_column', [ $this, 'render_shortcode_column' ], 10, 2 ); |
| 93 | if ( defined( 'ELEMENTOR_PRO_VERSION' ) && ELEMENTOR_PRO_VERSION > 2.8 ) { |
| 94 | add_action( 'elementor/editor/footer', [ $this, 'register_hfe_epro_script' ], 99 ); |
| 95 | } |
| 96 | |
| 97 | add_action( 'admin_notices', [ $this, 'hide_admin_notices' ], 1 ); |
| 98 | add_action( 'all_admin_notices', [ $this, 'hide_admin_notices' ], 1 ); |
| 99 | |
| 100 | if ( is_admin() ) { |
| 101 | add_action( 'manage_elementor-hf_posts_custom_column', [ $this, 'column_content' ], 10, 2 ); |
| 102 | add_filter( 'manage_elementor-hf_posts_columns', [ $this, 'column_headings' ] ); |
| 103 | require_once HFE_DIR . 'admin/class-hfe-addons-actions.php'; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Hide admin notices on the custom settings page. |
| 110 | * |
| 111 | * @since x.x.x |
| 112 | * @return void |
| 113 | */ |
| 114 | public static function hide_admin_notices() { |
| 115 | $screen = get_current_screen(); |
| 116 | $pages_to_hide_notices = array( |
| 117 | 'edit-elementor-hf', // Edit screen for elementor-hf post type |
| 118 | 'elementor-hf', // New post screen for elementor-hf post type |
| 119 | ); |
| 120 | |
| 121 | if ( in_array( $screen->id, $pages_to_hide_notices ) || 'toplevel_page_hfe' === $screen->id ) { |
| 122 | remove_all_actions( 'admin_notices' ); |
| 123 | remove_all_actions( 'all_admin_notices' ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Script for Elementor Pro full site editing support. |
| 129 | * |
| 130 | * @since 1.4.0 |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function register_hfe_epro_script() { |
| 135 | $ids_array = [ |
| 136 | [ |
| 137 | 'id' => get_hfe_header_id(), |
| 138 | 'value' => 'Header', |
| 139 | ], |
| 140 | [ |
| 141 | 'id' => get_hfe_footer_id(), |
| 142 | 'value' => 'Footer', |
| 143 | ], |
| 144 | [ |
| 145 | 'id' => hfe_get_before_footer_id(), |
| 146 | 'value' => 'Before Footer', |
| 147 | ], |
| 148 | ]; |
| 149 | |
| 150 | wp_enqueue_script( 'hfe-elementor-pro-compatibility', HFE_URL . 'inc/js/hfe-elementor-pro-compatibility.js', [ 'jquery' ], HFE_VER, true ); |
| 151 | |
| 152 | wp_localize_script( |
| 153 | 'hfe-elementor-pro-compatibility', |
| 154 | 'hfe_admin', |
| 155 | [ |
| 156 | 'ids_array' => wp_json_encode( $ids_array ), |
| 157 | ] |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Adds or removes list table column headings. |
| 163 | * |
| 164 | * @param array $columns Array of columns. |
| 165 | * @return array |
| 166 | */ |
| 167 | public function column_headings( $columns ) { |
| 168 | unset( $columns['date'] ); |
| 169 | |
| 170 | $columns['elementor_hf_display_rules'] = __( 'Display Rules', 'header-footer-elementor' ); |
| 171 | $columns['date'] = __( 'Date', 'header-footer-elementor' ); |
| 172 | |
| 173 | return $columns; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Adds the custom list table column content. |
| 178 | * |
| 179 | * @since 1.2.0 |
| 180 | * @param array $column Name of column. |
| 181 | * @param int $post_id Post id. |
| 182 | * @return void |
| 183 | */ |
| 184 | public function column_content( $column, $post_id ) { |
| 185 | |
| 186 | if ( 'elementor_hf_display_rules' === $column ) { |
| 187 | |
| 188 | $locations = get_post_meta( $post_id, 'ehf_target_include_locations', true ); |
| 189 | if ( ! empty( $locations ) ) { |
| 190 | echo '<div class="ast-advanced-headers-location-wrap" style="margin-bottom: 5px;">'; |
| 191 | echo '<strong>Display: </strong>'; |
| 192 | $this->column_display_location_rules( $locations ); |
| 193 | echo '</div>'; |
| 194 | } |
| 195 | |
| 196 | $locations = get_post_meta( $post_id, 'ehf_target_exclude_locations', true ); |
| 197 | if ( ! empty( $locations ) ) { |
| 198 | echo '<div class="ast-advanced-headers-exclusion-wrap" style="margin-bottom: 5px;">'; |
| 199 | echo '<strong>Exclusion: </strong>'; |
| 200 | $this->column_display_location_rules( $locations ); |
| 201 | echo '</div>'; |
| 202 | } |
| 203 | |
| 204 | $users = get_post_meta( $post_id, 'ehf_target_user_roles', true ); |
| 205 | if ( isset( $users ) && is_array( $users ) ) { |
| 206 | if ( isset( $users[0] ) && ! empty( $users[0] ) ) { |
| 207 | $user_label = []; |
| 208 | foreach ( $users as $user ) { |
| 209 | $user_label[] = Astra_Target_Rules_Fields::get_user_by_key( $user ); |
| 210 | } |
| 211 | echo '<div class="ast-advanced-headers-users-wrap">'; |
| 212 | echo '<strong>Users: </strong>'; |
| 213 | echo esc_html( join( ', ', $user_label ) ); |
| 214 | echo '</div>'; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get Markup of Location rules for Display rule column. |
| 222 | * |
| 223 | * @param array $locations Array of locations. |
| 224 | * @return void |
| 225 | */ |
| 226 | public function column_display_location_rules( $locations ) { |
| 227 | |
| 228 | $location_label = []; |
| 229 | if ( is_array( $locations ) && is_array( $locations['rule'] ) && isset( $locations['rule'] ) ) { |
| 230 | $index = array_search( 'specifics', $locations['rule'] ); |
| 231 | if ( false !== $index && ! empty( $index ) ) { |
| 232 | unset( $locations['rule'][ $index ] ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if ( isset( $locations['rule'] ) && is_array( $locations['rule'] ) ) { |
| 237 | foreach ( $locations['rule'] as $location ) { |
| 238 | $location_label[] = Astra_Target_Rules_Fields::get_location_by_key( $location ); |
| 239 | } |
| 240 | } |
| 241 | if ( isset( $locations['specific'] ) && is_array( $locations['specific'] ) ) { |
| 242 | foreach ( $locations['specific'] as $location ) { |
| 243 | $location_label[] = Astra_Target_Rules_Fields::get_location_by_key( $location ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | echo esc_html( join( ', ', $location_label ) ); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * Register Post type for Elementor Header & Footer Builder templates |
| 253 | * |
| 254 | * @return void |
| 255 | */ |
| 256 | public function header_footer_posttype() { |
| 257 | if ( ! current_user_can( 'manage_options' ) ) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | $setting_location = $this->is_pro_active() ? 'uaepro' : 'hfe'; |
| 262 | |
| 263 | $labels = [ |
| 264 | 'name' => esc_html__( 'Elementor Header & Footer Builder', 'header-footer-elementor' ), |
| 265 | 'singular_name' => esc_html__( 'Elementor Header & Footer Builder', 'header-footer-elementor' ), |
| 266 | 'menu_name' => esc_html__( 'Elementor Header & Footer Builder', 'header-footer-elementor' ), |
| 267 | 'name_admin_bar' => esc_html__( 'Elementor Header & Footer Builder', 'header-footer-elementor' ), |
| 268 | 'add_new' => esc_html__( 'Add New', 'header-footer-elementor' ), |
| 269 | 'add_new_item' => esc_html__( 'Add New', 'header-footer-elementor' ), |
| 270 | 'new_item' => esc_html__( 'New Template', 'header-footer-elementor' ), |
| 271 | 'edit_item' => esc_html__( 'Edit Template', 'header-footer-elementor' ), |
| 272 | 'view_item' => esc_html__( 'View Template', 'header-footer-elementor' ), |
| 273 | 'all_items' => esc_html__( 'View All', 'header-footer-elementor' ), |
| 274 | 'search_items' => esc_html__( 'Search Templates', 'header-footer-elementor' ), |
| 275 | 'parent_item_colon' => esc_html__( 'Parent Templates:', 'header-footer-elementor' ), |
| 276 | 'not_found' => esc_html__( 'No Templates found.', 'header-footer-elementor' ), |
| 277 | 'not_found_in_trash' => esc_html__( 'No Templates found in Trash.', 'header-footer-elementor' ), |
| 278 | ]; |
| 279 | |
| 280 | $args = [ |
| 281 | 'labels' => $labels, |
| 282 | 'public' => true, |
| 283 | 'show_ui' => true, |
| 284 | 'show_in_menu' => false, |
| 285 | 'show_in_nav_menus' => false, |
| 286 | 'exclude_from_search' => true, |
| 287 | 'capability_type' => 'post', |
| 288 | 'hierarchical' => false, |
| 289 | 'menu_icon' => 'dashicons-editor-kitchensink', |
| 290 | 'supports' => [ 'title', 'thumbnail', 'elementor' ], |
| 291 | 'menu_position' => 5, |
| 292 | ]; |
| 293 | |
| 294 | register_post_type( 'elementor-hf', $args ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Register the admin menu for Elementor Header & Footer Builder. |
| 299 | * |
| 300 | * @since 1.0.0 |
| 301 | * @since 1.0.1 |
| 302 | * Moved the menu under Appearance -> Elementor Header & Footer Builder |
| 303 | * @return void |
| 304 | */ |
| 305 | public function register_admin_menu() { |
| 306 | |
| 307 | $setting_location = $this->is_pro_active() ? 'uaepro' : 'hfe'; |
| 308 | |
| 309 | add_submenu_page( |
| 310 | $setting_location, |
| 311 | __( 'Create New', 'header-footer-elementor' ), |
| 312 | __( 'Create New', 'header-footer-elementor' ), |
| 313 | 'edit_pages', |
| 314 | 'post-new.php?post_type=elementor-hf', |
| 315 | '', |
| 316 | 1 |
| 317 | ); |
| 318 | |
| 319 | add_submenu_page( |
| 320 | $setting_location, |
| 321 | __( 'Header/Footer Builder', 'header-footer-elementor' ), |
| 322 | __( 'Header & Footer Builder', 'header-footer-elementor' ), |
| 323 | 'edit_pages', |
| 324 | 'edit.php?post_type=elementor-hf', |
| 325 | '', |
| 326 | 2 |
| 327 | ); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /** |
| 332 | * Check if UAE Pro is active. |
| 333 | * |
| 334 | * @return bool True if UAE Pro is active, false otherwise. |
| 335 | */ |
| 336 | public function is_pro_active() { |
| 337 | if ( is_plugin_active( 'ultimate-elementor/ultimate-elementor.php' ) && defined( 'UAEL_PRO' ) && UAEL_PRO ) { |
| 338 | return true; |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Register meta box(es). |
| 345 | * |
| 346 | * @return void |
| 347 | */ |
| 348 | public function ehf_register_metabox() { |
| 349 | add_meta_box( |
| 350 | 'ehf-meta-box', |
| 351 | __( 'Elementor Header & Footer Builder Options', 'header-footer-elementor' ), |
| 352 | [ |
| 353 | $this, |
| 354 | 'efh_metabox_render', |
| 355 | ], |
| 356 | 'elementor-hf', |
| 357 | 'normal', |
| 358 | 'high' |
| 359 | ); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Render Meta field. |
| 364 | * |
| 365 | * @param array $post Currennt post object which is being displayed. |
| 366 | * @return void |
| 367 | */ |
| 368 | public function efh_metabox_render( $post ) { |
| 369 | $values = get_post_custom( $post->ID ); |
| 370 | $template_type = isset( $values['ehf_template_type'] ) ? esc_attr( sanitize_text_field( $values['ehf_template_type'][0] ) ) : ''; |
| 371 | $display_on_canvas = isset( $values['display-on-canvas-template'] ) ? true : false; |
| 372 | |
| 373 | // We'll use this nonce field later on when saving. |
| 374 | wp_nonce_field( 'ehf_meta_nounce', 'ehf_meta_nounce' ); |
| 375 | ?> |
| 376 | <table class="hfe-options-table widefat"> |
| 377 | <tbody> |
| 378 | <tr class="hfe-options-row type-of-template"> |
| 379 | <td class="hfe-options-row-heading"> |
| 380 | <label for="ehf_template_type"><?php esc_html_e( 'Type of Template', 'header-footer-elementor' ); ?></label> |
| 381 | </td> |
| 382 | <td class="hfe-options-row-content"> |
| 383 | <select name="ehf_template_type" id="ehf_template_type"> |
| 384 | <option value="" <?php selected( $template_type, '' ); ?>><?php esc_html_e( 'Select Option', 'header-footer-elementor' ); ?></option> |
| 385 | <option value="type_header" <?php selected( $template_type, 'type_header' ); ?>><?php esc_html_e( 'Header', 'header-footer-elementor' ); ?></option> |
| 386 | <option value="type_before_footer" <?php selected( $template_type, 'type_before_footer' ); ?>><?php esc_html_e( 'Before Footer', 'header-footer-elementor' ); ?></option> |
| 387 | <option value="type_footer" <?php selected( $template_type, 'type_footer' ); ?>><?php esc_html_e( 'Footer', 'header-footer-elementor' ); ?></option> |
| 388 | <option value="custom" <?php selected( $template_type, 'custom' ); ?>><?php esc_html_e( 'Custom Block', 'header-footer-elementor' ); ?></option> |
| 389 | </select> |
| 390 | </td> |
| 391 | </tr> |
| 392 | |
| 393 | <?php $this->display_rules_tab(); ?> |
| 394 | <tr class="hfe-options-row hfe-shortcode"> |
| 395 | <td class="hfe-options-row-heading"> |
| 396 | <label for="ehf_template_type"><?php esc_html_e( 'Shortcode', 'header-footer-elementor' ); ?></label> |
| 397 | <i class="hfe-options-row-heading-help dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Copy this shortcode and paste it into your post, page, or text widget content.', 'header-footer-elementor' ); ?>"> |
| 398 | </i> |
| 399 | </td> |
| 400 | <td class="hfe-options-row-content"> |
| 401 | <span class="hfe-shortcode-col-wrap"> |
| 402 | <input type="text" onfocus="this.select();" readonly="readonly" value="[hfe_template id='<?php echo esc_attr( $post->ID ); ?>']" class="hfe-large-text code"> |
| 403 | </span> |
| 404 | </td> |
| 405 | </tr> |
| 406 | <tr class="hfe-options-row enable-for-canvas"> |
| 407 | <td class="hfe-options-row-heading"> |
| 408 | <label for="display-on-canvas-template"> |
| 409 | <?php esc_html_e( 'Enable Layout for Elementor Canvas Template?', 'header-footer-elementor' ); ?> |
| 410 | </label> |
| 411 | <i class="hfe-options-row-heading-help dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Enabling this option will display this layout on pages using Elementor Canvas Template.', 'header-footer-elementor' ); ?>"></i> |
| 412 | </td> |
| 413 | <td class="hfe-options-row-content"> |
| 414 | <input type="checkbox" id="display-on-canvas-template" name="display-on-canvas-template" value="1" <?php checked( $display_on_canvas, true ); ?> /> |
| 415 | </td> |
| 416 | </tr> |
| 417 | </tbody> |
| 418 | </table> |
| 419 | <?php |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Markup for Display Rules Tabs. |
| 424 | * |
| 425 | * @since 1.0.0 |
| 426 | * @return void |
| 427 | */ |
| 428 | public function display_rules_tab() { |
| 429 | // Load Target Rule assets. |
| 430 | Astra_Target_Rules_Fields::get_instance()->admin_styles(); |
| 431 | |
| 432 | $include_locations = get_post_meta( get_the_id(), 'ehf_target_include_locations', true ); |
| 433 | $exclude_locations = get_post_meta( get_the_id(), 'ehf_target_exclude_locations', true ); |
| 434 | $users = get_post_meta( get_the_id(), 'ehf_target_user_roles', true ); |
| 435 | ?> |
| 436 | <tr class="bsf-target-rules-row hfe-options-row"> |
| 437 | <td class="bsf-target-rules-row-heading hfe-options-row-heading"> |
| 438 | <label><?php esc_html_e( 'Display On', 'header-footer-elementor' ); ?></label> |
| 439 | <i class="bsf-target-rules-heading-help dashicons dashicons-editor-help" |
| 440 | title="<?php echo esc_attr__( 'Add locations for where this template should appear.', 'header-footer-elementor' ); ?>"></i> |
| 441 | </td> |
| 442 | <td class="bsf-target-rules-row-content hfe-options-row-content"> |
| 443 | <?php |
| 444 | Astra_Target_Rules_Fields::target_rule_settings_field( |
| 445 | 'bsf-target-rules-location', |
| 446 | [ |
| 447 | 'title' => __( 'Display Rules', 'header-footer-elementor' ), |
| 448 | 'value' => '[{"type":"basic-global","specific":null}]', |
| 449 | 'tags' => 'site,enable,target,pages', |
| 450 | 'rule_type' => 'display', |
| 451 | 'add_rule_label' => __( 'Add Display Rule', 'header-footer-elementor' ), |
| 452 | ], |
| 453 | $include_locations |
| 454 | ); |
| 455 | ?> |
| 456 | </td> |
| 457 | </tr> |
| 458 | <tr class="bsf-target-rules-row hfe-options-row"> |
| 459 | <td class="bsf-target-rules-row-heading hfe-options-row-heading"> |
| 460 | <label><?php esc_html_e( 'Do Not Display On', 'header-footer-elementor' ); ?></label> |
| 461 | <i class="bsf-target-rules-heading-help dashicons dashicons-editor-help" |
| 462 | title="<?php echo esc_attr__( 'Add locations for where this template should not appear.', 'header-footer-elementor' ); ?>"></i> |
| 463 | </td> |
| 464 | <td class="bsf-target-rules-row-content hfe-options-row-content"> |
| 465 | <?php |
| 466 | Astra_Target_Rules_Fields::target_rule_settings_field( |
| 467 | 'bsf-target-rules-exclusion', |
| 468 | [ |
| 469 | 'title' => __( 'Exclude On', 'header-footer-elementor' ), |
| 470 | 'value' => '[]', |
| 471 | 'tags' => 'site,enable,target,pages', |
| 472 | 'add_rule_label' => __( 'Add Exclusion Rule', 'header-footer-elementor' ), |
| 473 | 'rule_type' => 'exclude', |
| 474 | ], |
| 475 | $exclude_locations |
| 476 | ); |
| 477 | ?> |
| 478 | </td> |
| 479 | </tr> |
| 480 | <tr class="bsf-target-rules-row hfe-options-row"> |
| 481 | <td class="bsf-target-rules-row-heading hfe-options-row-heading"> |
| 482 | <label><?php esc_html_e( 'User Roles', 'header-footer-elementor' ); ?></label> |
| 483 | <i class="bsf-target-rules-heading-help dashicons dashicons-editor-help" title="<?php echo esc_attr__( 'Display custom template based on user role.', 'header-footer-elementor' ); ?>"></i> |
| 484 | </td> |
| 485 | <td class="bsf-target-rules-row-content hfe-options-row-content"> |
| 486 | <?php |
| 487 | Astra_Target_Rules_Fields::target_user_role_settings_field( |
| 488 | 'bsf-target-rules-users', |
| 489 | [ |
| 490 | 'title' => __( 'Users', 'header-footer-elementor' ), |
| 491 | 'value' => '[]', |
| 492 | 'tags' => 'site,enable,target,pages', |
| 493 | 'add_rule_label' => __( 'Add User Rule', 'header-footer-elementor' ), |
| 494 | ], |
| 495 | $users |
| 496 | ); |
| 497 | ?> |
| 498 | </td> |
| 499 | </tr> |
| 500 | <?php |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Save meta field. |
| 505 | * |
| 506 | * @param POST $post_id Currennt post object which is being displayed. |
| 507 | * |
| 508 | * @return Void |
| 509 | */ |
| 510 | public function ehf_save_meta( $post_id ) { |
| 511 | |
| 512 | // Bail if we're doing an auto save. |
| 513 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | // if our nonce isn't there, or we can't verify it, bail. |
| 518 | if ( ! isset( $_POST['ehf_meta_nounce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ehf_meta_nounce'] ) ), 'ehf_meta_nounce' ) ) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | // if our current user can't edit this post, bail. |
| 523 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | $target_locations = Astra_Target_Rules_Fields::get_format_rule_value( $_POST, 'bsf-target-rules-location' ); |
| 528 | $target_exclusion = Astra_Target_Rules_Fields::get_format_rule_value( $_POST, 'bsf-target-rules-exclusion' ); |
| 529 | $target_users = []; |
| 530 | |
| 531 | if ( isset( $_POST['bsf-target-rules-users'] ) ) { |
| 532 | $target_users = array_map( 'sanitize_text_field', wp_unslash( $_POST['bsf-target-rules-users'] ) ); |
| 533 | } |
| 534 | |
| 535 | update_post_meta( $post_id, 'ehf_target_include_locations', $target_locations ); |
| 536 | update_post_meta( $post_id, 'ehf_target_exclude_locations', $target_exclusion ); |
| 537 | update_post_meta( $post_id, 'ehf_target_user_roles', $target_users ); |
| 538 | |
| 539 | if ( isset( $_POST['ehf_template_type'] ) ) { |
| 540 | update_post_meta( $post_id, 'ehf_template_type', sanitize_text_field( wp_unslash( $_POST['ehf_template_type'] ) ) ); |
| 541 | } |
| 542 | |
| 543 | if ( isset( $_POST['display-on-canvas-template'] ) ) { |
| 544 | update_post_meta( $post_id, 'display-on-canvas-template', sanitize_text_field( wp_unslash( $_POST['display-on-canvas-template'] ) ) ); |
| 545 | } else { |
| 546 | delete_post_meta( $post_id, 'display-on-canvas-template' ); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Display notice when editing the header or footer when there is one more of similar layout is active on the site. |
| 552 | * |
| 553 | * @since 1.0.0 |
| 554 | * @return void |
| 555 | */ |
| 556 | public function location_notice() { |
| 557 | global $pagenow; |
| 558 | global $post; |
| 559 | |
| 560 | if ( 'post.php' != $pagenow || ! is_object( $post ) || 'elementor-hf' != $post->post_type ) { |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | $template_type = get_post_meta( $post->ID, 'ehf_template_type', true ); |
| 565 | |
| 566 | if ( '' !== $template_type ) { |
| 567 | $templates = Header_Footer_Elementor::get_template_id( $template_type ); |
| 568 | |
| 569 | // Check if more than one template is selected for current template type. |
| 570 | if ( is_array( $templates ) && isset( $templates[1] ) && $post->ID != $templates[0] ) { |
| 571 | $post_title = '<strong>' . esc_html( get_the_title( $templates[0] ) ) . '</strong>'; |
| 572 | $template_location = '<strong>' . esc_html( $this->template_location( $template_type ) ) . '</strong>'; |
| 573 | /* Translators: Post title, Template Location */ |
| 574 | $message = sprintf( __( 'Template %1$s is already assigned to the location %2$s', 'header-footer-elementor' ), $post_title, $template_location ); |
| 575 | |
| 576 | echo '<div class="error"><p>'; |
| 577 | echo esc_html( $message ); |
| 578 | echo '</p></div>'; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Convert the Template name to be added in the notice. |
| 585 | * |
| 586 | * @since 1.0.0 |
| 587 | * |
| 588 | * @param string $template_type Template type name. |
| 589 | * |
| 590 | * @return string $template_type Template type name. |
| 591 | */ |
| 592 | public function template_location( $template_type ) { |
| 593 | $template_type = ucfirst( str_replace( 'type_', '', $template_type ) ); |
| 594 | |
| 595 | return $template_type; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Don't display the elementor Elementor Header & Footer Builder templates on the frontend for non edit_posts capable users. |
| 600 | * |
| 601 | * @since 1.0.0 |
| 602 | * @return void |
| 603 | */ |
| 604 | public function block_template_frontend() { |
| 605 | if ( is_singular( 'elementor-hf' ) && ! current_user_can( 'edit_posts' ) ) { |
| 606 | wp_redirect( site_url(), 301 ); |
| 607 | die; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Single template function which will choose our template |
| 613 | * |
| 614 | * @since 1.0.1 |
| 615 | * |
| 616 | * @param string $single_template Single template. |
| 617 | * @return string |
| 618 | */ |
| 619 | public function load_canvas_template( $single_template ) { |
| 620 | global $post; |
| 621 | |
| 622 | if ( 'elementor-hf' == $post->post_type ) { |
| 623 | $elementor_2_0_canvas = ELEMENTOR_PATH . '/modules/page-templates/templates/canvas.php'; |
| 624 | |
| 625 | if ( file_exists( $elementor_2_0_canvas ) ) { |
| 626 | return $elementor_2_0_canvas; |
| 627 | } else { |
| 628 | return ELEMENTOR_PATH . '/includes/page-templates/canvas.php'; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return $single_template; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Set shortcode column for template list. |
| 637 | * |
| 638 | * @param array $columns template list columns. |
| 639 | * @return array |
| 640 | */ |
| 641 | public function set_shortcode_columns( $columns ) { |
| 642 | $date_column = $columns['date']; |
| 643 | |
| 644 | unset( $columns['date'] ); |
| 645 | |
| 646 | $columns['shortcode'] = __( 'Shortcode', 'header-footer-elementor' ); |
| 647 | $columns['date'] = $date_column; |
| 648 | |
| 649 | return $columns; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Display shortcode in template list column. |
| 654 | * |
| 655 | * @param array $column template list column. |
| 656 | * @param int $post_id post id. |
| 657 | * @return void |
| 658 | */ |
| 659 | public function render_shortcode_column( $column, $post_id ) { |
| 660 | switch ( $column ) { |
| 661 | case 'shortcode': |
| 662 | ob_start(); |
| 663 | ?> |
| 664 | <span class="hfe-shortcode-col-wrap"> |
| 665 | <input type="text" onfocus="this.select();" readonly="readonly" value="[hfe_template id='<?php echo esc_attr( $post_id ); ?>']" class="hfe-large-text code"> |
| 666 | </span> |
| 667 | |
| 668 | <?php |
| 669 | |
| 670 | ob_get_contents(); |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | HFE_Admin::instance(); |
| 677 |