/** * Set up our control. * * @since 3.0.0 * @access public * @param object $manager * @param string $id * @param array $args * @return void */ public function __construct($manager, $id, $args = array()) { // Array of allowed layouts. Pass via `$args['layouts']`. $allowed = !empty($args['layouts']) ? $args['layouts'] : array_keys(hybrid_get_layouts()); // Loop through each of the layouts and add it to the choices array with proper key/value pairs. foreach (hybrid_get_layouts() as $layout) { if (in_array($layout->name, $allowed) && !('theme_layout' === $id && false === $layout->is_global_layout) && $layout->image) { $args['choices'][$layout->name] = array('label' => $layout->label, 'url' => sprintf($layout->image, get_template_directory_uri(), get_stylesheet_directory_uri())); } } // Let the parent class handle the rest. parent::__construct($manager, $id, $args); }
/** * Callback function for displaying the layout meta box. * * @since 3.0.0 * @access public * @param object $object * @param array $box * @return void */ function hybrid_post_layout_meta_box($post, $box) { // Get the current post's layout. $post_layout = hybrid_get_post_layout($post->ID); $post_layout = $post_layout ? $post_layout : 'default'; wp_nonce_field(basename(__FILE__), 'hybrid-post-layout-nonce'); ?> <?php foreach (hybrid_get_layouts() as $layout) { ?> <?php if (true === $layout->is_post_layout && $layout->image && !(!empty($layout->post_types) && !in_array($post->post_type, $layout->post_types))) { ?> <label class="has-img"> <input type="radio" value="<?php echo esc_attr($layout->name); ?> " name="hybrid-post-layout" <?php checked($post_layout, $layout->name); ?> /> <span class="screen-reader-text"><?php echo esc_html($layout->label); ?> </span> <img src="<?php echo esc_url(sprintf($layout->image, get_template_directory_uri(), get_stylesheet_directory_uri())); ?> " alt="<?php echo esc_attr($layout->label); ?> " /> </label> <?php } ?> <?php } ?> <script type="text/javascript"> jQuery( document ).ready( function( $ ) { // Add the `.checked` class to whichever radio is checked. $( '#hybrid-post-layout input:checked' ).addClass( 'checked' ); // When a radio is clicked. $( "#hybrid-post-layout input" ).click( function() { // If the radio has the `.checked` class, remove it and uncheck the radio. if ( $( this ).hasClass( 'checked' ) ) { $( "#hybrid-post-layout input" ).removeClass( 'checked' ); $( this ).prop( 'checked', false ); // If the radio is not checked, ad the `.checked` class and check it. } else { $( "#hybrid-post-layout input" ).removeClass( 'checked' ); $( this ).addClass( 'checked' ); } } ); } ); </script> <?php }