_c(). $nonce_value = isset( $_GET['nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : ''; if ( ! $nonce_value || ! wp_verify_nonce( $nonce_value, 'wpaf_shortcode_form' ) ) { $this->setError( __( 'You are not authorized to fetch coupons, refresh page and try again', 'affcoupon' ) ); $this->getResponseJSON( true ); die(); } // Capability check — must be logged in to access own coupons. if ( ! is_user_logged_in() ) { $this->setError( __( 'You must be logged in to access coupons.', 'affcoupon' ) ); $this->getResponseJSON( true ); die(); } if ( isset( $_GET['coupons'] ) ) { $coupons_query = sanitize_text_field( wp_unslash( $_GET['coupons'] ) ); switch ( $coupons_query ) { case 'thrash': if ( isset( $_GET['coupon_id'] ) ) { $coupon_id = absint( wp_unslash( $_GET['coupon_id'] ) ); $this->thrash_a_coupon( $coupon_id ); } else { $this->setError( __( 'Bad request, check request and try again', 'affcoupon' ) ); } break; case 'single': if ( isset( $_GET['coupon_id'] ) ) { $coupon_id = absint( wp_unslash( $_GET['coupon_id'] ) ); $this->get_a_coupon( $coupon_id ); } else { $this->setError( __( 'Bad request, check request and try again', 'affcoupon' ) ); } break; default: $user_id = $this->_c()->wp_get_current_user()->ID; $this->get_all_user_coupons( $user_id ); break; } } $this->getResponseJSON( true ); die(); } /** * thrash current user's coupon * * @param int $coupon_id coupon id */ private function thrash_a_coupon( $coupon_id ) { $is_thrash_enabled = get_option( 'wpaf_form-shortcode-enable-thrash', '' ) === 'on'; if ( $this->is_current_user_original_author( $coupon_id ) && $is_thrash_enabled ) { $result = $this->_c()->wp_delete_post( $coupon_id ); if ( $result ) { $this->setData( 'message', __( 'Coupon deleted', 'affcoupon' ) ); $this->setData( 'id', $coupon_id ); } else { $this->setError( __( 'An error occured, try again later', 'affcoupon' ) ); } } else { $this->setError( __( 'You are not authorized to fetch coupons, refresh page and try again', 'affcoupon' ) ); } } /** * check if current user is the original author of the given post * * @param int $post_id post id * * @return bool current user is original author or not */ private function is_current_user_original_author( $post_id ) { $current_user = $this->_c()->wp_get_current_user()->ID; $post_author = $this->_c()->get_post( $post_id )->post_author; return $current_user == $post_author; } /** * fetch a single coupon * * @param int $coupon_id coupon post id */ private function get_a_coupon( $coupon_id ) { $coupon = []; $coupon['ID'] = $coupon_id; if ( $this->is_current_user_original_author( $coupon_id ) ) { // coupon meta fetch $post_meta = $this->_c()->get_post_meta( $coupon_id ); $coupon = array_merge( $coupon, $post_meta ); $post_meta_prefix = 'coupon_details_'; foreach ( array_keys( $coupon ) as $key ) { if ( strpos( $key, $post_meta_prefix ) === 0 ) { $new_key = str_replace( $post_meta_prefix, '', $key ); $coupon[ $new_key ] = $coupon[ $key ][0]; unset( $coupon[ $key ] ); } } // coupon taxonomy fetch $custom_taxonomy = wp_get_post_terms( $coupon_id, WPAF_Plugin::CUSTOM_TAXONOMY ); $vendor_taxonomy = wp_get_post_terms( $coupon_id, WPAF_Plugin::VENDOR_TAXONOMY ); $coupon['terms'][ WPAF_Plugin::CUSTOM_TAXONOMY ] = $this->filter_tax( $custom_taxonomy, 'term_id' ); $coupon['terms'][ WPAF_Plugin::VENDOR_TAXONOMY ] = $this->filter_tax( $vendor_taxonomy, 'term_id' ); // coupon thumbnail url fetch $coupon['featured_url'] = $this->_c()->get_the_post_thumbnail_url( $coupon_id ); // coupon attachment image url fetch $coupon_attachment_id = $coupon['coupon-image-input']; $coupon['coupon-image-input-url'] = wp_get_attachment_image_url( $coupon_attachment_id ); $coupon['wpaf_description'] = $coupon['description']; $coupon['coupon-title'] = get_the_title( $coupon_id ); unset( $coupon['description'] ); // setting coupon data $this->setData( 'data', $coupon ); } else { $this->setError( __( 'You are not authorized to fetch this coupon', 'affcoupon' ) ); } } /** * filter the taxonomy array so that only 'name' fields of the terms will be returned * * @param array $tax_array source array * @param string $key key * * @return array filtered array */ private function filter_tax( $tax_array, $key ) { $temp_array = []; foreach ( $tax_array as $tax ) { $temp_array[] = $tax->$key; } return $temp_array; } /** * ajax logic for getting all current users' coupons * * @param int $user_id current user id */ private function get_all_user_coupons( $user_id ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Per-user authored coupon list for the logged-in author's own management screen; no benefit from object cache. $results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_status, post_title, MAX(CASE WHEN (meta.meta_key = 'coupon_details_coupon-type') THEN meta.meta_value ELSE NULL END) as coupon_type, MAX(CASE WHEN (meta.meta_key = 'coupon_details_coupon-title') THEN meta.meta_value ELSE NULL END) as coupon_title, MAX(CASE WHEN (meta.meta_key = 'coupon_details_expire-date') THEN meta.meta_value ELSE NULL END) as expire_date FROM {$wpdb->posts} as posts INNER JOIN {$wpdb->postmeta} as meta ON posts.ID = meta.post_id WHERE posts.post_type = %s AND posts.post_status IN ('publish', 'draft', 'pending') AND meta.meta_key IN ('coupon_details_coupon-type', 'coupon_details_coupon-title', 'coupon_details_expire-date') AND posts.post_author = %d GROUP BY posts.ID ORDER BY ID DESC", WPAF_Plugin::CUSTOM_POST_TYPE, $user_id ) ); foreach ( $results as $r ) { $terms = []; // tax insert $terms['category'] = wp_get_post_terms( $r->ID, WPAF_Plugin::CUSTOM_TAXONOMY ); $terms['vendor'] = wp_get_post_terms( $r->ID, WPAF_Plugin::VENDOR_TAXONOMY ); $r->terms = $terms; } $this->setData( 'data', $results ); } }