| 1 |
<?php |
| 2 |
/** |
| 3 |
* API for managing the grids. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/api |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* API for managing the grids. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/api |
| 18 |
* @author Brecht Vandersmissen <[email protected]> |
| 19 |
*/ |
| 20 |
class WPUPG_API_Manage_Grids { |
| 21 |
|
| 22 |
/** |
| 23 |
* Register actions and filters. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
add_action( 'rest_api_init', array( __CLASS__, 'api_register_data' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register data for the REST API. |
| 33 |
* |
| 34 |
* @since 3.0.0 |
| 35 |
*/ |
| 36 |
public static function api_register_data() { |
| 37 |
if ( function_exists( 'register_rest_field' ) ) { |
| 38 |
register_rest_route( 'wp-ultimate-post-grid/v1', '/manage/grids', array( |
| 39 |
'callback' => array( __CLASS__, 'api_manage_grids' ), |
| 40 |
'methods' => 'POST', |
| 41 |
'permission_callback' => array( __CLASS__, 'api_required_permissions' ), |
| 42 |
) ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Required permissions for the API. |
| 48 |
* |
| 49 |
* @since 3.0.0 |
| 50 |
*/ |
| 51 |
public static function api_required_permissions() { |
| 52 |
return current_user_can( WPUPG_Settings::get( 'features_manage_access' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Handle manage grids call to the REST API. |
| 57 |
* |
| 58 |
* @since 3.0.0 |
| 59 |
* @param WP_REST_Request $request Current request. |
| 60 |
*/ |
| 61 |
public static function api_manage_grids( $request ) { |
| 62 |
// Parameters. |
| 63 |
$params = $request->get_params(); |
| 64 |
|
| 65 |
$page = isset( $params['page'] ) ? intval( $params['page'] ) : 0; |
| 66 |
$page_size = isset( $params['pageSize'] ) ? intval( $params['pageSize'] ) : 25; |
| 67 |
$sorted = isset( $params['sorted'] ) ? $params['sorted'] : array( array( 'id' => 'id', 'desc' => true ) ); |
| 68 |
$filtered = isset( $params['filtered'] ) ? $params['filtered'] : array(); |
| 69 |
|
| 70 |
// Starting query args. |
| 71 |
$args = array( |
| 72 |
'post_type' => WPUPG_POST_TYPE, |
| 73 |
'post_status' => 'any', |
| 74 |
'posts_per_page' => $page_size, |
| 75 |
'offset' => $page * $page_size, |
| 76 |
'meta_query' => array( |
| 77 |
'relation' => 'AND', |
| 78 |
), |
| 79 |
'tax_query' => array(), |
| 80 |
); |
| 81 |
|
| 82 |
// Order. |
| 83 |
$args['order'] = $sorted[0]['desc'] ? 'DESC' : 'ASC'; |
| 84 |
switch( $sorted[0]['id'] ) { |
| 85 |
case 'date': |
| 86 |
$args['orderby'] = 'date'; |
| 87 |
break; |
| 88 |
case 'name': |
| 89 |
$args['orderby'] = 'title'; |
| 90 |
break; |
| 91 |
case 'language': |
| 92 |
$args['orderby'] = 'meta_value'; |
| 93 |
$args['meta_key'] = 'wpupg_language'; |
| 94 |
break; |
| 95 |
default: |
| 96 |
$args['orderby'] = 'ID'; |
| 97 |
} |
| 98 |
|
| 99 |
// Filter. |
| 100 |
if ( $filtered ) { |
| 101 |
foreach ( $filtered as $filter ) { |
| 102 |
$value = trim( $filter['value'] ); |
| 103 |
switch( $filter['id'] ) { |
| 104 |
case 'id': |
| 105 |
$args['wpupg_search_id'] = $value; |
| 106 |
break; |
| 107 |
case 'date': |
| 108 |
$args['wpupg_search_date'] = $value; |
| 109 |
break; |
| 110 |
case 'name': |
| 111 |
$args['wpupg_search_title'] = $value; |
| 112 |
break; |
| 113 |
case 'language': |
| 114 |
if ( 'all' !== $value ) { |
| 115 |
$args['meta_query'][] = array( |
| 116 |
'key' => 'wpupg_language', |
| 117 |
'compare' => '=', |
| 118 |
'value' => $value, |
| 119 |
); |
| 120 |
} |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
add_filter( 'posts_where', array( __CLASS__, 'api_manage_grids_query_where' ), 10, 2 ); |
| 127 |
$query = new WP_Query( $args ); |
| 128 |
remove_filter( 'posts_where', array( __CLASS__, 'api_manage_grids_query_where' ), 10, 2 ); |
| 129 |
|
| 130 |
$grids = array(); |
| 131 |
$posts = $query->posts; |
| 132 |
foreach ( $posts as $post ) { |
| 133 |
$grid = WPUPG_Grid_Manager::get_grid( $post ); |
| 134 |
|
| 135 |
if ( ! $grid ) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
$grids[] = $grid->get_data_manage(); |
| 140 |
} |
| 141 |
|
| 142 |
// Got total number of grids. |
| 143 |
$total = (array) wp_count_posts( WPUPG_POST_TYPE ); |
| 144 |
unset( $total['trash'] ); |
| 145 |
|
| 146 |
return array( |
| 147 |
'rows' => array_values( $grids ), |
| 148 |
'total' => array_sum( $total ), |
| 149 |
'filtered' => intval( $query->found_posts ), |
| 150 |
'pages' => ceil( $query->found_posts / $page_size ), |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Filter the where query. |
| 156 |
* |
| 157 |
* @since 3.0.0 |
| 158 |
*/ |
| 159 |
public static function api_manage_grids_query_where( $where, $wp_query ) { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$id_search = $wp_query->get( 'wpupg_search_id' ); |
| 163 |
if ( $id_search ) { |
| 164 |
$where .= ' AND ' . $wpdb->posts . '.ID LIKE \'%' . esc_sql( like_escape( $id_search ) ) . '%\''; |
| 165 |
} |
| 166 |
|
| 167 |
$date_search = $wp_query->get( 'wpupg_search_date' ); |
| 168 |
if ( $date_search ) { |
| 169 |
$where .= ' AND ' . $wpdb->posts . '.post_date LIKE \'%' . esc_sql( like_escape( $date_search ) ) . '%\''; |
| 170 |
} |
| 171 |
|
| 172 |
$title_search = $wp_query->get( 'wpupg_search_title' ); |
| 173 |
if ( $title_search ) { |
| 174 |
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $title_search ) ) . '%\''; |
| 175 |
} |
| 176 |
|
| 177 |
$slug_search = $wp_query->get( 'wpupg_search_slug' ); |
| 178 |
if ( $slug_search ) { |
| 179 |
$where .= ' AND ' . $wpdb->posts . '.post_name LIKE \'%' . esc_sql( like_escape( $slug_search ) ) . '%\''; |
| 180 |
} |
| 181 |
|
| 182 |
return $where; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
WPUPG_API_Manage_Grids::init(); |
| 187 |
|