| @@ -19,8 +19,15 @@ | ||
| 19 | 19 | 'fields' => [ |
| 20 | 20 | 'post_type' => 'string', |
| 21 | 21 | ], |
| 22 | 22 | ), |
| 23 | + 'get_taxonomy_term_data' => array( | |
| 24 | + 'callback' => array( $this, 'get_taxonomy_term_data' ), | |
| 25 | + 'capability' => 'edit_posts', | |
| 26 | + 'fields' => [ | |
| 27 | + 'taxonomy' => 'string', | |
| 28 | + ], | |
| 29 | + ), | |
| 23 | 30 | 'get_terms_for_post' => array( |
| 24 | 31 | 'callback' => array( $this, 'get_terms_for_post' ), |
| 25 | 32 | 'capability' => 'edit_posts', |
| 26 | 33 | 'fields' => [ |
| @@ -102,8 +109,16 @@ | ||
| 102 | 109 | 'fields' => [ |
| 103 | 110 | 'postID' => 'integer', |
| 104 | 111 | ], |
| 105 | 112 | ), |
| 113 | + 'get_academy_course_data' => array( | |
| 114 | + 'callback' => array( $this, 'get_academy_course_data' ), | |
| 115 | + 'capability' => 'edit_posts', | |
| 116 | + 'fields' => [ | |
| 117 | + 'post_id' => 'integer', | |
| 118 | + 'meta_key' => 'string', | |
| 119 | + ], | |
| 120 | + ), | |
| 106 | 121 | ); |
| 107 | 122 | } |
| 108 | 123 | |
| 109 | 124 | public function get_taxonomies_data( $payload ) { |
| @@ -117,8 +132,27 @@ | ||
| 117 | 132 | |
| 118 | 133 | wp_send_json_success( $all_taxonomies ); |
| 119 | 134 | } |
| 120 | 135 | |
| 136 | + public function get_taxonomy_term_data( $payload ) { | |
| 137 | + if ( empty( $payload['taxonomy'] ) ) { | |
| 138 | + wp_send_json_error( __( 'Sorry, Taxonomy is required', 'ablocks' ) ); | |
| 139 | + } | |
| 140 | + $terms = get_terms([ | |
| 141 | + 'taxonomy' => $payload['taxonomy'], | |
| 142 | + 'hide_empty' => false, // Set to true if you only want terms attached to posts | |
| 143 | + ]); | |
| 144 | + | |
| 145 | + $results = array_map(function( $term ) { | |
| 146 | + return [ | |
| 147 | + 'label' => $term->name, | |
| 148 | + 'value' => $term->term_id, | |
| 149 | + ]; | |
| 150 | + }, $terms); | |
| 151 | + | |
| 152 | + wp_send_json_success( $results ); | |
| 153 | + } | |
| 154 | + | |
| 121 | 155 | public function get_terms_for_post( $payload ) { |
| 122 | 156 | $terms = Helper::get_terms_for_post( $payload['taxonomy'], $payload['post_id'] ); |
| 123 | 157 | wp_send_json_success( $terms ); |
| 124 | 158 | } |
| @@ -138,8 +172,9 @@ | ||
| 138 | 172 | WHERE p.post_type = %s |
| 139 | 173 | AND pm.meta_key NOT LIKE '\_%' |
| 140 | 174 | "; |
| 141 | 175 | |
| 176 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 142 | 177 | $meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) ); |
| 143 | 178 | |
| 144 | 179 | wp_send_json_success( $meta_keys ); |
| 145 | 180 | } |
| @@ -149,13 +184,12 @@ | ||
| 149 | 184 | if ( empty( $payload['post_id'] ) || empty( $payload['meta_key'] ) ) { |
| 150 | 185 | wp_send_json_error( __( 'Sorry, Post ID and Meta Key is required!', 'ablocks' ) ); |
| 151 | 186 | } |
| 152 | 187 | |
| 153 | - $post_id = $payload['post_id']; | |
| 188 | + $post_id = (int) $payload['post_id']; | |
| 154 | 189 | $meta_key = $payload['meta_key']; |
| 155 | 190 | |
| 156 | 191 | $result = get_post_meta( $post_id, $meta_key, true ); |
| 157 | - | |
| 158 | 192 | wp_send_json_success( $result ); |
| 159 | 193 | } |
| 160 | 194 | |
| 161 | 195 | public function search_posts( $payload ) { |
| @@ -180,8 +214,9 @@ | ||
| 180 | 214 | AND post_type IN ($post_type_placeholders) |
| 181 | 215 | ORDER BY post_date DESC |
| 182 | 216 | LIMIT 5 |
| 183 | 217 | "; |
| 218 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 184 | 219 | $prepared_query = $wpdb->prepare( $query, ...$included_post_types ); |
| 185 | 220 | } else { |
| 186 | 221 | $query = " |
| 187 | 222 | SELECT ID, post_title, post_type |
| @@ -191,10 +226,12 @@ | ||
| 191 | 226 | AND post_type IN ($post_type_placeholders) |
| 192 | 227 | LIMIT 5 |
| 193 | 228 | "; |
| 194 | 229 | $safe_query = '%' . $wpdb->esc_like( $search_query ) . '%'; |
| 230 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 195 | 231 | $prepared_query = $wpdb->prepare( $query, array_merge( [ $safe_query ], $included_post_types ) ); |
| 196 | 232 | }//end if |
| 233 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 197 | 234 | $results = $wpdb->get_results( $prepared_query, ARRAY_A ); |
| 198 | 235 | wp_send_json_success( $results ); |
| 199 | 236 | } |
| 200 | 237 | |
| @@ -284,13 +321,11 @@ | ||
| 284 | 321 | $search_query = $payload['query'] ?? ''; |
| 285 | 322 | $media_id = $payload['mediaId'] ?? 0; |
| 286 | 323 | |
| 287 | 324 | if ( empty( $search_query ) ) { |
| 288 | - $prepared_query = $wpdb->prepare( | |
| 289 | - "SELECT ID, post_title FROM {$wpdb->posts} | |
| 325 | + $prepared_query = "SELECT ID, post_title FROM {$wpdb->posts} | |
| 290 | 326 | WHERE post_type = 'attachment' AND post_status = 'inherit' |
| 291 | - ORDER BY post_date DESC LIMIT 5" | |
| 292 | - ); | |
| 327 | + ORDER BY post_date DESC LIMIT 5"; | |
| 293 | 328 | } else { |
| 294 | 329 | $prepared_query = $wpdb->prepare( |
| 295 | 330 | "SELECT ID, post_title FROM {$wpdb->posts} |
| 296 | 331 | WHERE post_type = 'attachment' AND post_status = 'inherit' |
| @@ -299,8 +334,9 @@ | ||
| 299 | 334 | '%' . $wpdb->esc_like( $search_query ) . '%' |
| 300 | 335 | ); |
| 301 | 336 | } |
| 302 | 337 | |
| 338 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 303 | 339 | $results = $wpdb->get_results( $prepared_query, ARRAY_A ); |
| 304 | 340 | |
| 305 | 341 | $media_items = array_map(function ( $media ) { |
| 306 | 342 | return [ |
| @@ -313,8 +349,9 @@ | ||
| 313 | 349 | $media_item = null; |
| 314 | 350 | $media_id_present_in_result = $media_id && ! in_array( "{$media_id}", array_column( $media_items, 'value' ) ); |
| 315 | 351 | |
| 316 | 352 | if ( $media_id_present_in_result ) { |
| 353 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 317 | 354 | $media_item = $wpdb->get_row($wpdb->prepare( |
| 318 | 355 | "SELECT ID, post_title FROM {$wpdb->posts} |
| 319 | 356 | WHERE ID = %d AND post_type = 'attachment' AND post_status = 'inherit'", |
| 320 | 357 | $media_id |
| @@ -365,6 +402,34 @@ | ||
| 365 | 402 | $author_id = get_post_field( 'post_author', $post_id ); |
| 366 | 403 | $result = get_avatar_url( $author_id ); |
| 367 | 404 | |
| 368 | 405 | wp_send_json_success( $result ); |
| 406 | + } | |
| 407 | + | |
| 408 | + public function get_academy_course_data( $payload ) { | |
| 409 | + | |
| 410 | + $post_id = $payload['post_id'] ?? 0; | |
| 411 | + $meta_key = $payload['meta_key'] ?? ''; | |
| 412 | + | |
| 413 | + if ( ! $post_id ) { | |
| 414 | + $post_id = get_the_ID(); | |
| 415 | + } | |
| 416 | + | |
| 417 | + if ( ! Helper::is_active_academy() ) { | |
| 418 | + wp_send_json_error( '' ); | |
| 419 | + } | |
| 420 | + | |
| 421 | + $Analytics = new \Academy\Classes\Analytics(); | |
| 422 | + $results = 0; | |
| 423 | + if ( 'numberOfTopics' === $meta_key ) { | |
| 424 | + $curriculums = \Academy\Helper::get_course_curriculums_number_of_counts( $post_id ); | |
| 425 | + $results = (int) isset( $curriculums['total_topics'] ) ? $curriculums['total_topics'] : 0; | |
| 426 | + } elseif ( 'numberOfReviews' === $meta_key ) { | |
| 427 | + $results = (int) $Analytics->get_total_number_of_reviews_by_course_id( $post_id ); | |
| 428 | + } elseif ( 'numberOfEnrolled' === $meta_key ) { | |
| 429 | + $results = (int) $Analytics->get_total_number_of_enrolled_by_course_id( $post_id ); | |
| 430 | + } elseif ( 'totalDuration' === $meta_key ) { | |
| 431 | + $results = (int) \Academy\Helper::get_course_duration( $post_id ); | |
| 432 | + } | |
| 433 | + wp_send_json_success( $results ); | |
| 369 | 434 | } |
| 370 | 435 | } |