From ceaa2a39bc14a15fec4f67766c1db6d4b61edba3 Mon Sep 17 00:00:00 2001 From: Justin Sternberg Date: Sun, 27 Mar 2016 18:16:21 -0400 Subject: [PATCH] Add loader to manage loading the most recent version of this lib --- cmb2-attached-posts-field.php | 353 +++++++++++++--------------------- example-field-setup.php | 5 +- init.php | 252 ++++++++++++++++++++++++ 3 files changed, 387 insertions(+), 223 deletions(-) create mode 100644 init.php diff --git a/cmb2-attached-posts-field.php b/cmb2-attached-posts-field.php index f549230..7d76ce9 100644 --- a/cmb2-attached-posts-field.php +++ b/cmb2-attached-posts-field.php @@ -1,244 +1,159 @@ + * @copyright 2016 WebDevStudios + * @license GPL-2.0+ + * @version 1.2.3 + * @link https://github.com/WebDevStudios/cmb2-attached-posts + * @since 1.2.3 */ -class WDS_CMB2_Attached_Posts_Field { - /** - * Current version number - */ - const VERSION = '1.2.2'; +/** + * Copyright (c) 2016 WebDevStudios (email : contact@webdevstudios.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 or, at + * your discretion, any later version, as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ - /** - * Initialize the plugin by hooking into CMB2 - */ - public function __construct() { - add_action( 'cmb2_render_custom_attached_posts', array( $this, 'render' ), 10, 5 ); - add_action( 'cmb2_sanitize_custom_attached_posts', array( $this, 'sanitize' ), 10, 2 ); - } +/** + * Loader versioning: http://jtsternberg.github.io/wp-lib-loader/ + */ + +if ( ! class_exists( 'WDS_CMB2_Attached_Posts_Field_123', false ) ) { /** - * Add a CMB custom field to allow for the selection of multiple posts - * attached to a single page + * Versioned loader class-name + * + * This ensures each version is loaded/checked. + * + * @category WordPressLibrary + * @package WDS_CMB2_Attached_Posts_Field + * @author WebDevStudios + * @license GPL-2.0+ + * @version 1.2.3 + * @link https://github.com/WebDevStudios/cmb2-attached-posts + * @since 1.2.3 */ - public function render( $field, $escaped_value, $object_id, $object_type, $field_type ) { - - $this->setup_admin_scripts(); - - // Setup our args - $args = wp_parse_args( (array) $field->options( 'query_args' ), array( - 'post_type' => 'post', - 'posts_per_page' => 100, - 'orderby' => 'name', - 'order' => 'ASC', - ) ); - - // loop through post types to get labels for all - $post_type_labels = array(); - foreach ( (array) $args['post_type'] as $post_type ) { - // Get post type object for attached post type - $attached_post_type = get_post_type_object( $post_type ); - - // continue if we don't have a label for the post type - if ( ! $attached_post_type || ! isset( $attached_post_type->labels->name ) ) { - continue; + class WDS_CMB2_Attached_Posts_Field_123 { + + /** + * WDS_CMB2_Attached_Posts_Field version number + * @var string + * @since 1.2.3 + */ + const VERSION = '1.2.3'; + + /** + * Current version hook priority. + * Will decrement with each release + * + * @var int + * @since 1.2.3 + */ + const PRIORITY = 9999; + + /** + * Starts the version checking process. + * Creates CMB2_ATTACHED_POSTS_FIELD_LOADED definition for early detection by + * other scripts. + * + * Hooks WDS_CMB2_Attached_Posts_Field inclusion to the cmb2_attached_posts_field_load hook + * on a high priority which decrements (increasing the priority) with + * each version release. + * + * @since 1.2.3 + */ + public function __construct() { + if ( ! defined( 'CMB2_ATTACHED_POSTS_FIELD_LOADED' ) ) { + /** + * A constant you can use to check if WDS_CMB2_Attached_Posts_Field is loaded + * for your plugins/themes with WDS_CMB2_Attached_Posts_Field dependency. + * + * Can also be used to determine the priority of the hook + * in use for the currently loaded version. + */ + define( 'CMB2_ATTACHED_POSTS_FIELD_LOADED', self::PRIORITY ); } - $post_type_labels[] = $attached_post_type->labels->name; - } - - $post_type_labels = implode( '/', $post_type_labels ); - - // Check 'filter' setting - $filter_boxes = $field->options( 'filter_boxes' ) - ? '
' - : ''; - - // Get our posts - $posts = get_posts( $args ); - - // If there are no posts found, just stop - if ( ! $posts ) { - return; - } - - // Check to see if we have any meta values saved yet - $attached = (array) $escaped_value; - - // Set our count class - $count = 0; - - // Wrap our lists - echo '
'; - - // Open our retrieved, or found posts, list - echo '
'; - echo '

