6eba0f215e0f63e4602666ff62bef6e8ffe01842.svn-base 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
<?php if ( ! defined( 'ABSPATH' ) ) {
	die;
} // Cannot access pages directly.
/**
 *
 * Field: Select
 *
 */
/* Usage:
 *
array(
    'id'             => 'select_1',
    'type'           => 'select',
    'title'          => 'Title',
    'query'          => array(
        'type'          => 'callback',
        'function'      => array( $this, 'function_name' ),
        'args'          => array()                              // WordPress query args
    ),
),

OPTIONS OR QUERY:

- with static options
'options'        => array(
    'bmw'            => 'BMW',
    'mercedes'       => 'Mercedes',
    'volkswagen'     => 'Volkswagen',
    'other'          => 'Other',
),

- with a callback function
'query'          => array(
    'type'          => 'callback',
    'function'      => array( $this, 'function_name' ),
    'args'          => array()                                  // WordPress query args
),

- with a build-in types, you can use: posts, pages, categories, tags, menus, post_types and users
'query'          => array(
    'type'          => 'users',
    'args'			=> array(                                   // WordPress query args
        'orderby'   	=> 'nicename',
        'meta_query'	=> array(),                             // Your meta query
    ),
),

- also possible to use different args on different pages with ['query']['args']['multi_query'] = true
'query'          => array(
    'type'          => 'users',
    'args' 		    => array(                                   // WordPress query args
        'multi_query'	=> true,                                // to activate multi query
        array(
            'display_on'	=> array( 'page-slugs or ids' ),    // page slug or id where this args should apply
            ... continue with your meta query ...
        ),
        array(
            'display_on'	=> array( 'page-slugs or ids' ),    // page slug or id where this args should apply
            'orderby'   	=> 'nicename',
            'meta_query'	=> array(
                'relation'		=> 'AND',
                array(), // your meta query
                array(), // your meta query
            ),
        ),
    ),
),

 */
