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 ); ?>
get_global_layout(); if ( ! $current_layout ) { $current_layout = $global_layout; } $name = isset( $wp_customize ) ? '_customize-radio' : 'primer'; ?>
get_post_layout( $post_id ); if ( ! $override && $current ) { delete_post_meta( $post_id, 'primer_layout' ); return; } $layout = isset( $_POST['primer-layout'] ) ? sanitize_key( $_POST['primer-layout'] ) : null; // input var ok. if ( ! $override || ! $this->layout_exists( $layout ) || $layout === $current ) { return; } update_post_meta( $post_id, 'primer_layout', $layout ); } /** * Register custom layout settings. * * @action customize_register * @see WP_Customize_Manager * * @since 1.0.0 * * @param WP_Customize_Manager $wp_customize Instance of the WP_Customize_Manager class. */ public function customize_register( WP_Customize_Manager $wp_customize ) { $wp_customize->add_section( 'layout', array( 'title' => esc_html__( 'Layout', 'primer' ), 'priority' => 30, 'capability' => 'edit_theme_options', ) ); if ( ! $this->page_widths ) { return; } reset( $this->page_widths ); $wp_customize->add_setting( 'page_width', array( 'default' => key( $this->page_widths ), 'sanitize_callback' => 'sanitize_key', 'transport' => 'postMessage', ) ); $wp_customize->add_control( 'page_width', array( 'label' => esc_html__( 'Page Width', 'primer' ), 'description' => esc_html__( 'Display your site differently on larger screens.', 'primer' ), 'section' => 'layout', 'settings' => 'page_width', 'type' => 'radio', 'choices' => $this->page_widths, ) ); } /** * Add layout class to body element on the front-end. * * @filter body_class * @since 1.0.0 * * @param array $classes Array of body classes. * * @return array Returns the filtered array of body classes. */ public function body_class( array $classes ) { $classes[] = sanitize_html_class( sprintf( 'layout-%s', $this->get_current_layout() ) ); $classes[] = primer_is_fluid_width() ? 'no-max-width' : null; return array_filter( $classes ); } /** * Check if a layout exists. * * @since 1.0.0 * * @param string $layout The layout name to check. * * @return bool Returns true if the layout is set, otherwise false. */ protected function layout_exists( $layout ) { return isset( $this->layouts[ $layout ] ); } /** * Return a post ID. * * @param WP_Post|int $post (optional) The WP_Post object. * * @return int Returns the post ID. */ protected function get_post_id( $post = null ) { return is_a( 'WP_Post', $post ) ? $post->ID : ( is_numeric( $post ) ? absint( $post ) : get_queried_object_id() ); } /** * Return the layout override for a post. * * @param WP_Post|int $post (optional) The WP_Post object. * * @return string Returns the post layout. */ protected function get_post_layout( $post = null ) { return get_post_meta( $this->get_post_id( $post ), 'primer_layout', true ); } /** * Return the global layout. * * @uses [get_theme_mod](https://codex.wordpress.org/Function_Reference/get_theme_mod) To retreive the layout mod. * * @return string Returns the layout theme modification. */ public function get_global_layout() { return get_theme_mod( 'layout', $this->default ); } /** * Return the current layout. * * @uses get_post_layout * @uses get_global_layout * @uses layout_exists * * @param WP_Post|int $post (optional) The WP_Post object. * * @return string Returns the current layout which is set. */ public function get_current_layout( $post = null ) { $override = $this->get_post_layout( $post ); $layout = ( $override ) ? $override : $this->get_global_layout(); /** * Filter the current layout. * * @since 1.0.0 * * @param WP_Post|int|null $post * * @var string */ $layout = (string) apply_filters( 'primer_current_layout', $layout, $post ); return $this->layout_exists( $layout ) ? $layout : $this->default; } } $GLOBALS['primer_customizer_layouts'] = new Primer_Customizer_Layouts;