auxin-elements
/
includes
/
elementor
/
modules
/
query-control
/
controls
/
group-control-related.php
group-control-posts.php
2 years ago
group-control-query.php
6 years ago
group-control-related.php
6 years ago
query.php
6 years ago
group-control-related.php
120 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Controls; |
| 3 | |
| 4 | use Elementor\Controls_Manager; |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; // Exit if accessed directly |
| 8 | } |
| 9 | |
| 10 | class Group_Control_Related extends Group_Control_Query { |
| 11 | |
| 12 | public static function get_type() { |
| 13 | return 'aux-related-query'; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Build the group-controls array |
| 18 | * Note: this method completely overrides any settings done in Group_Control_Posts |
| 19 | * @param string $name |
| 20 | * |
| 21 | * @return array |
| 22 | */ |
| 23 | protected function init_fields_by_name( $name ) { |
| 24 | $fields = parent::init_fields_by_name( $name ); |
| 25 | |
| 26 | $tabs_wrapper = $name . '_query_args'; |
| 27 | $include_wrapper = $name . '_query_include'; |
| 28 | |
| 29 | $fields['post_type']['options']['related'] = __( 'Related', 'auxin-elements' ); |
| 30 | $fields['include_term_ids']['condition']['post_type!'][] = 'related'; |
| 31 | $fields['related_taxonomies']['condition']['post_type'][] = 'related'; |
| 32 | $fields['include_authors']['condition']['post_type!'][] = 'related'; |
| 33 | $fields['exclude_authors']['condition']['post_type!'][] = 'related'; |
| 34 | $fields['avoid_duplicates']['condition']['post_type!'][] = 'related'; |
| 35 | $fields['offset']['condition']['post_type!'][] = 'related'; |
| 36 | |
| 37 | $related_taxonomies = [ |
| 38 | 'label' => __( 'Term', 'auxin-elements' ), |
| 39 | 'type' => Controls_Manager::SELECT2, |
| 40 | 'options' => $this->get_supported_taxonomies(), |
| 41 | 'label_block' => true, |
| 42 | 'multiple' => true, |
| 43 | 'condition' => [ |
| 44 | 'include' => 'terms', |
| 45 | 'post_type' => [ |
| 46 | 'related', |
| 47 | ], |
| 48 | ], |
| 49 | 'tabs_wrapper' => $tabs_wrapper, |
| 50 | 'inner_tab' => $include_wrapper, |
| 51 | ]; |
| 52 | |
| 53 | $related_fallback = [ |
| 54 | 'label' => __( 'Fallback', 'auxin-elements' ), |
| 55 | 'type' => Controls_Manager::SELECT, |
| 56 | 'options' => [ |
| 57 | 'fallback_none' => __( 'None', 'auxin-elements' ), |
| 58 | 'fallback_by_id' => __( 'Manual Selection', 'auxin-elements' ), |
| 59 | 'fallback_recent' => __( 'Recent Posts', 'auxin-elements' ), |
| 60 | ], |
| 61 | 'default' => 'fallback_none', |
| 62 | 'description' => __( 'Displayed if no relevant results are found. Manual selection display order is random', 'auxin-elements' ), |
| 63 | 'condition' => [ |
| 64 | 'post_type' => 'related', |
| 65 | ], |
| 66 | 'separator' => 'before', |
| 67 | ]; |
| 68 | |
| 69 | $fallback_ids = [ |
| 70 | 'label' => __( 'Search & Select', 'auxin-elements' ), |
| 71 | 'type' => 'aux-query', |
| 72 | 'post_type' => '', |
| 73 | 'options' => [], |
| 74 | 'label_block' => true, |
| 75 | 'multiple' => true, |
| 76 | 'filter_type' => 'by_id', |
| 77 | 'condition' => [ |
| 78 | 'post_type' => 'related', |
| 79 | 'related_fallback' => 'fallback_by_id', |
| 80 | ], |
| 81 | 'export' => false, |
| 82 | ]; |
| 83 | |
| 84 | $fields = \Elementor\Utils::array_inject( $fields, 'include_term_ids', [ 'related_taxonomies' => $related_taxonomies ] ); |
| 85 | $fields = \Elementor\Utils::array_inject( $fields, 'offset', [ 'related_fallback' => $related_fallback ] ); |
| 86 | $fields = \Elementor\Utils::array_inject( $fields, 'related_fallback', [ 'fallback_ids' => $fallback_ids ] ); |
| 87 | |
| 88 | return $fields; |
| 89 | } |
| 90 | |
| 91 | protected function get_supported_taxonomies() { |
| 92 | $supported_taxonomies = []; |
| 93 | |
| 94 | $public_types = auxin_get_public_post_types(); |
| 95 | |
| 96 | foreach ( $public_types as $type => $title ) { |
| 97 | $taxonomies = get_object_taxonomies( $type, 'objects' ); |
| 98 | foreach ( $taxonomies as $key => $tax ) { |
| 99 | if ( ! array_key_exists( $key, $supported_taxonomies ) ) { |
| 100 | $label = $tax->label; |
| 101 | if ( in_array( $tax->label, $supported_taxonomies ) ) { |
| 102 | $label = $tax->label . ' (' . $tax->name . ')'; |
| 103 | } |
| 104 | $supported_taxonomies[ $key ] = $label; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $supported_taxonomies; |
| 110 | } |
| 111 | |
| 112 | protected static function init_presets() { |
| 113 | parent::init_presets(); |
| 114 | static::$presets['related'] = [ |
| 115 | 'related_fallback', |
| 116 | 'fallback_ids', |
| 117 | ]; |
| 118 | } |
| 119 | } |
| 120 |