Layers_Custom_Meta

Post & Page Meta Initiation File.

Defined (1)

The class is defined in the following location(s).

/core/meta/init.php  
  1. class Layers_Custom_Meta { 
  2.  
  3. private static $instance; 
  4.  
  5. var $custom_meta; 
  6.  
  7. /** 
  8. * Initiator 
  9. */ 
  10.  
  11. public static function get_instance() { 
  12. if ( ! isset( self::$instance ) ) { 
  13. self::$instance = new Layers_Custom_Meta(); 
  14. return self::$instance; 
  15.  
  16. /** 
  17. * Constructor 
  18. */ 
  19.  
  20. public function __construct() { 
  21.  
  22. // Setup some folder variables 
  23. $meta_dir = '/core/meta/'; 
  24.  
  25. // Include Config file(s) 
  26. require_once get_template_directory() . $meta_dir . 'config.php'; 
  27.  
  28.  
  29. // Instantiate meta config class 
  30. $meta_config = new Layers_Meta_Config(); 
  31.  
  32. // Get post meta 
  33. $this->custom_meta = $meta_config->meta_data(); 
  34.  
  35.  
  36. public function init() { 
  37.  
  38. // Enqueue Styles 
  39. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) , 50 ); 
  40. add_action( 'admin_enqueue_scripts', array( $this, 'admin_print_styles' ) , 50 ); 
  41.  
  42. // Page Builder Button 
  43. add_action( 'edit_form_after_title', array( $this , 'page_builder_button' ) ); 
  44. add_action( 'wp_ajax_update_page_builder_meta' , array( $this , 'update_page_builder_meta' ) ); 
  45. // add_filter( 'layers_pointer_settings' , array( $this , 'page_builder_button_pointer' ) ); 
  46. add_action( 'page_row_actions' , array( $this , 'inline_page_builder_button' ), 10, 2 ); 
  47.  
  48. // Custom Fields 
  49. add_action( 'admin_menu', array( $this , 'register_post_meta' ) ); 
  50. add_action( 'save_post', array( $this , 'save_post_meta' ) ); 
  51. add_action( 'publish_post', array( $this , 'save_post_meta' ) ); 
  52.  
  53. /** 
  54. * Enqueue Widget Scripts 
  55. */ 
  56.  
  57. public function admin_enqueue_scripts() { 
  58.  
  59. // Customizer general 
  60. wp_enqueue_script( 
  61. LAYERS_THEME_SLUG . '-admin-meta' ,  
  62. get_template_directory_uri() . '/core/meta/js/meta.js' ,  
  63. array( 
  64. 'backbone',  
  65. 'jquery',  
  66. 'wp-color-picker' 
  67. ),  
  68. LAYERS_VERSION,  
  69. true 
  70. ); 
  71.  
  72. // Localize Scripts 
  73. wp_localize_script( LAYERS_THEME_SLUG . '-admin-meta' , "layers_meta_params", array( 'ajaxurl' => admin_url( "admin-ajax.php" ) , 'nonce' => wp_create_nonce( 'layers-customizer-actions' ) ) ); 
  74.  
  75. /** 
  76. * Enqueue Widget Styles 
  77. */ 
  78.  
  79. public function admin_print_styles() { 
  80. global $pagenow, $post; 
  81. if ( 'post.php' === $pagenow && ( LAYERS_BUILDER_TEMPLATE == basename( get_page_template() ) ) ) : ?> 
  82. <style> #postdivrich { display: none; }</style> 
  83. <?php endif; 
  84.  
  85. /** 
  86. * Page Builder Button 
  87. */ 
  88.  
  89. public function page_builder_button() { 
  90. global $post; 
  91.  
  92. // This button is only used for pages 
  93. if ( !in_array( $post->post_type, array( 'page' ) ) ) return; 
  94.  
  95. // Check if we're using the builder for this page 
  96.  
  97. $is_builder_used = ( 'builder.php' == basename( get_page_template() ) ) ? true : false; ?> 
  98.  
  99. <div id="layers_toggle_builder" class=" <?php echo ( true == $is_builder_used ? '' : 'l_admin-hide' ) ?>"> 
  100. <div class="postbox l_admin-push-top"> 
  101. <div class="l_admin-section-title l_admin-no-push-bottom l_admin-content"> 
  102. <div class="l_admin-heading"> 
  103. <?php ( 'auto-draft' == $post->post_status ? _e( 'Your page is almost ready.' , 'layerswp' ) : _e( 'Your page is ready.' , 'layerswp' ) ); ?> 
  104. </div> 
  105. <p class="l_admin-excerpt"> 
  106. <?php ( 'auto-draft' == $post->post_status ? _e( 'Click the Start button below to set this page up for Layers.' , 'layerswp' ) : _e( 'You can drag and drop widgets, edit content and tweak the design. Click the button below to see your page come to life.' , 'layerswp' ) ); ?> 
  107. </p> 
  108. </div> 
  109. <div class="l_admin-button-well clearfix"> 
  110. <a href="<?php echo admin_url() . 'customize.php?url=' . esc_url( get_permalink() ); ?>" class="button btn-massive btn-primary btn-full <?php echo ( 'auto-draft' == $post->post_status ? 'disable' : '' ); ?>" id="<?php echo ( isset( $post->ID ) ? 'builder-button-' . $post->ID : 'builder-button-' . rand(0, 1) ); ?>"> 
  111. <?php ( 'auto-draft' == $post->post_status ? _e( 'Start' , 'layerswp' ) : _e( 'Edit Your Page' , 'layerswp' ) ); ?> 
  112. </a> 
  113. </div> 
  114. </div> 
  115.  
  116. <div class="l_admin-row"> 
  117.  
  118. <div class="l_admin-column l_admin-span-4 postbox l_admin-content"> 
  119. <div class="l_admin-section-title l_admin-tiny"> 
  120. <h4 class="l_admin-heading"><?php _e( 'Export Layout', 'layerswp' ); ?></h4> 
  121. <p class="l_admin-excerpt"><?php _e( 'Export your layout to a <code>.json</code> file which you can use to upload to another site.', 'layerswp' ); ?></p> 
  122. </div> 
  123. <a href="?post=<?php echo get_the_ID(); ?>&action=edit&layers-export=1" class="button"><?php _e( 'Export', 'layerswp' ); ?></a> 
  124. </div> 
  125.  
  126. <div class="l_admin-column l_admin-span-4 postbox l_admin-content"> 
  127. <div class="l_admin-section-title l_admin-tiny"> 
  128. <h4 class="l_admin-heading"><?php _e( 'Import Layout', 'layerswp' ); ?></h4> 
  129. <p class="l_admin-excerpt"><?php _e( 'Upload a layout file (eg. <code>' . $post->post_name . '.json</code>) by clicking the button below.', 'layerswp' ); ?></p> 
  130. </div> 
  131. <button class="button" id="layers-page-import-button" data-post-id="<?php echo get_the_ID(); ?>" data-title="Upload .json" data-button_text="Upload & Import"><?php _e( 'Upload & Import', 'layerswp' ); ?></button> 
  132. </div> 
  133.  
  134. <div class="l_admin-column l_admin-span-4 postbox l_admin-content"> 
  135. <div class="l_admin-section-title l_admin-tiny"> 
  136. <h4 class="l_admin-heading"><?php _e( 'Duplicate Layout', 'layerswp' ); ?></h4> 
  137. <p class="l_admin-excerpt"><?php _e( 'Easily duplicate your layout, settings, text and images in order to get started quickly with a new page.', 'layerswp' ); ?></p> 
  138. </div> 
  139. <button href="" class="button" id="layers-page-duplicate-button" data-post-id="<?php echo get_the_ID(); ?>"><?php _e( 'Duplicate', 'layerswp' ); ?></button> 
  140. </div> 
  141.  
  142. </div> 
  143. </div> 
  144. <?php } 
  145.  
  146. /** 
  147. * Page Builder Inline Button 
  148. */ 
  149.  
  150. function inline_page_builder_button($actions, $post) { 
  151.  
  152. // Set the post object 
  153. $post_type_object = get_post_type_object( $post->post_type ); 
  154.  
  155. // Set user capability 
  156. $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); 
  157.  
  158. // Add our button 
  159. if ( $can_edit_post && 'builder.php' == get_page_template_slug( $post->ID ) ) { 
  160. $actions['builder'] = '<a href="' . admin_url( 'customize.php?url=' . esc_url( get_permalink() ) ) . '" title="' . esc_attr( __( 'Edit Layout' , 'layerswp' ) ) . '">' . __( 'Edit Layout' , 'layerswp' ) . '</a>'; 
  161.  
  162. return $actions; 
  163.  
  164. /** 
  165. * Page Builder Button Pointer 
  166. */ 
  167. public function page_builder_button_pointer( $pointers ) { 
  168. global $post; 
  169.  
  170. // If we are not in the post edit screen, just return 
  171. if( !isset( $post ) ) return; 
  172.  
  173. // This button is only used for pages 
  174. if ( !in_array( $post->post_type, array( 'page' ) ) || 'publish' != get_post_status() ) return; 
  175.  
  176. // Add the pointer to the pointer config 
  177. $pointers[ LAYERS_THEME_SLUG . '-builder-button-pointer-' . $post->ID ] = array( 
  178. 'selector' => '#builder-button-' . $post->ID,  
  179. 'position' => array( 
  180. 'edge' => 'right', // bottom / top/ right / left 
  181. 'align' => 'left' // left / center / right 
  182. ),  
  183. 'title' => __( 'Build Your Page' , 'layerswp' ),  
  184. 'content' => __( 'Use the' . LAYERS_THEME_TITLE . ' page builder to build a beautiful, dynamic page.' , 'layerswp' ),  
  185. ); 
  186.  
  187. return $pointers; 
  188.  
  189. /** 
  190. * Page Builder Meta Update 
  191. */ 
  192.  
  193. public function update_page_builder_meta() { 
  194.  
  195. // Get the Post ID 
  196. $post_id = $_POST['id']; 
  197.  
  198. if( isset($_POST[ 'template' ] ) && 'builder.php' == $_POST[ 'template' ] ) { 
  199. update_post_meta( $post_id , '_wp_page_template', $_POST[ 'template' ] ); 
  200. } else { 
  201. delete_post_meta( $post_id , '_wp_page_template' ); 
  202. die(); 
  203.  
  204. /** 
  205. * Custom Meta Register 
  206. */ 
  207.  
  208. public function register_post_meta() { 
  209. // If we have not published the post, don't set a post ID 
  210. if( isset( $_REQUEST[ 'post' ] ) ) { 
  211. $post_id = $_REQUEST[ 'post' ]; 
  212. } else { 
  213. $post_id = NULL; 
  214.  
  215. // Loop over the custom meta 
  216. foreach( $this->custom_meta as $meta_index => $custom_meta ) { 
  217.  
  218. // If there is Post Meta, register the metabox 
  219. if( isset( $this->custom_meta[ $meta_index ] ) ) { 
  220.  
  221. if( post_type_exists( $meta_index ) ) { 
  222. /** 
  223. * Add post meta for posts & other post types 
  224. */ 
  225.  
  226. // Set the post type 
  227. $post_type = $meta_index; 
  228.  
  229. $callback_args = array( 
  230. 'meta_index' =>$meta_index 
  231. ); 
  232. } else { 
  233. /** 
  234. * Add post meta for page templates 
  235. */ 
  236.  
  237. // Set the post type to 'page' 
  238. $post_type = 'page'; 
  239.  
  240. // Get the page template 
  241. $page_template = get_post_meta( $post_id, '_wp_page_template' , true ); 
  242.  
  243. // If there is no page template set, just return 
  244. if( '' == $page_template ) return; 
  245.  
  246. // Now check to see that we've selected the right page template 
  247. if( $meta_index != $page_template) return; 
  248.  
  249. $callback_args = array( 
  250. 'meta_index' => $meta_index 
  251. ); 
  252.  
  253. // Add Meta Box 
  254. add_meta_box( 
  255. LAYERS_THEME_SLUG . '-' . $meta_index, // Slug 
  256. $custom_meta[ 'title' ], // Title 
  257. array( $this , 'display_post_meta' ) , // Interface 
  258. $post_type , // Post Type 
  259. $custom_meta[ 'position' ], // Position 
  260. 'high', // Priority 
  261. $callback_args // Callback args 
  262. ); 
  263.  
  264. /** 
  265. * Custom Meta Interface 
  266. */ 
  267.  
  268. public function display_post_meta( $post , $callback_args ) { 
  269.  
  270. // Get post type 
  271. $post_type = get_post_type( $post->ID ); 
  272.  
  273. // Post Meta Value 
  274. $post_meta = get_post_meta( $post->ID, LAYERS_THEME_SLUG, true ); 
  275.  
  276. // Set the meta index ie. the array we will loop over for our options 
  277. $meta_index = $callback_args[ 'args' ][ 'meta_index' ]; 
  278.  
  279. // If there is no post meta to show, return 
  280. if( !isset( $this->custom_meta[ $meta_index ] ) ) return; 
  281.  
  282. // Instantiate form elements 
  283. $form_elements = new Layers_Form_Elements(); 
  284.  
  285. // If there is Post Meta, loop over the tabs. 
  286. if( isset( $this->custom_meta[ $meta_index ] ) ) { ?> 
  287. <!-- Tabs --> 
  288. <div class="l_admin-nav l_admin-nav-tabs"> 
  289. <ul class="l_admin-tabs clearfix"> 
  290. <?php foreach( $this->custom_meta[ $meta_index ]['custom-meta'] as $key => $meta_option ) { ?> 
  291. <li class="<?php if( ! isset( $inactive ) ) echo 'active'; ?> <?php echo esc_attr( 'l_admin-tab-' . sanitize_title( $key ) ); ?>" ><a href="#"><?php echo $meta_option[ 'title' ]; ?></a></li> 
  292. <?php $inactive=1; ?> 
  293. <?php } // foreach $this->custom_meta[ $post_type ]['custom-meta'] ?> 
  294. </ul> 
  295. </div> 
  296. <!-- Tab Content --> 
  297. <div class="l_admin-tab-content"> 
  298. <?php foreach( $this->custom_meta[ $meta_index ]['custom-meta'] as $key => $meta_option ) { ?> 
  299. <section class="l_admin-accordion-section l_admin-content l_admin-tab-content <?php echo esc_attr( 'l_admin-tab-content-' . sanitize_title( $key ) ); ?> <?php if( isset( $hide_tab ) ) echo 'l_admin-hide'; ?> customize-control"> <?php // @TODO: Remove .customizer-control class ?> 
  300. <div class="l_admin-row clearfix"> 
  301. <?php if( isset( $meta_option[ 'elements' ] ) ) { ?> 
  302. <fieldset class="layers-post-meta"> 
  303. <?php foreach( $meta_option[ 'elements' ] as $input_key => $input ) { 
  304. $data = ''; 
  305. if( isset( $input[ 'data' ] ) ) { 
  306.  
  307. if( isset( $input[ 'data' ][ 'show-if-value' ] ) ) { 
  308. $data .= ' data-show-if-selector="' . esc_attr( $input[ 'data' ][ 'show-if-selector' ] ) . '" '; 
  309.  
  310. if( isset( $input[ 'data' ][ 'show-if-value' ] ) ) { 
  311. $data .= ' data-show-if-value="' . esc_attr( $input[ 'data' ][ 'show-if-value' ] ) . '" '; 
  312.  
  313. if( isset( $input[ 'data' ][ 'show-if-operator' ] ) ) { 
  314. $data .= ' data-show-if-operator="' . esc_attr( $input[ 'data' ][ 'show-if-operator' ] ) . '" '; 
  315. } ?> 
  316. <p class="l_admin-form-item" <?php echo $data; ?>> 
  317. <label><?php echo $input[ 'label' ]; ?></label> 
  318. <?php echo $form_elements->input( 
  319. array( 
  320. 'type' => $input[ 'type' ],  
  321. 'name' => LAYERS_THEME_SLUG . '[' . $input_key . ']',  
  322. 'id' => $input_key ,  
  323. 'default' => ( isset( $input[ 'default' ] ) ) ? $input[ 'default' ] : NULL ,  
  324. 'placeholder' => ( isset( $input[ 'placeholder' ] ) ) ? $input[ 'placeholder' ] : NULL ,  
  325. 'value' => ( isset( $post_meta[ $input_key ] ) ) ? $post_meta[ $input_key ] : ( ( isset( $input[ 'default' ] ) ) ? $input[ 'default' ] : NULL ), // Check for a value, then check for a default, then finally settle on NULL 
  326. 'options' => ( isset( $input[ 'options' ] ) ) ? $input[ 'options' ] : NULL,  
  327. 'class' => 'layers-' . $input[ 'type' ] 
  328.  
  329. ); ?> 
  330. </p> 
  331. <?php } // foreach $meta_option[ 'elements' ] ?> 
  332. </fieldset> 
  333. <?php } // if $meta_option[ 'elements' ] ?> 
  334. </div> 
  335. </section> 
  336. <?php $hide_tab = 1; ?> 
  337. <?php } // foreach $this->custom_meta[ $post_type ]['custom-meta'] ?> 
  338. </div> 
  339. <?php wp_nonce_field( LAYERS_THEME_SLUG . '-post-meta' , '_wp_nonce_' . LAYERS_THEME_SLUG ); ?> 
  340. <?php } // if $this->custom_meta[ $post_type ] ?> 
  341. <?php } 
  342.  
  343. /** 
  344. * Custom Meta Interface 
  345. */ 
  346.  
  347. public function save_post_meta( $post_id ) { 
  348. global $post; 
  349.  
  350. // Get post type 
  351. $post_type = get_post_type( $post_id ); 
  352.  
  353. // Verify our nonce 
  354. $nonce_key = '_wp_nonce_' . LAYERS_THEME_SLUG; 
  355.  
  356. // If there is no nonce to use, can this function 
  357. if( !isset( $_REQUEST[ $nonce_key ] ) ) return; 
  358.  
  359. $nonce = $_REQUEST[ $nonce_key ]; 
  360.  
  361. // Form key 
  362. $form_key = LAYERS_THEME_SLUG; 
  363.  
  364. // Do some nonce 
  365. if ( wp_verify_nonce( $nonce, LAYERS_THEME_SLUG . '-post-meta' ) ) { 
  366. if( isset( $_REQUEST[ $form_key ] ) ) { 
  367. update_post_meta( $post_id, LAYERS_THEME_SLUG, $_REQUEST[ $form_key ] ); 
  368. } // if isset( $this->custom_meta[ $post_type ] ) 
  369. } // if nonce