Collaboration
2 weeks ago
Apps.php
2 weeks ago
Collection.php
1 month ago
Comments.php
2 months ago
DynamicContent.php
1 month ago
ExportImport.php
2 days ago
Form.php
2 weeks ago
Media.php
2 days ago
Page.php
2 days ago
PageSettings.php
2 weeks ago
RBAC.php
2 weeks ago
Symbol.php
2 weeks ago
Taxonomy.php
2 months ago
TemplateExportImport.php
2 months ago
UserData.php
2 weeks ago
Users.php
2 months ago
Walkthrough.php
2 months ago
WordpressData.php
2 months ago
WpAdmin.php
2 days ago
WordpressData.php
235 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage WordPress global data |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Ajax; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * WordpressData API Class |
| 16 | */ |
| 17 | class WordpressData { |
| 18 | |
| 19 | /** |
| 20 | * Find Menu object by id |
| 21 | * |
| 22 | * @param array $menus wp menus. |
| 23 | * @param int $id wp menu id. |
| 24 | * @return array |
| 25 | */ |
| 26 | private static function find_object_by_id( $menus, $id ) { |
| 27 | return array_values( ( array_filter( $menus, fn ( $menu) => strval( $id ) === $menu->menu_item_parent ) ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Find parent submenu from all menus |
| 32 | * |
| 33 | * @param array $menus wp menus. |
| 34 | * @return array |
| 35 | */ |
| 36 | private static function find_parent_submenus( $menus ) { |
| 37 | return array_values( ( array_filter( $menus, fn ( $menu) => '0' === $menu->menu_item_parent ) ) ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get all submenus |
| 42 | * |
| 43 | * @param array $menu wp menu. |
| 44 | * @return array |
| 45 | */ |
| 46 | private static function get_submenus( $menu ) { |
| 47 | $submenus = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) ); |
| 48 | if ( $submenus ) { |
| 49 | $count = count( $submenus ); |
| 50 | for ( $i = 0; $i < $count; $i++ ) { |
| 51 | $submenus[ $i ]->submenus = self::find_object_by_id( $submenus, $submenus[ $i ]->ID ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $menu->submenus = self::find_parent_submenus( $submenus ); |
| 56 | |
| 57 | return $menu; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get WordPress menus data api |
| 62 | * |
| 63 | * @return void wp_send_json |
| 64 | */ |
| 65 | public static function get_wordpress_menus_data() { |
| 66 | $menus = wp_get_nav_menus(); |
| 67 | $count = count( $menus ); |
| 68 | for ( $i = 0; $i < $count; $i++ ) { |
| 69 | $menus[ $i ] = self::get_submenus( $menus[ $i ] ); |
| 70 | } |
| 71 | |
| 72 | wp_send_json( $menus ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get WordPress single menu data api |
| 77 | * |
| 78 | * @param int $menu_term_id single menu id. |
| 79 | * @param boolean $internal that means this method call from internally or not. |
| 80 | * @return void|object wp_send_json |
| 81 | */ |
| 82 | public static function get_wordpress_single_menu_data( $menu_term_id, $internal = false ) { |
| 83 | $menu = wp_get_nav_menu_object( $menu_term_id ); |
| 84 | |
| 85 | if ( $menu ) { |
| 86 | $menu = self::get_submenus( $menu ); |
| 87 | } |
| 88 | |
| 89 | if ( $internal ) { |
| 90 | return $menu; |
| 91 | } |
| 92 | |
| 93 | wp_send_json( $menu ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get WordPress author list |
| 98 | * |
| 99 | * @return void wp_send_json |
| 100 | */ |
| 101 | public static function get_author_list() { |
| 102 | $authors = get_users( |
| 103 | array( |
| 104 | 'capability__in' => array( |
| 105 | 'publish_posts', |
| 106 | ), |
| 107 | 'fields' => array( |
| 108 | 'ID', |
| 109 | 'display_name', |
| 110 | 'user_nicename', |
| 111 | ), |
| 112 | ) |
| 113 | ); |
| 114 | |
| 115 | wp_send_json( $authors ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get WordPress user list |
| 120 | * |
| 121 | * @return void wp_send_json |
| 122 | */ |
| 123 | public static function get_user_list() { |
| 124 | $authors = get_users( |
| 125 | array( |
| 126 | 'fields' => array( |
| 127 | 'ID', |
| 128 | 'display_name', |
| 129 | 'user_nicename', |
| 130 | ), |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | wp_send_json( $authors ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get WordPress category list |
| 139 | * |
| 140 | * @return void wp_send_json |
| 141 | */ |
| 142 | public static function get_category_list() { |
| 143 | $categories = get_categories(); |
| 144 | wp_send_json( $categories ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get WordPress post types data |
| 149 | * |
| 150 | * @return void wp_send_json |
| 151 | */ |
| 152 | public static function get_wordpress_post_types_data() { |
| 153 | $args = array( |
| 154 | 'public' => true, |
| 155 | ); |
| 156 | $post_types = get_post_types( $args, 'objects' ); |
| 157 | $types_arr = array(); |
| 158 | |
| 159 | $excluded_post_types = array( 'attachment' ); |
| 160 | |
| 161 | $filtered_post_types = array_filter( |
| 162 | $post_types, |
| 163 | function( $post_type ) use ( $excluded_post_types ) { |
| 164 | return ! in_array( $post_type->name, $excluded_post_types ); |
| 165 | } |
| 166 | ); |
| 167 | |
| 168 | foreach ( $filtered_post_types as $value ) { |
| 169 | $types_arr[] = array( |
| 170 | 'title' => $value->label, |
| 171 | 'value' => $value->name, |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | $types_arr = apply_filters( 'kirki_post_types', $types_arr ); |
| 176 | |
| 177 | wp_send_json( $types_arr ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get WordPress post types data |
| 182 | * |
| 183 | * @return void wp_send_json |
| 184 | */ |
| 185 | public static function get_wordpress_comment_types_data() { |
| 186 | global $wpdb; |
| 187 | |
| 188 | $comment_types = $wpdb->get_col( "SELECT DISTINCT(comment_type) FROM $wpdb->comments" ); |
| 189 | |
| 190 | $types_arr = array(); |
| 191 | |
| 192 | if ( is_array( $comment_types ) ) { |
| 193 | foreach ( $comment_types as $type ) { |
| 194 | $types_arr[] = array( |
| 195 | 'title' => ucfirst( preg_replace( '/[_ -]+/', ' ', $type ) ), |
| 196 | 'value' => $type, |
| 197 | ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | wp_send_json( $types_arr ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get WordPress role list |
| 206 | * |
| 207 | * @return void wp_send_json |
| 208 | */ |
| 209 | |
| 210 | public static function get_role_list() { |
| 211 | global $wp_roles; |
| 212 | |
| 213 | // Make sure wp_roles is instantiated |
| 214 | if ( ! isset( $wp_roles ) ) { |
| 215 | $wp_roles = new WP_Roles(); |
| 216 | } |
| 217 | |
| 218 | // Get all roles |
| 219 | $all_roles = $wp_roles->get_names(); |
| 220 | |
| 221 | // Prepare the array of objects |
| 222 | $roles_array = array(); |
| 223 | |
| 224 | foreach ( $all_roles as $role_slug => $role_name ) { |
| 225 | $roles_array[] = array( |
| 226 | 'value' => $role_slug, |
| 227 | 'title' => $role_name, |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | wp_send_json( $roles_array ); |
| 232 | } |
| 233 | |
| 234 | } |
| 235 |