class-hfe-admin.php
268 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 | defined( 'ABSPATH' ) or exit; |
| 9 | |
| 10 | /** |
| 11 | * HFE_Admin setup |
| 12 | * |
| 13 | * @since 1.0 |
| 14 | */ |
| 15 | class HFE_Admin { |
| 16 | |
| 17 | /** |
| 18 | * Instance of HFE_Admin |
| 19 | * |
| 20 | * @var HFE_Admin |
| 21 | */ |
| 22 | private static $_instance = null; |
| 23 | |
| 24 | /** |
| 25 | * Instance of HFE_Admin |
| 26 | * |
| 27 | * @return HFE_Admin Instance of HFE_Admin |
| 28 | */ |
| 29 | public static function instance() { |
| 30 | if ( ! isset( self::$_instance ) ) { |
| 31 | self::$_instance = new self; |
| 32 | } |
| 33 | |
| 34 | return self::$_instance; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | */ |
| 40 | private function __construct() { |
| 41 | add_action( 'init', array( $this, 'header_footer_posttype' ) ); |
| 42 | add_action( 'admin_menu', array( $this, 'register_admin_menu' ), 50 ); |
| 43 | add_action( 'add_meta_boxes', array( $this, 'ehf_register_metabox' ) ); |
| 44 | add_action( 'save_post', array( $this, 'ehf_save_meta' ) ); |
| 45 | add_action( 'admin_notices', array( $this, 'location_notice' ) ); |
| 46 | add_action( 'template_redirect', array( $this, 'block_template_frontend' ) ); |
| 47 | add_filter( 'single_template', array( $this, 'load_canvas_template' ) ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Register Post type for header footer templates |
| 52 | */ |
| 53 | public function header_footer_posttype() { |
| 54 | |
| 55 | $labels = array( |
| 56 | 'name' => __( 'Header Footers Template', 'header-footer-elementor' ), |
| 57 | 'singular_name' => __( 'Elementor Header Footer', 'header-footer-elementor' ), |
| 58 | 'menu_name' => __( 'Header Footers Template', 'header-footer-elementor' ), |
| 59 | 'name_admin_bar' => __( 'Elementor Header Footer', 'header-footer-elementor' ), |
| 60 | 'add_new' => __( 'Add New', 'header-footer-elementor' ), |
| 61 | 'add_new_item' => __( 'Add New Header Footer', 'header-footer-elementor' ), |
| 62 | 'new_item' => __( 'New Header Footers Template', 'header-footer-elementor' ), |
| 63 | 'edit_item' => __( 'Edit Header Footers Template', 'header-footer-elementor' ), |
| 64 | 'view_item' => __( 'View Header Footers Template', 'header-footer-elementor' ), |
| 65 | 'all_items' => __( 'All Elementor Header Footers', 'header-footer-elementor' ), |
| 66 | 'search_items' => __( 'Search Header Footers Templates', 'header-footer-elementor' ), |
| 67 | 'parent_item_colon' => __( 'Parent Header Footers Templates:', 'header-footer-elementor' ), |
| 68 | 'not_found' => __( 'No Header Footers Templates found.', 'header-footer-elementor' ), |
| 69 | 'not_found_in_trash' => __( 'No Header Footers Templates found in Trash.', 'header-footer-elementor' ), |
| 70 | ); |
| 71 | |
| 72 | $args = array( |
| 73 | 'labels' => $labels, |
| 74 | 'public' => true, |
| 75 | 'rewrite' => false, |
| 76 | 'show_ui' => true, |
| 77 | 'show_in_menu' => false, |
| 78 | 'show_in_nav_menus' => false, |
| 79 | 'exclude_from_search' => true, |
| 80 | 'capability_type' => 'post', |
| 81 | 'hierarchical' => false, |
| 82 | 'menu_icon' => 'dashicons-editor-kitchensink', |
| 83 | 'supports' => array( 'title', 'thumbnail', 'elementor' ), |
| 84 | ); |
| 85 | |
| 86 | register_post_type( 'elementor-hf', $args ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Register the admin menu for Header Footer builder. |
| 91 | * |
| 92 | * @since 1.0.0 |
| 93 | * @since 1.0.1 |
| 94 | * Moved the menu under Appearance -> Header Footer Builder |
| 95 | */ |
| 96 | public function register_admin_menu() { |
| 97 | add_submenu_page( |
| 98 | 'themes.php', |
| 99 | __( 'Header Footer Builder', 'header-footer-elementor' ), |
| 100 | __( 'Header Footer Builder', 'header-footer-elementor' ), |
| 101 | 'edit_pages', |
| 102 | 'edit.php?post_type=elementor-hf' |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Register meta box(es). |
| 108 | */ |
| 109 | function ehf_register_metabox() { |
| 110 | add_meta_box( |
| 111 | 'ehf-meta-box', __( 'Elementor Header Footer options', 'header-footer-elementor' ), array( |
| 112 | $this, |
| 113 | 'efh_metabox_render', |
| 114 | ), 'elementor-hf', 'normal', 'high' |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Render Meta field. |
| 120 | * |
| 121 | * @param POST $post Currennt post object which is being displayed. |
| 122 | */ |
| 123 | function efh_metabox_render( $post ) { |
| 124 | $values = get_post_custom( $post->ID ); |
| 125 | $template_type = isset( $values['ehf_template_type'] ) ? esc_attr( $values['ehf_template_type'][0] ) : ''; |
| 126 | $display_on_canvas = isset( $values['display-on-canvas-template'] ) ? true : false; |
| 127 | |
| 128 | // We'll use this nonce field later on when saving. |
| 129 | wp_nonce_field( 'ehf_meta_nounce', 'ehf_meta_nounce' ); |
| 130 | ?> |
| 131 | <p> |
| 132 | <label for="ehf_template_type"><?php _e( 'Select the type of template this is', 'header-footer-elementor' ); ?></label> |
| 133 | <select name="ehf_template_type" id="ehf_template_type"> |
| 134 | <option value="" <?php selected( $template_type, '' ); ?>><?php _e( 'Select Option', 'header-footer-elementor' ); ?></option> |
| 135 | <option value="type_header" <?php selected( $template_type, 'type_header' ); ?>><?php _e( 'Header', 'header-footer-elementor' ); ?></option> |
| 136 | <option value="type_footer" <?php selected( $template_type, 'type_footer' ); ?>><?php _e( 'Footer', 'header-footer-elementor' ); ?></option> |
| 137 | </select> |
| 138 | </p> |
| 139 | <p> |
| 140 | <label for="display-on-canvas-template"> |
| 141 | <input type="checkbox" id="display-on-canvas-template" name="display-on-canvas-template" value="1" <?php checked( $display_on_canvas, true ); ?> /> |
| 142 | <?php _e( 'Display Layout on Elementor Canvas Template?', 'header-footer-elementor' ); ?> |
| 143 | </label> |
| 144 | </p> |
| 145 | <p class="description"><?php _e( 'Enabling this option will display this layout on pages using Elementor Canvas Template.', 'header-footer-elementor' ); ?></p> |
| 146 | <?php |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Save meta field. |
| 151 | * |
| 152 | * @param POST $post_id Currennt post object which is being displayed. |
| 153 | * |
| 154 | * @return Void |
| 155 | */ |
| 156 | public function ehf_save_meta( $post_id ) { |
| 157 | |
| 158 | // Bail if we're doing an auto save. |
| 159 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | // if our nonce isn't there, or we can't verify it, bail. |
| 164 | if ( ! isset( $_POST['ehf_meta_nounce'] ) || ! wp_verify_nonce( $_POST['ehf_meta_nounce'], 'ehf_meta_nounce' ) ) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // if our current user can't edit this post, bail. |
| 169 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if ( isset( $_POST['ehf_template_type'] ) ) { |
| 174 | update_post_meta( $post_id, 'ehf_template_type', esc_attr( $_POST['ehf_template_type'] ) ); |
| 175 | } |
| 176 | |
| 177 | if ( isset( $_POST['display-on-canvas-template'] ) ) { |
| 178 | update_post_meta( $post_id, 'display-on-canvas-template', esc_attr( $_POST['display-on-canvas-template'] ) ); |
| 179 | } else { |
| 180 | delete_post_meta( $post_id, 'display-on-canvas-template' ); |
| 181 | } |
| 182 | |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Display notice when editing the header or footer when there is one more of similar layout is active on the site. |
| 187 | * |
| 188 | * @since 1.0.0 |
| 189 | */ |
| 190 | public function location_notice() { |
| 191 | |
| 192 | global $pagenow; |
| 193 | global $post; |
| 194 | |
| 195 | if ( 'post.php' != $pagenow || ! is_object( $post ) || 'elementor-hf' != $post->post_type ) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | $template_type = get_post_meta( $post->ID, 'ehf_template_type', true ); |
| 200 | |
| 201 | if ( '' !== $template_type ) { |
| 202 | $templates = Header_Footer_Elementor::get_template_id( $template_type ); |
| 203 | |
| 204 | // Check if more than one template is selected for current template type. |
| 205 | if ( is_array( $templates ) && isset( $templates[1] ) && $post->ID != $templates[0] ) { |
| 206 | |
| 207 | $post_title = '<strong>' . get_the_title( $templates[0] ) . '</strong>'; |
| 208 | $template_location = '<strong>' . $this->template_location( $template_type ) . '</strong>'; |
| 209 | /* Translators: Post title, Template Location */ |
| 210 | $message = sprintf( __( 'Template %1$s is already assigned to the location %2$s', 'header-footer-elementor' ), $post_title, $template_location ); |
| 211 | |
| 212 | echo '<div class="error"><p>'; |
| 213 | echo $message; |
| 214 | echo '</p></div>'; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Convert the Template name to be added in the notice. |
| 222 | * |
| 223 | * @since 1.0.0 |
| 224 | * |
| 225 | * @param String $template_type Template type name. |
| 226 | * |
| 227 | * @return String $template_type Template type name. |
| 228 | */ |
| 229 | public function template_location( $template_type ) { |
| 230 | $template_type = ucfirst( str_replace( 'type_', '', $template_type ) ); |
| 231 | |
| 232 | return $template_type; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Don't display the elementor header footer templates on the frontend for non edit_posts capable users. |
| 237 | * |
| 238 | * @since 1.0.0 |
| 239 | */ |
| 240 | public function block_template_frontend() { |
| 241 | if ( is_singular( 'elementor-hf' ) && ! current_user_can( 'edit_posts' ) ) { |
| 242 | wp_redirect( site_url(), 301 ); |
| 243 | die; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Single template function which will choose our template |
| 249 | * |
| 250 | * @since 1.0.1 |
| 251 | * |
| 252 | * @param String $single_template Single template. |
| 253 | */ |
| 254 | function load_canvas_template( $single_template ) { |
| 255 | |
| 256 | global $post; |
| 257 | |
| 258 | if ( 'elementor-hf' == $post->post_type ) { |
| 259 | return ELEMENTOR_PATH . '/includes/page-templates/canvas.php'; |
| 260 | } |
| 261 | |
| 262 | return $single_template; |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | |
| 267 | HFE_Admin::instance(); |
| 268 |