Builder
3 years ago
I18n
11 months ago
Migrate-ACF
1 year ago
Migrate-CPTUI
1 year ago
Migrate-PHP
1 year ago
Migrate-Packages
11 months ago
Roles
11 months ago
Templates
11 months ago
Advanced-Content-Types.php
3 years ago
Advanced-Relationships.php
3 years ago
Markdown.php
2 years ago
Pages.php
1 year ago
Table-Storage.php
4 years ago
Advanced-Relationships.php
181 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Name: Advanced Relationships |
| 4 | * |
| 5 | * Description: Add advanced relationship objects for relating to including Database Tables, Multisite Networks, Multisite Sites, Themes, Page Templates, Sidebars, Post Type Objects, and Taxonomy Objects |
| 6 | * |
| 7 | * Version: 2.3 |
| 8 | * |
| 9 | * Category: Advanced |
| 10 | * |
| 11 | * Tableless Mode: No |
| 12 | * |
| 13 | * @package Pods\Components |
| 14 | * @subpackage Advanced Relationships |
| 15 | */ |
| 16 | |
| 17 | if ( class_exists( 'Pods_Advanced_Relationships' ) ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Class Pods_Advanced_Relationships |
| 23 | */ |
| 24 | class Pods_Advanced_Relationships extends PodsComponent { |
| 25 | |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
| 29 | public function init() { |
| 30 | // Bypass if Pods is in types-only mode. |
| 31 | if ( pods_is_types_only() ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | add_action( 'pods_form_ui_field_pick_related_objects_other', array( $this, 'add_related_objects' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Add Advanced Related Objects |
| 40 | * |
| 41 | * @since 2.3.0 |
| 42 | */ |
| 43 | public function add_related_objects() { |
| 44 | |
| 45 | PodsField_Pick::$related_objects['table'] = array( |
| 46 | 'label' => __( 'Database Tables', 'pods' ), |
| 47 | 'group' => __( 'Advanced Objects', 'pods' ), |
| 48 | ); |
| 49 | |
| 50 | if ( is_multisite() ) { |
| 51 | PodsField_Pick::$related_objects['site'] = array( |
| 52 | 'label' => __( 'Multisite Sites', 'pods' ), |
| 53 | 'group' => __( 'Advanced Objects', 'pods' ), |
| 54 | ); |
| 55 | |
| 56 | PodsField_Pick::$related_objects['network'] = array( |
| 57 | 'label' => __( 'Multisite Networks', 'pods' ), |
| 58 | 'group' => __( 'Advanced Objects', 'pods' ), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | PodsField_Pick::$related_objects['theme'] = array( |
| 63 | 'label' => __( 'Themes', 'pods' ), |
| 64 | 'group' => __( 'Advanced Objects', 'pods' ), |
| 65 | 'simple' => true, |
| 66 | 'data_callback' => array( $this, 'data_themes' ), |
| 67 | ); |
| 68 | |
| 69 | PodsField_Pick::$related_objects['page-template'] = array( |
| 70 | 'label' => __( 'Page Templates', 'pods' ), |
| 71 | 'group' => __( 'Advanced Objects', 'pods' ), |
| 72 | 'simple' => true, |
| 73 | 'data_callback' => array( $this, 'data_page_templates' ), |
| 74 | ); |
| 75 | |
| 76 | PodsField_Pick::$related_objects['sidebar'] = array( |
| 77 | 'label' => __( 'Sidebars', 'pods' ), |
| 78 | 'group' => __( 'Advanced Objects', 'pods' ), |
| 79 | 'simple' => true, |
| 80 | 'data_callback' => array( $this, 'data_sidebars' ), |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Data callback for Themes |
| 86 | * |
| 87 | * @param string $name The name of the field |
| 88 | * @param string|array $value The value of the field |
| 89 | * @param array $options Field options |
| 90 | * @param array $pod Pod data |
| 91 | * @param int $id Item ID |
| 92 | * |
| 93 | * @return array |
| 94 | * |
| 95 | * @since 2.3.0 |
| 96 | */ |
| 97 | public function data_themes( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
| 98 | |
| 99 | $data = array(); |
| 100 | |
| 101 | $themes = wp_get_themes( array( 'allowed' => true ) ); |
| 102 | |
| 103 | foreach ( $themes as $theme ) { |
| 104 | $data[ $theme->Template ] = $theme->Name; |
| 105 | } |
| 106 | |
| 107 | return apply_filters( 'pods_form_ui_field_pick_data_themes', $data, $name, $value, $options, $pod, $id ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Data callback for Page Templates |
| 112 | * |
| 113 | * @param string $name The name of the field |
| 114 | * @param string|array $value The value of the field |
| 115 | * @param array $options Field options |
| 116 | * @param array $pod Pod data |
| 117 | * @param int $id Item ID |
| 118 | * |
| 119 | * @return array |
| 120 | * |
| 121 | * @since 2.3.0 |
| 122 | */ |
| 123 | public function data_page_templates( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
| 124 | |
| 125 | $data = array(); |
| 126 | |
| 127 | if ( ! function_exists( 'get_page_templates' ) ) { |
| 128 | include_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 129 | } |
| 130 | |
| 131 | $page_templates = apply_filters( 'pods_page_templates', get_page_templates() ); |
| 132 | |
| 133 | if ( ! in_array( 'page.php', $page_templates, true ) && locate_template( array( 'page.php', false ) ) ) { |
| 134 | $page_templates['Page (WP Default)'] = 'page.php'; |
| 135 | } |
| 136 | |
| 137 | if ( ! in_array( 'index.php', $page_templates, true ) && locate_template( array( 'index.php', false ) ) ) { |
| 138 | $page_templates['Index (WP Fallback)'] = 'index.php'; |
| 139 | } |
| 140 | |
| 141 | ksort( $page_templates ); |
| 142 | |
| 143 | $page_templates = array_flip( $page_templates ); |
| 144 | |
| 145 | foreach ( $page_templates as $page_template_file => $page_template ) { |
| 146 | $data[ $page_template_file ] = $page_template; |
| 147 | } |
| 148 | |
| 149 | return apply_filters( 'pods_form_ui_field_pick_data_page_templates', $data, $name, $value, $options, $pod, $id ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Data callback for Sidebars |
| 154 | * |
| 155 | * @param string $name The name of the field |
| 156 | * @param string|array $value The value of the field |
| 157 | * @param array $options Field options |
| 158 | * @param array $pod Pod data |
| 159 | * @param int $id Item ID |
| 160 | * |
| 161 | * @return array |
| 162 | * |
| 163 | * @since 2.3.0 |
| 164 | */ |
| 165 | public function data_sidebars( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
| 166 | |
| 167 | $data = array(); |
| 168 | |
| 169 | global $wp_registered_sidebars; |
| 170 | |
| 171 | if ( ! empty( $wp_registered_sidebars ) ) { |
| 172 | foreach ( $wp_registered_sidebars as $sidebar ) { |
| 173 | $data[ $sidebar['id'] ] = $sidebar['name']; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return apply_filters( 'pods_form_ui_field_pick_data_sidebars', $data, $name, $value, $options, $pod, $id ); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 |