Skip to content

Commit

Permalink
Merge pull request #3243 from WordImpress/issues/2710
Browse files Browse the repository at this point in the history
fix(compat): prevent missing donation data with Restrict Content Pro #2710
  • Loading branch information
Devin Walker committed May 24, 2018
2 parents 8a4f327 + 19c4224 commit dc33641
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 43 deletions.
21 changes: 11 additions & 10 deletions includes/admin/tools/export/give-export-donations-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function give_export_donations_get_custom_fields() {
global $wpdb;
$post_type = 'give_payment';
$responses = array();
$donationmeta_table_key = Give()->payment_meta->get_meta_type() . '_id';

$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : '';

Expand All @@ -32,16 +33,16 @@ function give_export_donations_get_custom_fields() {

$query_and = sprintf(
"AND $wpdb->posts.ID IN (%s)
AND $wpdb->paymentmeta.meta_key != ''
AND $wpdb->paymentmeta.meta_key NOT RegExp '(^[_0-9].+$)'",
AND $wpdb->donationmeta.meta_key != ''
AND $wpdb->donationmeta.meta_key NOT RegExp '(^[_0-9].+$)'",
$donation_list
);

$query = "
SELECT DISTINCT($wpdb->paymentmeta.meta_key)
SELECT DISTINCT($wpdb->donationmeta.meta_key)
FROM $wpdb->posts
LEFT JOIN $wpdb->paymentmeta
ON $wpdb->posts.ID = $wpdb->paymentmeta.payment_id
LEFT JOIN $wpdb->donationmeta
ON $wpdb->posts.ID = {$wpdb->donationmeta}.{$donationmeta_table_key}
WHERE $wpdb->posts.post_type = '%s'
" . $query_and;

Expand All @@ -53,16 +54,16 @@ function give_export_donations_get_custom_fields() {

$query_and = sprintf(
"AND $wpdb->posts.ID IN (%s)
AND $wpdb->paymentmeta.meta_key != ''
AND $wpdb->paymentmeta.meta_key NOT RegExp '^[^_]'",
AND $wpdb->donationmeta.meta_key != ''
AND $wpdb->donationmeta.meta_key NOT RegExp '^[^_]'",
$donation_list
);

$query = "
SELECT DISTINCT($wpdb->paymentmeta.meta_key)
SELECT DISTINCT($wpdb->donationmeta.meta_key)
FROM $wpdb->posts
LEFT JOIN $wpdb->paymentmeta
ON $wpdb->posts.ID = $wpdb->paymentmeta.payment_id
LEFT JOIN $wpdb->donationmeta
ON $wpdb->posts.ID = {$wpdb->donationmeta}.{$donationmeta_table_key}
WHERE $wpdb->posts.post_type = '%s'
" . $query_and;

Expand Down
30 changes: 28 additions & 2 deletions includes/admin/upgrades/upgrade-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ function give_show_upgrade_notices( $give_updates ) {
)
);

// v2.1.3 Verify Form Status Upgrade.
$give_updates->register(
array(
'id' => 'v213_rename_donation_meta_type',
'version' => '2.1.3',
'callback' => 'give_v213_rename_donation_meta_type_callback',
'depend' => array( 'v20_move_metadata_into_new_table' ),
)
);

}

add_action( 'give_register_updates', 'give_show_upgrade_notices' );
Expand Down Expand Up @@ -2668,7 +2678,6 @@ function give_v210_verify_form_status_upgrades_callback() {
// The Update Ran.
give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
}

}

/**
Expand Down Expand Up @@ -2703,5 +2712,22 @@ function give_v213_delete_donation_meta_callback() {
// The Update Ran.
give_set_upgrade_complete( 'v213_delete_donation_meta' );
}
}

}
/**
* Rename donation meta type
*
* @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
*
* @since 2.1.3
*/
function give_v213_rename_donation_meta_type_callback(){
global $wpdb;
$give_updates = Give_Updates::get_instance();

$wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta CHANGE COLUMN payment_id donation_id bigint(20)" );
$wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta RENAME TO {$wpdb->prefix}give_donationmeta" );

give_set_upgrade_complete('v213_rename_donation_meta_type');
$give_updates->set_percentage(1, 1);
}
28 changes: 15 additions & 13 deletions includes/class-give-db-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,29 +284,29 @@ public function __rename_meta_table_name_in_query( $clause, $wp_query ) {
*
* @return mixed
*/
public function __rename_meta_table_name( $clause, $filter ){
public function __rename_meta_table_name( $clause, $filter ) {
global $wpdb;

$clause = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.{$this->meta_type}_id", $clause );
$clause = str_replace( $wpdb->postmeta, $this->table_name, $clause );

switch( $filter ) {
switch ( $filter ) {
case 'posts_join':
$joins = array( 'INNER JOIN', 'LEFT JOIN' );

foreach ( $joins as $join ) {
if( false !== strpos( $clause, $join ) ) {
if ( false !== strpos( $clause, $join ) ) {
$clause = explode( $join, $clause );

foreach ( $clause as $key => $clause_part ) {
if( empty( $clause_part ) ) {
if ( empty( $clause_part ) ) {
continue;
}

preg_match( '/' . $wpdb->prefix . 'give_' . $this->meta_type . 'meta AS (.*) ON/', $clause_part, $alias_table_name );

if( isset( $alias_table_name[1] ) ) {
$clause[$key] = str_replace( "{$alias_table_name[1]}.post_id", "{$alias_table_name[1]}.{$this->meta_type}_id", $clause_part );
if ( isset( $alias_table_name[1] ) ) {
$clause[ $key ] = str_replace( "{$alias_table_name[1]}.post_id", "{$alias_table_name[1]}.{$this->meta_type}_id", $clause_part );
}
}

Expand All @@ -316,7 +316,10 @@ public function __rename_meta_table_name( $clause, $filter ){
break;

case 'posts_where':
$clause = str_replace( array( 'mt2.post_id', 'mt1.post_id' ), array( "mt2.{$this->meta_type}_id", "mt1.{$this->meta_type}_id" ), $clause );
$clause = str_replace( array( 'mt2.post_id', 'mt1.post_id' ), array(
"mt2.{$this->meta_type}_id",
"mt1.{$this->meta_type}_id"
), $clause );
break;
}

Expand Down Expand Up @@ -396,11 +399,10 @@ private function delete_cache( $id, $meta_type = '' ) {
$meta_type = empty( $meta_type ) ? $this->meta_type : $meta_type;

$group = array(
// 'form' => 'give-forms',
'payment' => 'give-donations',
'payment' => 'give-donations', // Backward compatibility
'donation' => 'give-donations',
'donor' => 'give-donors',
'customer' => 'give-donors', // Backward compatibility for pre upgrade in 2.0
// 'log' => 'give-logs',
);

if ( array_key_exists( $meta_type, $group ) ) {
Expand Down Expand Up @@ -510,12 +512,12 @@ public function create_table() {
/**
* Get meta type
*
* @since 2.0.4
* @since 2.0.4
* @access public
*
* @return string
*/
public function get_meta_type(){
public function get_meta_type() {
return $this->meta_type;
}

Expand All @@ -539,4 +541,4 @@ public function delete_all_meta( $id = 0 ) {

return $status;
}
}
}
20 changes: 13 additions & 7 deletions includes/class-give-db-payment-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Give_DB_Payment_Meta extends Give_DB_Meta {
* @access protected
* @var bool
*/
protected $meta_type = 'payment';
protected $meta_type = 'donation';

/**
* Give_DB_Payment_Meta constructor.
Expand All @@ -50,10 +50,16 @@ public function __construct() {
/* @var WPDB $wpdb */
global $wpdb;

$wpdb->paymentmeta = $this->table_name = $wpdb->prefix . 'give_paymentmeta';
$this->primary_key = 'meta_id';
// @todo: We leave $wpdb->paymentmeta for backward compatibility, use $wpdb->donationmeta instead. We can remove it after 2.1.3.
$wpdb->paymentmeta = $wpdb->donationmeta = $this->table_name = $wpdb->prefix . 'give_donationmeta';
$this->version = '1.0';

// Backward compatibility.
if ( ! give_has_upgrade_completed( 'v213_rename_donation_meta_type' ) ) {
$this->meta_type = 'payment';
$wpdb->paymentmeta = $wpdb->donationmeta = $this->table_name = $wpdb->prefix . 'give_paymentmeta';
}

$this->register_table();

parent::__construct();
Expand All @@ -69,10 +75,10 @@ public function __construct() {
*/
public function get_columns() {
return array(
'meta_id' => '%d',
'payment_id' => '%d',
'meta_key' => '%s',
'meta_value' => '%s',
'meta_id' => '%d',
"{$this->meta_type}_id" => '%d',
'meta_key' => '%s',
'meta_value' => '%s',
);
}

Expand Down
3 changes: 2 additions & 1 deletion includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ function give_run_install() {
'v201_move_metadata_into_new_table',
'v201_logs_upgrades',
'v210_verify_form_status_upgrades',
'v213_delete_donation_meta'
'v213_delete_donation_meta',
'v213_rename_donation_meta_type'
);

foreach ( $upgrade_routines as $upgrade ) {
Expand Down
5 changes: 2 additions & 3 deletions includes/misc-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,8 @@ function __give_v20_bc_table_details( $type ) {
break;

case 'payment':
$table['name'] = $wpdb->paymentmeta;
$table['column']['id'] = 'payment_id';
$table['name'] = $wpdb->donationmeta;
$table['column']['id'] = Give()->payment_meta->get_meta_type() . '_id';
}

// Backward compatibility.
Expand Down Expand Up @@ -2112,7 +2112,6 @@ function give_get_title_prefixes( $form_id = 0 ) {
* @return bool
*/
function give_is_title_prefix_enabled( $form_id = 0, $status = '' ) {

if ( empty( $status ) ) {
$status = array( 'required', 'optional' );
} else {
Expand Down
9 changes: 5 additions & 4 deletions includes/payments/class-payment-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ public function get_earnings( $form_id = 0, $start_date = false, $end_date = fal
$earnings = 0;

if ( ! empty( $payments ) ) {
$query = "SELECT payment_id as id, meta_value as total
FROM {$wpdb->paymentmeta}
$donation_id_col = Give()->payment_meta->get_meta_type() . '_id';
$query = "SELECT {$donation_id_col} as id, meta_value as total
FROM {$wpdb->donationmeta}
WHERE meta_key='_give_payment_total'
AND payment_id IN ('". implode( '\',\'', $payments ) ."')";
AND {$donation_id_col} IN ('". implode( '\',\'', $payments ) ."')";

$payments = $wpdb->get_results($query, ARRAY_A);

Expand Down Expand Up @@ -287,4 +288,4 @@ public function get_best_selling( $number = 10 ) {
return $give_forms;
}

}
}
7 changes: 4 additions & 3 deletions includes/payments/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ function give_is_guest_payment( $payment_id ) {
function give_get_payment_user_id( $payment_id ) {
global $wpdb;
$paymentmeta_table = Give()->payment_meta->table_name;
$donationmeta_primary_key = Give()->payment_meta->get_meta_type() . '_id';

return (int) $wpdb->get_var(
$wpdb->prepare(
Expand All @@ -980,7 +981,7 @@ function give_get_payment_user_id( $payment_id ) {
WHERE id=(
SELECT meta_value
FROM $paymentmeta_table
WHERE payment_id=%s
WHERE {$donationmeta_primary_key}=%s
AND meta_key=%s
)
",
Expand Down Expand Up @@ -2003,14 +2004,14 @@ function give_is_donation_completed( $donation_id ) {
$wpdb->prepare(
"
SELECT meta_value
FROM {$wpdb->paymentmeta}
FROM {$wpdb->donationmeta}
WHERE EXISTS (
SELECT ID
FROM {$wpdb->posts}
WHERE post_status=%s
AND ID=%d
)
AND {$wpdb->paymentmeta}.meta_key=%s
AND {$wpdb->donationmeta}.meta_key=%s
",
'publish',
$donation_id,
Expand Down

0 comments on commit dc33641

Please sign in to comment.