' . sprintf( __( 'Available %s', 'cmb' ), $post_type_labels ) . '

'; + // Use the hook system to ensure only the newest version is loaded. + add_action( 'cmb2_attached_posts_field_load', array( $this, 'include_lib' ), self::PRIORITY ); - // Set .has_thumbnail - $has_thumbnail = $field->options( 'show_thumbnails' ) ? ' has-thumbnails' : ''; - $hide_selected = $field->options( 'hide_selected' ) ? ' hide-selected' : ''; - - if ( $filter_boxes ) { - printf( $filter_boxes, 'available-search' ); + // Use the hook system to ensure only the newest version is loaded. + add_action( 'after_setup_theme', array( $this, 'do_hook' ) ); } - echo '
    '; - - // Loop through our posts as list items - foreach ( $posts as $post ) { - - // Increase our count - $count++; - - // Set our zebra stripes - $zebra = $count % 2 == 0 ? 'even' : 'odd'; - - // Set a class if our post is in our attached post meta - $added = ! empty ( $attached ) && in_array( $post->ID, $attached ) ? ' added' : ''; - - // Set thumbnail if the options is true - $thumbnail = $has_thumbnail ? get_the_post_thumbnail( $post->ID, array( 50, 50 ) ) : ''; - - // Build our list item - echo '
  • ', $thumbnail ,'', get_the_title( $post ) ,'
  • '; - - } - - // Close our retrieved, or found, posts - echo '
'; - echo '
'; - - // Open our attached posts list - echo '
'; - echo '

' . sprintf( __( 'Attached %s', 'cmb' ), $post_type_labels ) . '

'; - - if ( $filter_boxes ) { - printf( $filter_boxes, 'attached-search' ); + /** + * Fires the cmb2_attached_posts_field_load action hook + * (from the after_setup_theme hook). + * + * @since 1.2.3 + */ + public function do_hook() { + // Then fire our hook. + do_action( 'cmb2_attached_posts_field_load' ); } - echo '
    '; - - // If we have any posts saved already, display them - $post_ids = $this->display_attached( $field, $attached ); - - $value = ! empty( $post_ids ) ? implode( ',', $post_ids ) : ''; - - // Close up shop - echo '
