Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor wpdb statements #165

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions includes/class-boldgrid-inspirations-purchase-for-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ public function get_local_publish_cost_data() {
*
* @since 1.1.4
*
* @return string A string to be used after in IN statement, example: ( 'draft', 'publish' ).
* @return array An array of post status.
*/
public function get_post_status() {
$post_status = array( 'draft', 'publish' );
Expand All @@ -1005,9 +1005,6 @@ public function get_post_status() {
*/
$post_status = apply_filters( 'boldgrid_cart_post_status', $post_status );

// Implode our array of $post_status to be used alongside an IN statement.
$post_status = '( "' . implode( '", "', esc_sql( $post_status ) ) . '" )';

return $post_status;
}

Expand Down Expand Up @@ -1298,14 +1295,20 @@ public function asset_needs_purchase( $asset, $asset_type, $args = array() ) {
* [gallery ids='123,456'].
*/
$regexp = '[\[][^\]]+[\'\", ]' . $attachment_id . '[^0-9]+.*[\]]';
$query = '
SELECT `ID`
FROM ' . $wpdb->posts . '
WHERE `post_status` IN ' . $post_status . ' AND
`post_type` IN ("page","post") AND
`post_content` REGEXP "' . $regexp . '"
';
$in_shortcode = $wpdb->get_var( $query );
/**
* Previously, we were not using a prepare statement.
* In order to ensure that the prepare statment
* doesn't incorrectly escape the post status array,
* we dynamically create the placeholders now.
*/
$in_shortcode = $wpdb->get_var(
$wpdb->prepare(
"SELECT `ID` FROM $wpdb->posts WHERE `post_status` IN ( " .
implode( ', ', array_fill( 0, count( $post_status ), '%s' ) ) .
" ) AND `post_type` IN ( 'page', 'post' ) AND `post_content` REGEXP %s",
array_merge( $post_status, array( $regexp ) )
)
);
if ( ! empty( $in_shortcode ) ) {
$this->assets_needing_purchase['by_page_id'][ $in_shortcode ][] = $asset;
return true;
Expand All @@ -1322,9 +1325,12 @@ public function asset_needs_purchase( $asset, $asset_type, $args = array() ) {
WHERE $wpdb->postmeta.meta_key = '_thumbnail_id' AND
$wpdb->postmeta.meta_value = %s AND
$wpdb->postmeta.post_id = $wpdb->posts.ID AND
$wpdb->posts.post_status IN $post_status AND
$wpdb->posts.post_type IN ('page','post')
", $attachment_id ) );
$wpdb->posts.post_status IN ( " .
implode( ', ', array_fill( 0, count( $post_status ), '%s' ) ) .
") AND $wpdb->posts.post_type IN ('page','post')",
array_merge( array( $attachment_id ), $post_status )
)
);

// If we found results, then the image is being used in a page/post.
if ( ! empty( $asset_a_featured_image ) ) {
Expand Down Expand Up @@ -1414,17 +1420,16 @@ public function asset_needs_purchase( $asset, $asset_type, $args = array() ) {

if ( 'image' == $asset_type && ! empty( $array_file_names_to_query ) ) {
foreach ( $array_file_names_to_query as $file_name_to_query ) {

// SELECT post_title where post_content like
// '%2015/02/google-maps-int-1410976385-pi.jpg%'
$query = $wpdb->prepare("
SELECT `ID`
FROM $wpdb->posts
WHERE `post_content` LIKE %s AND
`post_status` IN $post_status AND
`post_status` IN ( " . implode( ', ', array_fill( 0, count( $post_status ), '%s' ) ) . " ) AND
`post_type` IN ('page','post')
",
'%' . $wpdb->esc_like( $file_name_to_query ) . '%'
array_merge( $post_status, array( '%' . $wpdb->esc_like( $file_name_to_query ) . '%' ) )
);

// If we want to exclude any page IDs, exclude them now.
Expand Down