is_select2 ) { return; } if ( is_callable( $this->data_source ) ) { $this->choices = call_user_func( $this->data_source ); return; } $choices = []; switch ( $this->data_source ) { case 'category': case 'tags': $taxonomy = 'category' === $this->data_source ? ( $this->data_source_name ?? 'category' ) : ( $this->data_source_name ?? 'post_tag' ); $args = [ 'hide_empty' => ( 'category' === $this->data_source ), 'taxonomy' => $taxonomy, ]; $terms = get_terms( $args ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { if ( is_object( $term ) ) { $choices[ $term->slug ] = $term->name; } } } break; case 'page': $pages = get_pages(); if ( ! empty( $pages ) ) { foreach ( $pages as $page ) { $choices[ $page->ID ] = $page->post_title; } } break; case 'post_types': case 'post_type': global $wp_post_types; $post_types = get_post_types( [ 'public' => true, 'exclude_from_search' => false, ] ); foreach ( $post_types as $name ) { if ( isset( $wp_post_types[ $name ]->labels->menu_name ) ) { $choices[ $name ] = $wp_post_types[ $name ]->labels->menu_name; } else { $choices[ $name ] = ucfirst( $name ); } } break; default: // Handle other data sources if needed. break; } $this->choices = $choices; } /** * Refresh the parameters passed to the JavaScript via JSON. * * @see WP_Customize_Control::to_json() */ public function to_json() { parent::to_json(); $this->json['subtitle'] = $this->subtitle; $this->json['choices'] = $this->choices; $this->json['placeholder'] = $this->placeholder; $this->json['is_select2'] = $this->is_select2; $this->json['multiple'] = $this->multiple ? ' multiple="multiple"' : ''; if ( $this->multiple ) { $this->json['value'] = implode( ',', (array) $this->json['value'] ); } } /** * Enqueue control related scripts/styles. * * @access public */ public function enqueue() { parent::enqueue(); if ( $this->is_select2 ) { // Script debug. $blogsy_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; /** * Enqueue select2 stylesheet. */ wp_enqueue_style( 'blogsy-select2-style', BLOGSY_THEME_URI . '/admin/dashboard/assets/css/select2' . $blogsy_suffix . '.css', false, BLOGSY_THEME_VERSION, 'all' ); /** * Enqueue select2 script. */ wp_enqueue_script( 'blogsy-select2-js', BLOGSY_THEME_URI . '/admin/dashboard/assets/js/libs/select2' . $blogsy_suffix . '.js', [ 'jquery' ], BLOGSY_THEME_VERSION, true ); } } /** * An Underscore (JS) template for this control's content (but not its container). * * Class variables for this control class are available in the `data` JS object; * export custom variables by overriding {@see WP_Customize_Control::to_json()}. * * @see WP_Customize_Control::print_template() */ protected function content_template() { ?>