layouts = (array) apply_filters( 'primer_layouts', array( 'one-column-wide' => esc_html__( 'One Column: Wide', 'primer' ), 'one-column-narrow' => esc_html__( 'One Column: Narrow', 'primer' ), 'two-column-default' => esc_html__( 'Two Columns: Content | Sidebar', 'primer' ), 'two-column-reversed' => esc_html__( 'Two Columns: Sidebar | Content', 'primer' ), 'three-column-default' => esc_html__( 'Three Columns: Content | Sidebar | Sidebar', 'primer' ), 'three-column-center' => esc_html__( 'Three Columns: Sidebar | Content | Sidebar', 'primer' ), 'three-column-reversed' => esc_html__( 'Three Columns: Sidebar | Sidebar | Content', 'primer' ), ) ); if ( ! $this->layouts ) { return; } /** * Filter the default layout. * * @since 1.0.0 * * @var string */ $default = (string) apply_filters( 'primer_default_layout', $this->default ); $this->default = $this->layout_exists( $default ) ? $default : ( $this->layout_exists( $this->default ) ? $this->default : key( $this->layouts ) ); /** * Filter if post/page overrides via meta box should be enabled. * * @since 1.0.0 * * @var bool */ $this->meta_box = (bool) apply_filters( 'primer_layouts_meta_box_enabled', $this->meta_box ); /** * Filter the registered page widths. * * @since 1.0.0 * * @var array */ $this->page_widths = (array) apply_filters( 'primer_page_widths', array( 'fixed' => /* translators: fixed-width page layout */ esc_html__( 'Fixed', 'primer' ), 'fluid' => /* translators: fluid-width page layout */ esc_html__( 'Fluid', 'primer' ), ) ); add_action( 'init', array( $this, 'rtl_layouts' ), 11 ); add_action( 'init', array( $this, 'post_type_support' ), 11 ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); add_action( 'customize_register', array( $this, 'customize_register' ) ); if ( $this->meta_box ) { add_action( 'load-post.php', array( $this, 'load_post' ) ); add_action( 'load-post-new.php', array( $this, 'load_post' ) ); add_action( 'save_post', array( $this, 'save_post' ) ); } add_filter( 'body_class', array( $this, 'body_class' ) ); } /** * Alter some registered layouts when in RTL mode. * * @action init * @uses primer_layouts_rtl To filter the possible layouts. * * @since 1.0.0 */ public function rtl_layouts() { if ( ! is_rtl() ) { return; } /** * Filter changes needed for registered layouts when in RTL mode. * * @since 1.0.0 * * @var array */ $rtl_layouts = (array) apply_filters( 'primer_layouts_rtl', array( 'two-column-default' => esc_html__( 'Two Columns: Sidebar | Content', 'primer' ), 'two-column-reversed' => esc_html__( 'Two Columns: Content | Sidebar', 'primer' ), 'three-column-default' => esc_html__( 'Three Columns: Sidebar | Sidebar | Content', 'primer' ), 'three-column-reversed' => esc_html__( 'Three Columns: Content | Sidebar | Sidebar', 'primer' ), ) ); $this->layouts = array_merge( $this->layouts, $rtl_layouts ); } /** * Add post type support. * * @action init * @uses [add_post_type_support](https://codex.wordpress.org/Function_Reference/add_post_type_support) To add layout support to specified post types. * * @since 1.0.0 */ public function post_type_support() { /** * Filter the post types that allow layouts. * * @since 1.0.0 * * @var array */ $post_types = (array) apply_filters( 'primer_layouts_post_types', get_post_types( array( 'public' => true ) ) ); foreach ( $post_types as $post_type ) { add_post_type_support( $post_type, 'primer-layouts' ); } } /** * Enqueue scripts and styles for post meta box. * * @action admin_enqueue_scripts * @since 1.0.0 * * @param string $hook The suffix of the current admin page. */ public function admin_enqueue_scripts( $hook ) { if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $rtl = is_rtl() ? '-rtl' : ''; $suffix = SCRIPT_DEBUG ? '' : '.min'; wp_enqueue_script( 'primer-layouts', get_template_directory_uri() . "/assets/js/admin/layouts{$suffix}.js", array( 'jquery' ), PRIMER_VERSION ); wp_enqueue_style( 'primer-layouts', get_template_directory_uri() . "/assets/css/admin/layouts{$rtl}{$suffix}.css", array(), PRIMER_VERSION ); } /** * Add a new meta box to post screens. * * @action load-post.php * @action load-post-new.php * * @since 1.0.0 * @uses $this->add_meta_box() */ public function load_post() { add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 10, 2 ); } /** * Add post meta box for custom layouts. * * @see $this->load_post() * @since 1.0.0 * @uses $this->render_meta_box() * * @param string $post_type Post type slug name. * @param WP_Post $post Post object. */ public function add_meta_box( $post_type, WP_Post $post ) { if ( ! post_type_supports( $post_type, 'primer-layouts' ) || ! current_user_can( 'edit_post_meta', $post->ID ) || ! current_user_can( 'add_post_meta', $post->ID ) || ! current_user_can( 'delete_post_meta', $post->ID ) ) { return; } add_meta_box( 'primer-layouts-meta-box', esc_html__( 'Layout', 'primer' ), array( $this, 'render_meta_box' ), $post_type, 'side', 'default', null ); } /** * Display the custom layouts post meta box. * * @see $this->add_meta_box() * @since 1.0.0 * * @param WP_Post $post Post object. */ public function render_meta_box( WP_Post $post ) { $current_layout = $this->get_post_layout( $post->ID ); $has_custom = ! empty( $current_layout ); wp_nonce_field( basename( __FILE__ ), 'primer-layout-nonce' ); ?>
print_layout_choices( $this->layouts, $post->ID, $current_layout, $has_custom ); ?>