'; - echo '
'; - - echo $field_type->input( array( - 'type' => 'hidden', - 'class' => 'attached-posts-ids', - 'value' => $value, - 'desc' => '', - ) ); - - echo '
'; - - // Display our description if one exists - $field_type->_desc( true, true ); - - } - - /** - * Helper function to grab and filter our post meta - */ - protected function display_attached( $field, $attached ) { - - // Start with nothing - $output = ''; - - // If we do, then we need to display them as items in our attached list - if ( ! $attached ) { - return; - } - - // Set our count to zero - $count = 0; - - $show_thumbnails = $field->options( 'show_thumbnails' ); - // Remove any empty values - $attached = array_filter( $attached ); - $post_ids = array(); - - // Loop through and build our existing display items - foreach ( $attached as $post_id ) { - if ( ! get_post( $post_id ) ) { - continue; + /** + * A final check if WDS_CMB2_Attached_Posts_Field exists before kicking off + * our WDS_CMB2_Attached_Posts_Field loading. + * + * CMB2_ATTACHED_POSTS_FIELD_VERSION and CMB2_ATTACHED_POSTS_FIELD_DIR constants are + * set at this point. + * + * @since 1.2.3 + */ + public function include_lib() { + if ( class_exists( 'WDS_CMB2_Attached_Posts_Field', false ) ) { + return; } - // Increase our count - $count++; - - // Set our zebra stripes - $zebra = $count % 2 == 0 ? 'even' : 'odd'; - - // Set thumbnail if the options is true - $thumbnail = $show_thumbnails ? get_the_post_thumbnail( $post_id, array( 50, 50 ) ) : ''; - - // Build our list item - echo '
  • ', $thumbnail ,''. get_the_title( $post_id ) .'
  • '; + if ( ! defined( 'CMB2_ATTACHED_POSTS_FIELD_VERSION' ) ) { + /** + * Defines the currently loaded version of WDS_CMB2_Attached_Posts_Field. + */ + define( 'CMB2_ATTACHED_POSTS_FIELD_VERSION', self::VERSION ); + } - $post_ids[] = $post_id; + if ( ! defined( 'CMB2_ATTACHED_POSTS_FIELD_DIR' ) ) { + /** + * Defines the directory of the currently loaded version of WDS_CMB2_Attached_Posts_Field. + */ + define( 'CMB2_ATTACHED_POSTS_FIELD_DIR', dirname( __FILE__ ) . '/' ); + } + // Include and initiate WDS_CMB2_Attached_Posts_Field. + require_once CMB2_ATTACHED_POSTS_FIELD_DIR . 'init.php'; } - return $post_ids; - } - - public function sanitize( $sanitized_val, $val ) { - if ( ! empty( $val ) ) { - return explode( ',', $val ); - } - return $sanitized_val; } - /** - * Enqueue admin scripts for our attached posts field - */ - protected function setup_admin_scripts() { - $dir = trailingslashit( dirname( __FILE__ ) ); - - if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { - // Windows - $content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR ); - $content_url = str_replace( $content_dir, WP_CONTENT_URL, $dir ); - $url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url ); - - } else { - $url = str_replace( - array( WP_CONTENT_DIR, WP_PLUGIN_DIR ), - array( WP_CONTENT_URL, WP_PLUGIN_URL ), - $dir - ); - } - - $url = set_url_scheme( $url ); - - $requirements = array( - 'jquery-ui-core', - 'jquery-ui-widget', - 'jquery-ui-mouse', - 'jquery-ui-draggable', - 'jquery-ui-droppable', - 'jquery-ui-sortable', - ); - - wp_enqueue_script( 'cmb2-attached-posts-field', $url . 'js/attached-posts.js', $requirements, self::VERSION, true ); - wp_enqueue_style( 'cmb2-attached-posts-field', $url . 'css/attached-posts-admin.css', array(), self::VERSION ); - - } + // Kick it off. + new WDS_CMB2_Attached_Posts_Field_123; } -$cmb2_attached_posts_field = new WDS_CMB2_Attached_Posts_Field(); diff --git a/example-field-setup.php b/example-field-setup.php index 67e2f84..a9a1bc5 100644 --- a/example-field-setup.php +++ b/example-field-setup.php @@ -7,10 +7,7 @@ * Get the bootstrap! If using as a plugin, REMOVE THIS! */ require_once WPMU_PLUGIN_DIR . '/cmb2/init.php'; - -if ( ! function_exists( 'cmb2_attached_posts_fields_render' ) ) { - require_once WPMU_PLUGIN_DIR . '/cmb2-attached-posts/cmb2-attached-posts-field.php'; -} +require_once WPMU_PLUGIN_DIR . '/cmb2-attached-posts/cmb2-attached-posts-field.php'; /** * Define the metabox and field configurations. diff --git a/init.php b/init.php new file mode 100644 index 0000000..1b27742 --- /dev/null +++ b/init.php @@ -0,0 +1,252 @@ +setup_admin_scripts(); + + // Setup our args + $args = wp_parse_args( (array) $field->options( 'query_args' ), array( + 'post_type' => 'post', + 'posts_per_page' => 100, + 'orderby' => 'name', + 'order' => 'ASC', + ) ); + + // loop through post types to get labels for all + $post_type_labels = array(); + foreach ( (array) $args['post_type'] as $post_type ) { + // Get post type object for attached post type + $attached_post_type = get_post_type_object( $post_type ); + + // continue if we don't have a label for the post type + if ( ! $attached_post_type || ! isset( $attached_post_type->labels->name ) ) { + continue; + } + + $post_type_labels[] = $attached_post_type->labels->name; + } + + $post_type_labels = implode( '/', $post_type_labels ); + + // Check 'filter' setting + $filter_boxes = $field->options( 'filter_boxes' ) + ? '
    ' + : ''; + + // Get our posts + $posts = get_posts( $args ); + + // If there are no posts found, just stop + if ( ! $posts ) { + return; + } + + // Check to see if we have any meta values saved yet + $attached = (array) $escaped_value; + + // Set our count class + $count = 0; + + // Wrap our lists + echo '
    '; + + // Open our retrieved, or found posts, list + echo '
    '; + echo '

    ' . sprintf( __( 'Available %s', 'cmb' ), $post_type_labels ) . '

    '; + + // Set .has_thumbnail + $has_thumbnail = $field->options( 'show_thumbnails' ) ? ' has-thumbnails' : ''; + $hide_selected = $field->options( 'hide_selected' ) ? ' hide-selected' : ''; + + if ( $filter_boxes ) { + printf( $filter_boxes, 'available-search' ); + } + + echo '
      '; + + // Loop through our posts as list items + foreach ( $posts as $post ) { + + // Increase our count + $count++; + + // Set our zebra stripes + $zebra = $count % 2 == 0 ? 'even' : 'odd'; + + // Set a class if our post is in our attached post meta + $added = ! empty ( $attached ) && in_array( $post->ID, $attached ) ? ' added' : ''; + + // Set thumbnail if the options is true + $thumbnail = $has_thumbnail ? get_the_post_thumbnail( $post->ID, array( 50, 50 ) ) : ''; + + // Build our list item + echo '
    • ', $thumbnail ,'', get_the_title( $post ) ,'
    • '; + + } + + // Close our retrieved, or found, posts + echo '
    '; + echo '
    '; + + // Open our attached posts list + echo '
    '; + echo '

    ' . sprintf( __( 'Attached %s', 'cmb' ), $post_type_labels ) . '

    '; + + if ( $filter_boxes ) { + printf( $filter_boxes, 'attached-search' ); + } + + echo '
      '; + + // If we have any posts saved already, display them + $post_ids = $this->display_attached( $field, $attached ); + + $value = ! empty( $post_ids ) ? implode( ',', $post_ids ) : ''; + + // Close up shop + echo '
    '; + echo '
    '; + + echo $field_type->input( array( + 'type' => 'hidden', + 'class' => 'attached-posts-ids', + 'value' => $value, + 'desc' => '', + ) ); + + echo '
    '; + + // Display our description if one exists + $field_type->_desc( true, true ); + + } + + /** + * Helper function to grab and filter our post meta + */ + protected function display_attached( $field, $attached ) { + + // Start with nothing + $output = ''; + + // If we do, then we need to display them as items in our attached list + if ( ! $attached ) { + return; + } + + // Set our count to zero + $count = 0; + + $show_thumbnails = $field->options( 'show_thumbnails' ); + // Remove any empty values + $attached = array_filter( $attached ); + $post_ids = array(); + + // Loop through and build our existing display items + foreach ( $attached as $post_id ) { + if ( ! get_post( $post_id ) ) { + continue; + } + + // Increase our count + $count++; + + // Set our zebra stripes + $zebra = $count % 2 == 0 ? 'even' : 'odd'; + + // Set thumbnail if the options is true + $thumbnail = $show_thumbnails ? get_the_post_thumbnail( $post_id, array( 50, 50 ) ) : ''; + + // Build our list item + echo '
  • ', $thumbnail ,''. get_the_title( $post_id ) .'
  • '; + + $post_ids[] = $post_id; + + } + + return $post_ids; + } + + public function sanitize( $sanitized_val, $val ) { + if ( ! empty( $val ) ) { + return explode( ',', $val ); + } + return $sanitized_val; + } + + /** + * Enqueue admin scripts for our attached posts field + */ + protected function setup_admin_scripts() { + $dir = CMB2_ATTACHED_POSTS_FIELD_DIR; + + if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { + // Windows + $content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR ); + $content_url = str_replace( $content_dir, WP_CONTENT_URL, $dir ); + $url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url ); + + } else { + $url = str_replace( + array( WP_CONTENT_DIR, WP_PLUGIN_DIR ), + array( WP_CONTENT_URL, WP_PLUGIN_URL ), + $dir + ); + } + + $url = set_url_scheme( $url ); + $url = apply_filters( 'cmb2_attached_posts_field_assets_url', $url ); + + $requirements = array( + 'jquery-ui-core', + 'jquery-ui-widget', + 'jquery-ui-mouse', + 'jquery-ui-draggable', + 'jquery-ui-droppable', + 'jquery-ui-sortable', + ); + + wp_enqueue_script( 'cmb2-attached-posts-field', $url . 'js/attached-posts.js', $requirements, self::VERSION, true ); + wp_enqueue_style( 'cmb2-attached-posts-field', $url . 'css/attached-posts-admin.css', array(), self::VERSION ); + } +} +WDS_CMB2_Attached_Posts_Field::get_instance();