Commit daf3025e authored by Michael Iseard's avatar Michael Iseard
Browse files

Code cleanup

parent 7275861f
......@@ -113,7 +113,8 @@ class Admin {
}
);
add_action( "admin_print_scripts-{$transactions_page_hook_suffix}", [ $this, 'kudos_transactions_page_assets' ] );
add_action( "admin_print_scripts-{$transactions_page_hook_suffix}",
[ $this, 'kudos_transactions_page_assets' ] );
$subscriptions_page_hook_suffix = add_submenu_page(
'kudos-settings',
......@@ -127,7 +128,8 @@ class Admin {
}
);
add_action( "admin_print_scripts-{$subscriptions_page_hook_suffix}", [ $this, 'kudos_subscriptions_page_assets' ] );
add_action( "admin_print_scripts-{$subscriptions_page_hook_suffix}",
[ $this, 'kudos_subscriptions_page_assets' ] );
$donors_page_hook_suffix = add_submenu_page(
'kudos-settings',
......@@ -169,22 +171,23 @@ class Admin {
require_once KUDOS_PLUGIN_DIR . '/app/Admin/partials/kudos-admin-debug.php';
}
);
add_action( "admin_print_scripts-{$debug_page_hook_suffix}", function() {
?>
<script>
document.addEventListener("DOMContentLoaded", function () {
let buttons = document.querySelectorAll('input[type="submit"].confirm');
for(let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function (e) {
if ( ! confirm( '<?php _e('Are you sure?', 'kudos-donations') ?>' ) ) {
e.preventDefault();
}
})
}
});
</script>
<?php
} );
add_action( "admin_print_scripts-{$debug_page_hook_suffix}",
function () {
?>
<script>
document.addEventListener("DOMContentLoaded", function () {
let buttons = document.querySelectorAll('input[type="submit"].confirm')
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function (e) {
if (!confirm('<?php _e( 'Are you sure?', 'kudos-donations' ) ?>')) {
e.preventDefault()
}
})
}
})
</script>
<?php
} );
}
}
......@@ -431,8 +434,14 @@ class Admin {
case 'kudos_recreate_database':
$mapper = new MapperService();
foreach ([SubscriptionEntity::get_table_name(), TransactionEntity::get_table_name(), DonorEntity::get_table_name()] as $table) {
$mapper->delete_table($table);
foreach (
[
SubscriptionEntity::get_table_name(),
TransactionEntity::get_table_name(),
DonorEntity::get_table_name(),
] as $table
) {
$mapper->delete_table( $table );
}
ActivatorService::activate();
new AdminNotice( __( 'Database re-created', 'kudos-donations' ) );
......
......@@ -30,8 +30,8 @@ class CampaignsTable extends WP_List_Table {
*/
public function __construct() {
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = $this->mapper->get_table_name();
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = $this->mapper->get_table_name();
$this->campaigns = new Campaigns();
$this->search_columns = [
......@@ -39,7 +39,7 @@ class CampaignsTable extends WP_List_Table {
];
$this->export_columns = [
'name' => __( 'Email', 'kudos-donations' ),
'name' => __( 'Email', 'kudos-donations' ),
'transactions' => __( 'Street', 'kudos-donations' ),
'total' => __( 'Total', 'kudos-donations' ),
];
......@@ -86,8 +86,9 @@ class CampaignsTable extends WP_List_Table {
// Add search query if exist.
if ( $search ) {
$campaigns = array_filter( $campaigns, function ( $value ) use ( $search ) {
return (strtolower($value[ $search['field'] ]) == strtolower($search['term']));
$campaigns = array_filter( $campaigns,
function ( $value ) use ( $search ) {
return ( strtolower( $value[ $search['field'] ] ) == strtolower( $search['term'] ) );
}
);
}
......@@ -97,7 +98,7 @@ class CampaignsTable extends WP_List_Table {
$transactions = $mapper->get_all_by( [ 'campaign_id' => $id ] );
$campaigns[ $key ]['date'] = date("r",hexdec(substr($id,3,8)));
$campaigns[ $key ]['date'] = date( "r", hexdec( substr( $id, 3, 8 ) ) );
$campaigns[ $key ]['transactions'] = 0;
$campaigns[ $key ]['total'] = 0;
if ( $transactions ) {
......@@ -131,8 +132,8 @@ class CampaignsTable extends WP_List_Table {
*/
public function column_names(): array {
return [
'date' => __('Date', 'kudos-donations'),
'name' => __( 'Name', 'kudos-donations' ),
'date' => __( 'Date', 'kudos-donations' ),
'name' => __( 'Name', 'kudos-donations' ),
'transactions' => __( 'Transactions', 'kudos-donations' ),
'total' => __( 'Total', 'kudos-donations' ),
'last_donation' => __( 'Last Donation', 'kudos-donations' ),
......@@ -232,7 +233,8 @@ 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_id&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'] )
);
......@@ -266,7 +268,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'] ) ) : sprintf("<i>%s</i>", __('None yet', 'kudos-donations'));
strtotime( $item['last_donation'] ) ) : sprintf( "<i>%s</i>", __( 'None yet', 'kudos-donations' ) );
}
}
......@@ -77,10 +77,10 @@ class DonorsTable extends WP_List_Table {
*/
public function fetch_table_data(): array {
$search = $this->get_search_data();
$search_field = isset($search['field']) ? $search['field'] : null;
$search_term = isset($search['term']) ? $search['term'] : null;
$donors = $this->mapper->get_all_by( [ $search_field => $search_term ], 'AND' );
$search = $this->get_search_data();
$search_field = isset( $search['field'] ) ? $search['field'] : null;
$search_term = isset( $search['term'] ) ? $search['term'] : null;
$donors = $this->mapper->get_all_by( [ $search_field => $search_term ], 'AND' );
return array_map( function ( $donor ) {
return $donor->to_array();
......
......@@ -142,7 +142,7 @@ trait TableTrait {
}
$input_id = $input_id . '-search-input';
$search_data = $this->get_search_data();
$search_data = $this->get_search_data();
$search_field = $search_data ? $search_data['field'] : null;
?>
......@@ -169,7 +169,8 @@ trait TableTrait {
$search = null;
if ( isset( $_REQUEST['s'] ) && isset( $_REQUEST['search-field'] ) && array_key_exists( $_REQUEST['search-field'], $this->column_names() ) ) {
if ( isset( $_REQUEST['s'] ) && isset( $_REQUEST['search-field'] ) && array_key_exists( $_REQUEST['search-field'],
$this->column_names() ) ) {
$search['term'] = strtolower( esc_attr( wp_unslash( $_REQUEST['s'] ) ) );
$search['field'] = esc_attr( wp_unslash( $_REQUEST['search-field'] ) );
}
......
......@@ -29,15 +29,15 @@ class TransactionsTable extends WP_List_Table {
*/
public function __construct() {
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = TransactionEntity::get_table_name();
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = TransactionEntity::get_table_name();
$this->campaigns = new Campaigns();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
'email' => __( 'Email', 'kudos-donations' ),
'campaign_id' => __( 'Campaign', 'kudos-donations' ),
'order_id' => __( 'Order ID', 'kudos-donations' ),
'name' => __( 'Name', 'kudos-donations' ),
'email' => __( 'Email', 'kudos-donations' ),
'campaign_id' => __( 'Campaign', 'kudos-donations' ),
'order_id' => __( 'Order ID', 'kudos-donations' ),
];
$this->export_columns = [
......@@ -164,11 +164,11 @@ class TransactionsTable extends WP_List_Table {
public function get_sortable_columns(): array {
return [
'created' => [
'created' => [
'created',
false,
],
'value' => [
'value' => [
'value',
false,
],
......@@ -449,8 +449,8 @@ class TransactionsTable extends WP_List_Table {
protected function column_campaign_id( array $item ): string {
$campaign_name = $this->campaigns->get_campaign(
$item['campaign_id'] )['name']
?? $item['campaign_id'];
$item['campaign_id'] )['name']
?? $item['campaign_id'];
return sprintf(
'<a href=%1$s>%2$s</a>',
......
......@@ -177,7 +177,7 @@ $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $default_tab;
<input class="button-secondary confirm" type='submit' value='Delete all subscriptions'>
</form>
<hr />
<hr/>
<p>This will <strong>delete all Kudos data</strong> and recreate the database</p>
<form action="<?php echo esc_url( $url ); ?>" method='post'>
......@@ -199,7 +199,7 @@ $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $default_tab;
$subscriptions = $kudos_mollie->get_subscriptions( $donor->customer_id );
if ( !($subscriptions instanceof SubscriptionCollection) || !$subscriptions->count() ) {
if ( ! ( $subscriptions instanceof SubscriptionCollection ) || ! $subscriptions->count() ) {
continue;
}
?>
......
......@@ -155,7 +155,7 @@ abstract class AbstractEntity implements EntityInterface {
$this->secret = bin2hex( random_bytes( 10 ) );
}
Utils::schedule_action(strtotime( $timeout ), $table . '_remove_secret_action', [ $this->secret ], true );
Utils::schedule_action( strtotime( $timeout ), $table . '_remove_secret_action', [ $this->secret ], true );
return wp_hash_password( $this->secret );
......
......@@ -135,9 +135,9 @@ class Front {
$handle,
'kudos',
[
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'_wpnonce' => wp_create_nonce('wp_rest'),
'createPaymentUrl' => rest_url( RestService::NAMESPACE . '/mollie/payment/create' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'_wpnonce' => wp_create_nonce( 'wp_rest' ),
'createPaymentUrl' => rest_url( RestService::NAMESPACE . '/mollie/payment/create' ),
]
);
wp_set_script_translations( $handle, 'kudos-donations', KUDOS_PLUGIN_DIR . '/languages' );
......@@ -197,15 +197,15 @@ class Front {
$atts = shortcode_atts(
[
'button_label' => __( 'Donate now', 'kudos-donations' ),
'modal_title' => __( 'Support us!', 'kudos-donations' ),
'welcome_text' => __( 'Your support is greatly appreciated and will help to keep us going.',
'button_label' => __( 'Donate now', 'kudos-donations' ),
'modal_title' => __( 'Support us!', 'kudos-donations' ),
'welcome_text' => __( 'Your support is greatly appreciated and will help to keep us going.',
'kudos-donations' ),
'amount_type' => 'open',
'donation_type' => 'both',
'fixed_amounts' => '5, 10, 20, 50',
'campaign_id' => 'default',
'alignment' => 'none',
'amount_type' => 'open',
'donation_type' => 'both',
'fixed_amounts' => '5, 10, 20, 50',
'campaign_id' => 'default',
'alignment' => 'none',
],
$atts,
'kudos'
......@@ -222,19 +222,19 @@ class Front {
'editor_script' => $this->plugin_name . '-button-block',
'render_callback' => [ $this, 'kudos_render_callback' ],
'attributes' => [
'button_label' => [
'button_label' => [
'type' => 'string',
'default' => __( 'Donate now', 'kudos-donations' ),
],
'campaign_id' => [
'campaign_id' => [
'type' => 'string',
'default' => 'default',
],
'alignment' => [
'alignment' => [
'type' => 'string',
'default' => 'none',
],
'id' => [
'id' => [
'type' => 'string',
'source' => 'attribute',
'default' => 'kudos_modal-1',
......@@ -258,7 +258,7 @@ class Front {
// Check if ready to go and if not return error messages
$status = self::ready();
if ( isset($status['error_messages']) ) {
if ( isset( $status['error_messages'] ) ) {
if ( is_user_logged_in() && ! is_admin() ) {
$out = '';
foreach ( $status['error_messages'] as $message ) {
......@@ -273,16 +273,16 @@ class Front {
// Set campaign according to atts and if none found then set as default
$campaigns = new Campaigns();
if(!empty($atts['campaign_id'])) {
$campaign = $campaigns->get_campaign($atts['campaign_id']);
if ( ! empty( $atts['campaign_id'] ) ) {
$campaign = $campaigns->get_campaign( $atts['campaign_id'] );
}
if(empty($campaign)) {
$campaign = $campaigns->get_campaign('default');
if ( empty( $campaign ) ) {
$campaign = $campaigns->get_campaign( 'default' );
}
// Add campaign config to atts
$atts = wp_parse_args($campaign, $atts);
$atts = wp_parse_args( $campaign, $atts );
// Create button and modal.
$button = new KudosButton( $atts );
......@@ -291,7 +291,7 @@ class Front {
// Return only if modal and button not empty.
if ( ! empty( $modal ) && ! empty( $button ) ) {
return $button->get_button( false ) . $modal;
return $button->get_button() . $modal;
}
return null;
......@@ -313,8 +313,12 @@ class Front {
$return = [];
if(!$api_connected || !$api_key) $return['error_messages'][] = __( 'Mollie not connected', 'kudos-donations' );
if(!$campaigns) $return['error_messages'][] = __( 'No campaigns found', 'kudos-donations' );
if ( ! $api_connected || ! $api_key ) {
$return['error_messages'][] = __( 'Mollie not connected', 'kudos-donations' );
}
if ( ! $campaigns ) {
$return['error_messages'][] = __( 'No campaigns found', 'kudos-donations' );
}
return $return;
......@@ -416,19 +420,19 @@ class Front {
}
/** @var DonorEntity $donor */
$donor = $transaction->get_donor();
$campaigns = new Campaigns();
$campaign = $campaigns->get_campaign($transaction->campaign_id);
$campaign_name = !empty($campaign['name']) ? $campaign['name'] : '';
$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':
$vars = [
'{{value}}' => ( ! empty( $transaction->currency ) ? html_entity_decode( Utils::get_currency_symbol( $transaction->currency ) ) : '' ) . number_format_i18n( $transaction->value,
'{{value}}' => ( ! empty( $transaction->currency ) ? html_entity_decode( Utils::get_currency_symbol( $transaction->currency ) ) : '' ) . number_format_i18n( $transaction->value,
2 ),
'{{name}}' => $donor->name,
'{{email}}' => $donor->email,
'{{campaign}}' => $campaign_name
'{{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 );
......
......@@ -25,7 +25,7 @@ class KudosButton {
*/
private $button_label;
/**
* Button id
* Modal id
*
* @var string
*/
......@@ -82,8 +82,8 @@ class KudosButton {
*/
public function __construct( array $atts ) {
$this->twig = TwigService::factory();
$this->target_id = uniqid('kudos_modal-');
$this->twig = TwigService::factory();
$this->target_id = uniqid( 'kudos_modal-' );
foreach ( $atts as $property => $value ) {
if ( property_exists( static::class, $property ) ) {
......@@ -96,12 +96,10 @@ class KudosButton {
/**
* Get the button markup
*
* @param bool $echo Whether to echo result or not.
*
* @return string|void
* @since 1.0.0
*/
public function get_button( bool $echo = true ): string {
public function get_button(): string {
$data = [
'alignment' => $this->alignment,
......@@ -109,13 +107,7 @@ class KudosButton {
'target' => $this->target_id,
];
$out = $this->twig->render( 'public/kudos.button.html.twig', $data );
if ( $echo ) {
echo $out;
}
return $out;
return $this->twig->render( 'public/kudos.button.html.twig', $data );
}
/**
......@@ -126,20 +118,21 @@ class KudosButton {
*/
public function get_donate_modal(): string {
$modal = new KudosModal($this->target_id);
$allowed_types = ['fixed', 'open', 'both'];
$modal = new KudosModal( $this->target_id );
$allowed_types = [ 'fixed', 'open', 'both' ];
$data = [
'modal_id' => $this->target_id,
'modal_title' => $this->modal_title,
'modal_text' => $this->welcome_text,
'amount_type' => in_array( $this->amount_type, $allowed_types, true ) ? $this->amount_type : 'open',
'fixed_amounts' => array_slice(explode( ',', $this->fixed_amounts ),0, 4),
'address_enabled' => $this->address_enabled,
'address_required' => $this->address_required,
'donation_type' => $this->donation_type,
'campaign_id' => $this->campaign_id,
'payment_by' => __( 'Secure payment by', 'kudos-donations' ),
'modal_id' => $this->target_id,
'modal_title' => $this->modal_title,
'modal_text' => $this->welcome_text,
'amount_type' => in_array( $this->amount_type, $allowed_types, true ) ? $this->amount_type : 'open',
'fixed_amounts' => array_slice( explode( ',', $this->fixed_amounts ), 0, 4 ),
'address_enabled' => $this->address_enabled,
'address_required' => $this->address_required,
'donation_type' => $this->donation_type,
'campaign_id' => $this->campaign_id,
'payment_by' => __( 'Secure payment by', 'kudos-donations' ),
];
return $modal->get_donate_modal( $data );
......
......@@ -20,19 +20,19 @@ class KudosModal {
*
* @var string
*/
private $id;
private $modal_id;
/**
* Kudos_Modal constructor.
* KudosModal constructor.
*
* @param string|null $id string
* @param string|null $modal_id string
*
* @since 1.0.0
*/
public function __construct( string $id = null ) {
public function __construct( string $modal_id = null ) {
$this->twig = TwigService::factory();
$this->id = $id ?? uniqid();
$this->twig = TwigService::factory();
$this->modal_id = $modal_id ?? uniqid();
}
......@@ -44,29 +44,15 @@ class KudosModal {
* @return string|bool
* @since 1.0.0
*/
public function get_message_modal( array $atts ) {
public function get_message_modal( array $atts ): string {
$data = [
'modal_id' => 'kudos_modal-message-' . $this->id,
'modal_id' => 'kudos_modal-message-' . $this->modal_id,
'modal_title' => isset( $atts['modal_title'] ) ? $atts['modal_title'] : '',
'modal_text' => isset( $atts['modal_text'] ) ? $atts['modal_text'] : '',
];
return $this->render_modal( '/public/modal/message.modal.html.twig', $data );
}
/**
* Renders the modal using twig
*
* @param string $template Template file to use.
* @param array $data Array of data for template.
*
* @return bool|string
*/
private function render_modal( string $template, array $data ): string {
return $this->twig->render( $template, $data );
return $this->twig->render( '/public/modal/message.modal.html.twig', $data );
}
......@@ -74,31 +60,20 @@ class KudosModal {
* Get the donate modal markup
*
* @param array $data Array of data for template.
* @param bool $echo Whether to echo result or not.
*
* @return string|void
* @since 1.0.0
*/
public function get_donate_modal( array $data, bool $echo = false ): string {
public function get_donate_modal( array $data ): string {
$data = array_merge(
$data,
$data = array_merge( $data,
[
'return_url' => Utils::get_return_url(),
// Global settings.
'vendor' => Settings::get_setting( 'payment_vendor' ),
'terms_link' => Settings::get_setting( 'terms_link' ),
]
);
$out = $this->render_modal( '/public/modal/donate.modal.html.twig', $data );
if ( $echo ) {
echo $out;
}
] );
return $out;
return $this->twig->render( '/public/modal/donate.modal.html.twig', $data );
}
}
\ No newline at end of file
......@@ -11,10 +11,50 @@ class Campaigns {
public function __construct() {
$this->campaigns = Settings::get_setting('campaigns');
$this->campaigns = Settings::get_setting( 'campaigns' );
}
/**
* Sanitize the various setting fields in the donation form array
*
* @param $campaigns
*
* @return array
* @since 2.3.0
*/
public static function sanitize_campaigns( $campaigns ): array {
//Define the array for the updated options
$output = [];
// Loop through each of the options sanitizing the data
foreach ( $campaigns as $key => $form ) {
if ( ! array_search( 'id', $form ) ) {
$output[ $key ]['id'] = strtoupper( uniqid( 'kdc_' ) );
}
foreach ( $form as $option => $value ) {
switch ( $option ) {
case 'modal_title':
case 'welcome_text':
$output[ $key ][ $option ] = sanitize_text_field( $value );
break;
case 'amount_type':