if ( ! class_exists( 'Exopite_Simple_Options_Framework_Field_select' ) ) {

	class Exopite_Simple_Options_Framework_Field_select extends Exopite_Simple_Options_Framework_Fields {

		public function __construct( $field, $value = '', $unique = '', $config = array() ) {
			parent::__construct( $field, $value, $unique, $config );
		}

		public function output() {

			echo $this->element_before();

			echo $this->element_prepend();

			if ( isset( $this->field['options'] ) || isset( $this->field['query'] ) ) {

				$options    = ( isset( $this->field['options'] ) && is_array( $this->field['options'] ) ) ? $this->field['options'] : array();
				$query      = ( isset( $this->field['query'] ) && isset( $this->field['query']['type'] ) ) ? $this->field['query'] : false;
				$select     = ( $query ) ? $this->element_data( $query['type'] ) : $options;
				$class      = $this->element_class();
				$extra_name = ( isset( $this->field['attributes']['multiple'] ) ) ? '[]' : '';

				echo '<select name="' . $this->element_name( $extra_name ) . '"' . $this->element_class() . $this->element_attributes() . '>';

				echo ( isset( $this->field['default_option'] ) ) ? '<option value="">' . $this->field['default_option'] . '</option>' : '';

				if ( ! empty( $select ) ) {

					foreach ( $select as $key => $value ) {
						echo '<option value="' . $key . '" ' . $this->checked( $this->element_value(), $key, 'selected' ) . '>' . $value . '</option>';

					}
				}

				echo '</select>';

			}

			echo $this->element_append();

			echo $this->element_after();

		}

		/**
		 * Populate select from wp_query
		 */
		public function element_data( $type = '' ) {

			$select     = array();
			$query      = ( isset( $this->field['query'] ) ) ? $this->field['query'] : array();
			$query_args = array();

			/**
			 * Make possible to have different args for different pages.
			 */
			if ( isset( $query['args'] ) && is_array( $query['args'] ) ) {

				// Check if multi_query is set
				if ( isset( $query['args']['multi_query'] ) && true === $query['args']['multi_query'] ) {

					foreach ( $query['args'] as $args ) {

						// Skip if not an array (eg. 'multi_query' => true )
						if ( ! is_array( $args ) ) {
							continue;
						}
						global $post;
						$display_on = $args['display_on'];

						// 'disply_on' is the post slog or id
						if ( ( is_array( $display_on ) && in_array( $post->post_name, $display_on ) ) ||
						     ( ! is_array( $display_on ) && $display_on == $post->post_name ) ||
						     ( is_array( $display_on ) && in_array( $post->ID, $display_on ) ) ||
						     ( ! is_array( $display_on ) && $display_on == $post->ID )
						) {

							// remove 'display_on'
							unset( $args['display_on'] );
							// set args
							$query_args = $args;
						}

					}

				} else {
					$query_args = $query['args'];
				}

			}

			switch ( $type ) {

				case 'pages':
				case 'page':

					$pages = get_pages( $query_args );

					if ( ! is_wp_error( $pages ) && ! empty( $pages ) ) {
						foreach ( $pages as $page ) {
							$select[ $page->ID ] = $page->post_title;
						}
					}

					break;

				case 'posts':
				case 'post':

					$posts = get_posts( $query_args );

					if ( ! is_wp_error( $posts ) && ! empty( $posts ) ) {
						foreach ( $posts as $post ) {
							$select[ $post->ID ] = $post->post_title;
						}
					}

					break;

				case 'categories':
				case 'category':

					$categories = get_categories( $query_args );

					if ( ! is_wp_error( $categories ) && ! empty( $categories ) && ! isset( $categories['errors'] ) ) {
						foreach ( $categories as $category ) {
							$select[ $category->term_id ] = $category->name;
						}
					}

					break;

				case 'tags':
				case 'tag':

					$taxonomies = ( isset( $query_args['taxonomies'] ) ) ? $query_args['taxonomies'] : 'post_tag';
					$tags       = get_terms( $taxonomies, $query_args );

					if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) {
						foreach ( $tags as $tag ) {
							$select[ $tag->term_id ] = $tag->name;
						}
					}

					break;

				case 'menus':
				case 'menu':

					$menus = wp_get_nav_menus( $query_args );

					if ( ! is_wp_error( $menus ) && ! empty( $menus ) ) {
						foreach ( $menus as $menu ) {
							$select[ $menu->term_id ] = $menu->name;
						}
					}

					break;

				case 'post_types':
				case 'post_type':

					$query_args['show_in_nav_menus'] = true;
					$post_types                      = get_post_types( $query_args );

					if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) ) {
						foreach ( $post_types as $post_type ) {
							$select[ $post_type ] = ucfirst( $post_type );
						}
					}

					break;

				case 'users':
				case 'user':

					$users = get_users( $query_args );

					/**
					 * key:   the name in select
					 * value: the value in select
					 */
					$key   = ( isset ( $this->field['query']['key'] ) ) ? sanitize_key( $this->field['query']['key'] ) : 'ID';
					$value = ( isset ( $this->field['query']['value'] ) ) ? sanitize_key( $this->field['query']['value'] ) : 'user_login';

					if ( ! is_wp_error( $users ) && ! empty( $users ) ) {
						foreach ( $users as $user ) {
							$select[ $user->{$key} ] = $user->{$value};
						}
					}

					break;

				case 'custom':
				case 'callback':

					/**
					 * Get post object if it is a metabox and not yet set.
					 * Then send post object to callback function.
					 */
					if ( isset( $this->config['type'] ) && $this->config['type'] == 'metabox' && ! isset( $post ) ) {
						global $post;
					} else {
						$post = array();
					}

					if ( is_callable( $query['function'] ) ) {
						$select = call_user_func( $query['function'], $query_args, $post );
					}

					break;

			}

			return $select;
		}


		public static function enqueue( $args ) {

			/**
			 * https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js
			 * https://www.sitepoint.com/jquery-select-box-components-chosen-vs-select2/
			 */
			$resources = array(
				array(
					'name'       => 'jquery-chosen',
					'fn'         => 'chosen.min.css',
					'type'       => 'style',
					'dependency' => array(),
					'version'    => '1.8.2',
					'attr'       => 'all',
				),
				array(
					'name'       => 'jquery-chosen',
					'fn'         => 'chosen.jquery.min.js',
					'type'       => 'script',
					'dependency' => array( 'jquery' ),
					'version'    => '1.8.2',
					'attr'       => true,
				),
				array(
					'name'       => 'exopite-sof-jquery-chosen-loader',
					'fn'         => 'loader-jquery-chosen.min.js',
					'type'       => 'script',
					'dependency' => array( 'jquery-chosen' ),
					'version'    => '',
					'attr'       => true,
				),
			);

			parent::do_enqueue( $resources, $args );

		}

	}

}