Commit 0ac6cffe authored by Michael Iseard's avatar Michael Iseard
Browse files

Yet more

parent 34520ea6
......@@ -2,7 +2,6 @@
namespace Kudos\Admin;
use Kudos\Entity\CampaignEntity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
......@@ -14,9 +13,7 @@ use Kudos\Service\LoggerService;
use Kudos\Service\MailerService;
use Kudos\Service\MapperService;
use Kudos\Service\MollieService;
use Kudos\Service\RestMapperService;
use Kudos\Service\TwigService;
use Kudos\Service\UpdateService;
use WP_REST_Server;
/**
......@@ -497,15 +494,6 @@ class Admin {
}
break;
case 'kudos_sync_campaigns':
$result = UpdateService::sync_campaign_labels();
if ( $result ) {
new AdminNotice( __( 'Campaign labels updated', 'kudos-donations' ) );
} else {
new AdminNotice( __( 'No additional campaigns found', 'kudos-donations' ), 'info' );
}
break;
case 'kudos_recreate_database':
$mapper = new MapperService();
foreach ([SubscriptionEntity::get_table_name(), TransactionEntity::get_table_name(), DonorEntity::get_table_name()] as $table) {
......
......@@ -4,7 +4,6 @@ namespace Kudos\Admin\Table;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Campaigns;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Service\MapperService;
use WP_List_Table;
......@@ -96,7 +95,7 @@ class CampaignsTable extends WP_List_Table {
foreach ( $campaigns as $key => $campaign ) {
$id = $campaign['id'];
$transactions = $mapper->get_all_by( [ 'campaign_label' => $id ] );
$transactions = $mapper->get_all_by( [ 'campaign_id' => $id ] );
$campaigns[ $key ]['date'] = date("r",hexdec(substr($id,3,8)));
$campaigns[ $key ]['transactions'] = 0;
......@@ -177,28 +176,6 @@ class CampaignsTable extends WP_List_Table {
];
}
/**
* Delete a campaign.
*
* @param string $label The campaign label.
*
* @return bool
* @since 2.0.4
*/
protected function delete_record( string $label ): bool {
$labels = Settings::get_setting( 'campaign_labels' );
$labels = array_filter(
$labels,
function ( $a ) use ( $label ) {
return ! in_array( $label, $a, true );
}
);
return Settings::update_setting( 'campaign_labels', $labels );
}
/**
* Render the bulk edit checkbox
*
......@@ -255,7 +232,7 @@ class CampaignsTable extends WP_List_Table {
return sprintf(
'<a href=%1$s>%2$s</a>',
sprintf( admin_url( 'admin.php?page=kudos-transactions&search-field=campaign_label&s=%s' ), rawurlencode( $item['id'] ) ),
sprintf( admin_url( 'admin.php?page=kudos-transactions&search-field=campaign_id&s=%s' ), rawurlencode( $item['id'] ) ),
strtoupper( $item['transactions'] )
);
......@@ -289,7 +266,7 @@ class CampaignsTable extends WP_List_Table {
protected function column_last_donation( array $item ): string {
return isset( $item['last_donation'] ) ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
strtotime( $item['last_donation'] ) ) : '';
strtotime( $item['last_donation'] ) ) : sprintf("<i>%s</i>", __('None yet', 'kudos-donations'));
}
}
......@@ -36,7 +36,7 @@ class TransactionsTable extends WP_List_Table {
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
'email' => __( 'Email', 'kudos-donations' ),
'campaign_label' => __( 'Campaign', 'kudos-donations' ),
'campaign_id' => __( 'Campaign', 'kudos-donations' ),
'order_id' => __( 'Order ID', 'kudos-donations' ),
];
......@@ -137,7 +137,7 @@ class TransactionsTable extends WP_List_Table {
'status' => __( 'Status', 'kudos-donations' ),
'order_id' => __( 'Order ID', 'kudos-donations' ),
'transaction_id' => __( 'Transaction Id', 'kudos-donations' ),
'campaign_label' => __( 'Campaign', 'kudos-donations' ),
'campaign_id' => __( 'Campaign', 'kudos-donations' ),
];
}
......@@ -172,8 +172,8 @@ class TransactionsTable extends WP_List_Table {
'value',
false,
],
'campaign_label' => [
'campaign_label',
'campaign_id' => [
'campaign_id',
false,
],
];
......@@ -446,11 +446,11 @@ class TransactionsTable extends WP_List_Table {
* @return string
* @since 2.0.2
*/
protected function column_campaign_label( array $item ): string {
protected function column_campaign_id( array $item ): string {
$campaign_name = $this->campaigns->get_campaign(
$item['campaign_label'] )['name']
?? $item['campaign_label'];
$item['campaign_id'] )['name']
?? $item['campaign_id'];
return sprintf(
'<a href=%1$s>%2$s</a>',
......
......@@ -177,15 +177,6 @@ $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $default_tab;
<input class="button-secondary confirm" type='submit' value='Delete all subscriptions'>
</form>
<hr/>
<p>This will add missing campaign labels from transactions</p>
<form action="<?php echo esc_url( $url ); ?>" method='post'>
<?php wp_nonce_field( 'kudos_sync_campaigns', '_wpnonce' ); ?>
<input type='hidden' name='kudos_action' value='kudos_sync_campaigns'>
<input class="button-secondary confirm" type='submit' value='Sync campaign labels'>
</form>
<hr />
<p>This will <strong>delete all Kudos data</strong> and recreate the database</p>
......
......@@ -55,7 +55,7 @@ class CampaignEntity extends AbstractEntity {
$mapper = new MapperService( TransactionEntity::class );
return $mapper->get_all_by( [ 'campaign_label' => $this->slug ] );
return $mapper->get_all_by( [ 'campaign' => $this->slug ] );
}
......
......@@ -77,7 +77,7 @@ class TransactionEntity extends AbstractEntity {
*
* @var string
*/
public $campaign_label;
public $campaign_id;
/**
* Refunds serialized array
*
......
......@@ -245,7 +245,7 @@ class Front {
$city = isset( $form['city'] ) ? sanitize_text_field( $form['city'] ) : null;
$country = isset( $form['country'] ) ? sanitize_text_field( $form['country'] ) : null;
$redirect_url = isset( $form['return_url'] ) ? sanitize_text_field( $form['return_url'] ) : null;
$campaign_label = isset( $form['campaign_label'] ) ? sanitize_text_field( $form['campaign_label'] ) : null;
$campaign_id = isset( $form['campaign_id'] ) ? sanitize_text_field( $form['campaign_id'] ) : null;
$mollie = MollieService::factory();
$mapper = new MapperService( DonorEntity::class );
......@@ -285,7 +285,7 @@ class Front {
$payment_frequency,
$recurring_length,
$redirect_url,
$campaign_label,
$campaign_id,
$name,
$email,
$customer_id
......@@ -330,7 +330,7 @@ class Front {
'amount_type' => 'open',
'donation_type' => 'both',
'fixed_amounts' => '5, 10, 20, 50',
'campaign' => 'default',
'campaign_id' => 'default',
'alignment' => 'none',
],
$atts,
......@@ -352,7 +352,7 @@ class Front {
'type' => 'string',
'default' => __( 'Donate now', 'kudos-donations' ),
],
'campaign' => [
'campaign_id' => [
'type' => 'string',
'default' => 'default',
],
......@@ -399,8 +399,8 @@ class Front {
// Set campaign according to atts and if none found then set as default
$campaigns = new Campaigns();
if(!empty($atts['campaign'])) {
$campaign = $campaigns->get_campaign($atts['campaign']);
if(!empty($atts['campaign_id'])) {
$campaign = $campaigns->get_campaign($atts['campaign_id']);
}
if(empty($campaign)) {
......@@ -539,7 +539,11 @@ class Front {
return false;
}
$donor = $transaction->get_donor();
/** @var DonorEntity $donor */
$donor = $transaction->get_donor();
$campaigns = new Campaigns();
$campaign = $campaigns->get_campaign($transaction->campaign_id);
$campaign_name = !empty($campaign['name']) ? $campaign['name'] : '';
switch ( $transaction->status ) {
case 'paid':
......@@ -548,6 +552,7 @@ class Front {
2 ),
'{{name}}' => $donor->name,
'{{email}}' => $donor->email,
'{{campaign}}' => $campaign_name
];
$atts['modal_title'] = strtr( Settings::get_setting( 'return_message_title' ), $vars );
$atts['modal_text'] = strtr( Settings::get_setting( 'return_message_text' ), $vars );
......
......@@ -59,7 +59,7 @@ class KudosButton {
*
* @var string
*/
private $campaign_label;
private $campaign_id;
/**
* @var string
*/
......@@ -138,7 +138,7 @@ class KudosButton {
'address_enabled' => $this->address_enabled,
'address_required' => $this->address_required,
'donation_type' => $this->donation_type,
'campaign_label' => $this->campaign_label,
'campaign_id' => $this->campaign_id,
'payment_by' => __( 'Secure payment by', 'kudos-donations' ),
];
......
......@@ -39,13 +39,15 @@ class ActivatorService {
$twig = new TwigService();
$twig->init();
self::create_donors_table();
self::create_transactions_table();
self::create_subscriptions_table();
self::set_defaults();
if ( $old_version ) {
if ( version_compare( $old_version, '2.3.0', '<' ) ) {
global $wpdb;
$table = TransactionEntity::get_table_name();
$wpdb->query("ALTER TABLE $table RENAME COLUMN `campaign_label` TO `campaign_id`");
Settings::update_setting('show_intro', 1);
}
if ( version_compare( $old_version, '2.2.0', '<' ) ) {
$link = Settings::get_setting('privacy_link');
Settings::remove_setting('subscription_enabled');
......@@ -61,16 +63,13 @@ class ActivatorService {
Settings::remove_setting('action_scheduler');
}
if ( version_compare( $old_version, '2.0.4', '<' ) ) {
$logger->info( 'Upgrading to version 2.0.4', [ 'previous_version' => $old_version ] );
$result = UpdateService::sync_campaign_labels();
if ( $result ) {
$logger->info( 'Updated campaign labels from transactions' );
}
}
}
self::create_donors_table();
self::create_transactions_table();
self::create_subscriptions_table();
self::set_defaults();
$logger->info( 'Kudos Donations plugin activated' );
}
......@@ -134,7 +133,7 @@ class ActivatorService {
transaction_id VARCHAR(255),
subscription_id VARCHAR(255),
refunds BLOB DEFAULT NULL,
campaign_label VARCHAR(255),
campaign VARCHAR(255),
secret VARCHAR(255),
PRIMARY KEY (id)
) $charset_collate;";
......
......@@ -73,7 +73,7 @@ class MollieService extends AbstractService {
* @param string $interval Interval of payment (oneoff, first, recurring).
* @param string $years Number of years for subscription.
* @param string $redirect_url URL to redirect customer to on payment completion.
* @param string|null $campaign_label Campaign name to associate payment to.
* @param string|null $campaign_id Campaign name to associate payment to.
* @param string|null $name Name of donor.
* @param string|null $email Email of donor.
* @param string|null $customer_id Mollie customer id.
......@@ -86,7 +86,7 @@ class MollieService extends AbstractService {
string $interval,
string $years,
string $redirect_url,
string $campaign_label = null,
string $campaign_id = null,
string $name = null,
string $email = null,
string $customer_id = null
......@@ -122,7 +122,7 @@ class MollieService extends AbstractService {
'years' => $years,
'email' => $email,
'name' => $name,
'campaign_label' => $campaign_label,
'campaign_id' => $campaign_id,
],
];
......@@ -143,7 +143,7 @@ class MollieService extends AbstractService {
'status' => $payment->status,
'mode' => $payment->mode,
'sequence_type' => $payment->sequenceType,
'campaign_label' => $campaign_label,
'campaign_id' => $campaign_id,
]
);
......@@ -500,11 +500,11 @@ class MollieService extends AbstractService {
try {
$customer = $this->mollie_api->customers->get( $customer_id );
$subscription_meta = $customer->getSubscription( $subscription_id )->metadata;
if ( array_key_exists( 'campaign_label', $subscription_meta ) ) {
$campaign_label = $subscription_meta->campaign_label;
if ( array_key_exists( 'campaign_id', $subscription_meta ) ) {
$campaign_id = $subscription_meta->campaign_id;
$transaction->set_fields(
[
'campaign_label' => $campaign_label,
'campaign_id' => $campaign_id,
]
);
} else {
......@@ -607,7 +607,7 @@ class MollieService extends AbstractService {
$transaction->order_id
),
'metadata' => [
'campaign_label' => $transaction->campaign_label,
'campaign_id' => $transaction->campaign_id,
],
];
......
<?php
namespace Kudos\Service;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Settings;
class UpdateService {
/**
* Adds campaign labels on transactions to the settings array
*
* @return bool
* @since 2.0.4
*/
public static function sync_campaign_labels(): bool {
$mapper = new MapperService( TransactionEntity::class );
// Get transactions and stop if none found.
$transactions = $mapper->get_all_by( [ "campaign_label" ] );
if ( ! $transactions ) {
return false;
}
$transactions = array_filter($transactions, function ($transaction) {
return ! empty($transaction->campaign_label);
});
// Sort transactions by date, ensures oldest dates used.
usort(
$transactions,
function ( $b, $a ) {
return strtotime( $a->created ) <=> strtotime( $b->created );
}
);
// Create array of labels.
$transaction_labels = [];
foreach ( $transactions as $key => $transaction ) {
$label = strtolower( $transaction->campaign_label );
$transaction_labels[ $label ] = [
'date' => $transaction->created,
'label' => $label,
];
}
// Merge arrays and ensure no duplicates.
$current_labels = ! empty( Settings::get_setting( 'campaign_labels' ) ) ? Settings::get_setting( 'campaign_labels' ) : [];
$labels = array_merge( $transaction_labels, $current_labels );
$labels = array_values( array_column( $labels, null, 'label' ) );
// Update labels in settings with new, merged values.
return Settings::update_setting( 'campaign_labels', $labels );
}
}
......@@ -59,18 +59,6 @@ services:
depends_on:
- db
ngrok:
image: wernight/ngrok
container_name: ngrok-kudos
restart: always
ports:
- "4040:4040"
depends_on:
- wordpress
environment:
NGROK_PORT: wordpress:80
NGROK_REGION: eu
redis:
image: redis:alpine
container_name: redis-kudos
......
......@@ -10,7 +10,7 @@
* Plugin Name: Kudos Donations
* Plugin URI: https://gitlab.iseard.media/michael/kudos-donations
* Description: Add a donation button to any page on your website. Easy & fast setup. Works with Mollie payments.
* Version: 2.2.0
* Version: 2.3.0
* Author: Iseard Media
* Author URI: https://iseard.media
* Requires at least: 5.4
......@@ -41,7 +41,7 @@ if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'KUDOS_VERSION', '2.2.0' );
define( 'KUDOS_VERSION', '2.3.0' );
define( 'KUDOS_PLUGIN_DIR', dirname( __FILE__ ) );
define( 'KUDOS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'KUDOS_STORAGE_DIR', wp_upload_dir()['basedir'] . '/kudos-donations/' );
......
{
"name": "kudos-donations",
"version": "2.2.0",
"version": "2.3.0",
"description": " Add a donation button to any page on your website. Easy & fast setup. Works with Mollie payments.",
"homepage": "https://gitlab.iseard.media/michael/kudos-donations",
"scripts": {
......
import live from "../../../img/guide-live.png"
import intro from "../../../img/guide-intro.png"
const { __ } = wp.i18n;
const { Guide } = wp.components;
const { useState } = wp.element;
const IntroGuide = ({ show, updateSetting } ) => {
const [ isOpen, setIsOpen ] = useState( show );
if ( ! isOpen ) {
return null;
}
const closeModal = () => {
setIsOpen(false)
updateSetting('_kudos_show_intro', false)
}
return (
<Guide
className={"kudos-welcome-guide"}
onFinish={ () => closeModal() }
pages={ [
{
image: <img src={intro} />,
content:
<div className="kd-p-4">
<h1 className="kd-leading-normal">Welcome to Kudos Donations</h1>
<p>It is easy to get started with Kudos Donations. Complete these simple steps to get your donations started.</p>
</div>
,
},
{
image: <img src={intro} />,
content:
<div className="kd-p-4">
<h1 className="kd-leading-normal">Connect with Mollie</h1>
<p>Log-in to your Mollie account and grab your <a target="_blank" href="https://mollie.com/dashboard/developers/api-keys">api keys</a>.</p>
<p>Make sure you get both your <strong>test</strong> and <strong>live</strong> api keys. Then enter them in the relevant fields.</p>
</div>
,
},
{
image: <img src={intro} />,
content:
<div className="kd-p-4">
<h1 className="kd-leading-normal">Set up a campaign</h1>
<p>Visit the <strong>Campaigns</strong> tab and either create a new campaign or edit the default one. </p>
<p>Don't forget to click <strong>Copy shortcode</strong> at the bottom of your campaign.</p>
</div>
,
},
{
image: <img src={intro} />,
content:
<div className="kd-p-4">
<h1 className="kd-leading-normal">Place a button</h1>
<p>Using the Kudos Donations block or shortcode, place the button anywhere on your website. If using the block, you will need to select the campaign you want it to use.</p>
</div>
,
},
{
image: <img src={live} />,
content:
<div className="kd-p-4">
<h1 className="kd-leading-normal">Test and go Live</h1>
<p>With the API mode still on <strong>Test</strong> make a payment using your button. If it all looks good then you can switch to <strong>Live</strong>.</p>
</div>
,
},
] }
/>
);
};
export { IntroGuide };
......@@ -38,7 +38,7 @@ const AddCampaignPanel = ({settings, showNotice, updateSetting, handleInputChang
};
return (
<div>
<>
<PanelBody
title={ __( 'Add a campaign', 'kudos-donations' ) }
opened={ true }
......@@ -51,14 +51,11 @@ const AddCampaignPanel = ({settings, showNotice, updateSetting, handleInputChang
) }
help={__("Give your campaign a unique name to identify it.", 'kudos-donations')}
id={'kudos_new_campaign'}
className={'kd-inline'}
type={ 'text' }
value={ addFormValue }
onChange={ (newValue) => updateValue( newValue ) }
/>
<br/>
<Button
isPrimary
isSmall
......@@ -88,7 +85,7 @@ const AddCampaignPanel = ({settings, showNotice, updateSetting, handleInputChang
)
})}
</div>
</>
);
};
......
......@@ -43,7 +43,6 @@ const CampaignPanel = ( { settings, campaign, showNotice, updateSetting, handleI
<TextControl
label={ __( 'Header', 'kudos-donations' ) }
id={ 'modal_title' + '-' + campaign.name }
className={ 'kd-inline' }
type={ 'text' }
value={ campaign.modal_title || '' }
onChange={ (value) => {
......@@ -55,7 +54,6 @@ const CampaignPanel = ( { settings, campaign, showNotice, updateSetting, handleI
<TextControl
label={ __( 'Welcome text', 'kudos-donations' ) }
id={ 'welcome_text' + '-' + campaign.name }
className={ 'kd-inline' }
type={ 'text' }
value={ campaign.welcome_text || '' }
onChange={ (value) => {
......@@ -117,37 +115,41 @@ const CampaignPanel = ( { settings, campaign, showNotice, updateSetting, handleI
</BaseControl>
<RadioControl
label={ __( 'Amount type', 'kudos-donations' ) }
selected={ campaign.amount_type || 'both' }
options={ [
{ label: __('Open', 'kudos-donations'), value: 'open' },
{ label: __('Fixed', 'kudos-donations'), value: 'fixed' },
{ label: __('Both', 'kudos-donations'), value: 'both' },
] }
onChange={ (value) => {