Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/8f2d343c0634a602b670b7387aab33c1 to your computer and use it in GitHub Desktop.
Save helgatheviking/8f2d343c0634a602b670b7387aab33c1 to your computer and use it in GitHub Desktop.
Create dummy data for Charitable Recurring 1.0.7
<?php
/**
* Plugin Name: Charitable - Recurring Donations 1.0.7 CLI Data Generator
* Plugin URI: https://www.wpcharitable.com/extensions/charitable-recurring-donations/
* Description: Create dummy data for Recurring 1.0.7
* Version: 1.0.0.beta.1
* Author: Kathy Darling
* Author URI: https://www.kathyisawesome.com
* Requires at least: 5.0
* Tested up to: 5.1
*
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* This is a script that creates campaigns & recurring donations.
*
* It's designed to be called with wp-cli via the command line.
*
* wp charitable_recurring generate --recurring_donations=100 --campaigns=5
* wp charitable_recurring destroy
*/
function charitable_recurring_cli_data_generator_start(){
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'charitable_recurring', 'Charitable_Recurring_Data_Generator' );
}
}
add_action( 'charitable_recurring_start', 'charitable_recurring_cli_data_generator_start' );
class Charitable_Recurring_Data_Generator {
private $donations_count;
private $campaigns_count;
private $people = array(
'Natascha Romanov',
'Peggy Carter',
'Zoe Washburn',
'Quellcrist Falconer',
'Portia Lin',
'Sarah Connor',
'Arya Stark',
'Buffy Summers',
'Miranda Priestly',
'Olivia Benson',
'Cara Mason',
'Carol Danvers',
'Annalise Keating',
'Kara Thrace',
'Laura Roslin',
'Tami Taylor',
'Samantha Groves',
'Ellen Ripley',
'Sameen Shaw',
'Joan Watson',
'Diana Prince',
'Penelope Alvarez',
'Leia Organa',
'Dana Skully',
'Hermione Granger',
'Clarke Griffin',
'Lagertha Lothbrok',
'Viola Crawley',
'Ziva David',
'Jane Villanueva',
'Sarah Manning',
'Barbara Gordon',
'Christina Yang',
'Kathy Kane',
'Imperator Furiosa',
'Cosima Niehaus',
'Kalinda Sharma',
'Brienne Tarth',
'Naomi Nagata',
'Chrisjen Avasarala',
'Michael Burnham',
'Philippa Georgiou',
'Grace Choi',
'Kara Zor-El',
'Queen Hippolyta'
);
private $people_count;
private $goals = array( 0, 500, 1000, 2500, 10000, 50000, 100000, 200000, 500000, 1000000 );
private $goal_count;
private $end_date_offsets = array(
0,
'+1 day',
'now',
'-15 days',
'+15 days',
'+150 days',
'-150 days'
);
private $end_date_count;
private $start_date_offsets = array(
'-30 days',
'-60 days',
'-120 days',
'-180 days',
'-240 days',
'-300 days',
'-360 days',
'-420 days',
'-480 days',
'-540 days'
);
private $start_date_count;
private $suggested_donations = array(
array(),
array(
array( 'amount' => 10, 'description' => '' ),
array( 'amount' => 25, 'description' => '' ),
array( 'amount' => 50, 'description' => '' )
),
array(
array( 'amount' => 14, 'description' => 'Bronze' ),
array( 'amount' => 38, 'description' => 'Silver' ),
array( 'amount' => 79, 'description' => 'Gold' ),
array( 'amount' => 113, 'description' => 'Platinum' ),
array( 'amount' => 198, 'description' => 'Adamantium' )
)
);
private $recurring_modes = array(
'simple', 'advanced'
);
private $recurring_modes_count;
private $suggested_donations_count;
private $amounts = array(
2, 4, 5, 10, 15, 25, 40, 50, 75, 100, 120, 150, 200, 250, 500, 800, 1200
);
private $amounts_count;
private $users = array();
private $campaigns = array();
private $donations = array();
public function generate( $args = array(), $assoc_args = array() ) {
// Get arguments.
$this->donations_count = WP_CLI\Utils\get_flag_value( $assoc_args, 'recurring_donations', 100 );
$this->campaigns_count = WP_CLI\Utils\get_flag_value( $assoc_args, 'campaigns', 5 );
$this->people_count = count( $this->people );
$this->goal_count = count( $this->goals );
$this->end_date_count = count( $this->end_date_offsets );
$this->start_date_count = count( $this->start_date_offsets );
$this->suggested_donations_count = count( $this->suggested_donations );
$this->amounts_count = count( $this->amounts );
$this->recurring_modes_count = count( $this->recurring_modes );
$this->make_campaigns();
$this->make_recurring_donations();
}
private function make_campaigns() {
$campaigns = wp_count_posts( 'campaign' );
$published = $campaigns->publish;
for ( $i = 1; $i <= $this->campaigns_count; $i++ ) {
$this->campaigns[] = $this->make_campaign( $i + $published );
}
}
private function make_campaign( $i ) {
$user = $this->make_user();
$title = sprintf( 'Campaign %d', $i );
$campaign_id = wp_insert_post( array(
'post_title' => $title,
'post_type' => 'campaign',
'post_status' => 'publish',
'post_author' => $user[ 'ID' ],
'post_content' => sprintf( 'Content of campaign %d', $i )
) );
$meta = array(
'_campaign_description' => sprintf( 'Description of campaign %d', $i ),
'_campaign_goal' => $this->get_random_goal(),
'_campaign_end_date' => $this->get_random_end_date(),
'_campaign_suggested_donations' => $this->get_random_suggested_donations(),
'_campaign_allow_custom_donations' => 1,
'_campaign_recurring_donations' => $this->get_random_mode(),
'_campaign_recurring_billing_period' => $this->get_period(),
'_campaign_suggested_recurring_donations' => $this->get_random_suggested_donations(),
'_campaign_recurring_default_tab' => 'recurring'
);
foreach ( $meta as $key => $value ) {
add_post_meta( $campaign_id, $key, $value );
}
// Give output
WP_CLI::success( sprintf( 'New campaign: %s (%d)', $title, $campaign_id ) );
return array( 'ID' => $campaign_id, 'title' => $title );
}
private function make_recurring_donations() {
$donations = wp_count_posts( 'recurring_donation' );
$published = $donations->publish;
for ( $i = 1; $i <= $this->donations_count; $i++ ) {
$this->donations[] = $this->make_recurring_donation( $i + $published );
}
}
private function make_recurring_donation( $i ) {
$user = $this->make_user();
$campaign = $this->get_random_campaign();
$amount = $this->get_random_amount();
$period = 'month';
$start_date = $this->get_random_start_date();
$args = array(
'user' => array(
'email' => $user[ 'user_email' ],
'first_name' => $user[ 'first_name' ],
'last_name' => $user[ 'last_name' ],
'address' => '',
'address_2' => '',
'city' => '',
'state' => '',
'postcode' => '',
'country' => ''
),
'campaigns' => array(
array(
'campaign_id' => $campaign[ 'ID' ],
'campaign_name' => $campaign[ 'title' ],
'amount' => $amount,
'donation_period' => $period,
'donation_interval' => 1,
'donation_length' => ''
)
),
'status' => 'charitable-completed',
'gateway' => 'dummy',
'date_gmt' => $start_date,
'donation_period' => $period,
'donation_interval' => 1,
'donation_length' => ''
);
$donation_id = charitable_create_donation( $args );
$recurring_id = wp_get_post_parent_id( $donation_id );
// Destory the child processor class.
Charitable_Recurring_Donation_Processor::destroy();
// Give output
$name = $user[ 'first_name' ] . ' ' . $user[ 'last_name' ];
WP_CLI::success( sprintf( '$%s/%s Recurring donation #%s to campaign %s by %s', $amount, $period, $recurring_id, $campaign[ 'title' ], $name ) );
$this->make_renewal_donations( $recurring_id );
return $donation_id;
}
private function make_renewal_donations( $recurring_id ) {
$recurring_donation = charitable_get_donation( $recurring_id );
if( $recurring_donation && is_callable( array( $recurring_donation, 'create_renewal_donation' ) ) ) {
$recurring_donation->update_status( 'charitable-active' );
$current_timestamp = current_time( 'timestamp' );
$start_timestamp = get_post_time( 'U', true, $recurring_id );
$expiration_timestamp = strtotime( date( "Y-m-d H:i:s", $start_timestamp ) . " +1 month" );
while( $expiration_timestamp < $current_timestamp ) {
$expiration_date = date( 'Y-m-d H:i:s', $expiration_timestamp );
$renewal_id = $recurring_donation->create_renewal_donation( array( 'status' => 'charitable-completed', 'date_gmt' => $expiration_date ) );
if( ! is_wp_error( $renewal_id ) ) {
// Generate the recurring donationa and generate new expiration date.
$expiration_timestamp = strtotime( date( "Y-m-d H:i:s", $expiration_timestamp ) . " +1 month" );
// Give output
WP_CLI::success( sprintf( '$%s Renewal donation created for Recurring donation #%s', $recurring_donation->get_recurring_donation_amount(), $recurring_id ) );
}
}
}
}
private function make_user() {
$idx = rand( 0, $this->people_count - 1);
$name = $this->people[ $idx ];
if ( isset( $this->users[ $name ] ) ) {
return $this->users[ $name ];
}
list( $first_name, $last_name ) = explode( ' ', $name, 2 );
$user_email = str_replace( ' ', '', sprintf( '%s.%s@mailinator.com', $first_name, $last_name ) );
$user = array(
'display_name' => $name,
'first_name' => $first_name,
'last_name' => $last_name,
'user_email' => $user_email,
'user_pass' => 'password',
'user_login' => str_replace( '-', '', strtolower( sprintf( '%s-%s', $first_name, $last_name ) ) ),
'role' => 'donor'
);
$id = email_exists( $user_email );
if ( ! $id ) {
$id = wp_insert_user( $user );
}
$user[ 'ID' ] = $id;
$this->users[ $name ] = $user;
// Give output
WP_CLI::success( sprintf( 'New user: %s', $name ) );
return $user;
}
private function get_random_goal() {
$idx = rand( 0, $this->goal_count - 1 );
return $this->goals[ $idx ];
}
private function get_random_end_date() {
$idx = rand( 0, $this->end_date_count - 1 );
$end_date_offset = $this->end_date_offsets[ $idx ];
if ( $end_date_offset == 0 ) {
return 0;
}
return date( 'Y-m-d 00:00:00', strtotime( $end_date_offset ) );
}
private function get_random_start_date() {
$idx = rand( 0, $this->start_date_count - 1 );
$start_date_offset = $this->start_date_offsets[ $idx ];
if ( $start_date_offset == 0 ) {
return 0;
}
return date( 'Y-m-d 00:00:00', strtotime( $start_date_offset ) );
}
private function get_random_suggested_donations() {
$idx = rand( 0, $this->suggested_donations_count - 1 );
return $this->suggested_donations[ $idx ];
}
private function get_random_mode() {
$idx = rand( 0, $this->recurring_modes_count - 1 );
return $this->recurring_modes[ $idx ];
}
private function get_period() {
return 'month';
}
private function get_random_campaign() {
$idx = rand( 0, $this->campaigns_count - 1 );
return $this->campaigns[ $idx ];
}
private function get_random_amount() {
$idx = rand( 0, $this->amounts_count - 1 );
return $this->amounts[ $idx ];
}
public function destroy() {
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->posts" );
$wpdb->query( "DELETE FROM $wpdb->postmeta" );
$wpdb->query( "DELETE FROM $wpdb->users WHERE ID != 1" );
$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE user_id != 1" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}charitable_campaign_donations" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}charitable_donormeta" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}charitable_donors" );
delete_option('charitable_recurring_upgrade_log');
delete_option('charitable_recurring_version');
WP_CLI::success( 'All post, user, donor, donations, and meta data destroyed' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment