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

Reformat code

parent 70fadd25
......@@ -68,6 +68,22 @@ class Admin {
$this->plugin_name = $plugin_name;
$this->version = $version;
add_action( 'kudos_transactions_update',
function ( $column, $value ) {
$logger = new LoggerService();
$logger->debug( 'kudos_transactions_update', [ $column, $value ] );
},
10,
3 );
add_action( 'kudos_donors_add',
function ( $column, $value ) {
$logger = new LoggerService();
$logger->debug( 'kudos_donors_add', [ $column, $value ] );
},
10,
3 );
}
/**
......@@ -400,7 +416,7 @@ class Admin {
case 'kudos_clear_cache':
$twig = new TwigService();
if($twig->clearCache()) {
if ( $twig->clearCache() ) {
new AdminNotice( __( 'Cache cleared', 'kudos-donations' ) );
}
break;
......@@ -411,7 +427,11 @@ class Admin {
$records = $mapper->delete_all();
if ( $records ) {
/* translators: %s: Number of records. */
new AdminNotice( sprintf( _n( 'Deleted %s transaction', 'Deleted %s transactions', $records, 'kudos-donations' ), $records ) );
new AdminNotice( sprintf( _n( 'Deleted %s transaction',
'Deleted %s transactions',
$records,
'kudos-donations' ),
$records ) );
}
break;
......@@ -422,7 +442,11 @@ class Admin {
$records = $mapper->delete_all();
if ( $records ) {
/* translators: %s: Number of records. */
new AdminNotice( sprintf( _n( 'Deleted %s donor', 'Deleted %s donors', $records, 'kudos-donations' ), $records ) );
new AdminNotice( sprintf( _n( 'Deleted %s donor',
'Deleted %s donors',
$records,
'kudos-donations' ),
$records ) );
}
break;
......@@ -432,14 +456,19 @@ class Admin {
$records = $mapper->delete_all();
if ( $records ) {
/* translators: %s: Number of records. */
new AdminNotice( sprintf( _n( 'Deleted %s subscription', 'Deleted %s subscriptions', $records, 'kudos-donations' ), $records ) );
new AdminNotice( sprintf( _n( 'Deleted %s subscription',
'Deleted %s subscriptions',
$records,
'kudos-donations' ),
$records ) );
}
break;
case 'kudos_cancel_subscription':
$mollie = MollieService::factory();
$subscription = $mollie->cancel_subscription( $_REQUEST['subscriptionId'], $_REQUEST['customerId'] );
$subscription = $mollie->cancel_subscription( $_REQUEST['subscriptionId'],
$_REQUEST['customerId'] );
if ( $subscription ) {
new AdminNotice( __( 'Subscription cancelled', 'kudos-donations' ) );
}
......
......@@ -66,7 +66,9 @@ class CampaignsTable extends WP_List_Table {
$mapper = $this->mapper;
$campaigns = Settings::get_setting( 'campaign_labels' );
if(!$campaigns) return [];
if ( ! $campaigns ) {
return [];
}
// Add search query if exist
if ( ! empty( $_REQUEST['s'] ) ) {
......@@ -186,19 +188,20 @@ class CampaignsTable extends WP_List_Table {
return __( 'Added',
'kudos-donations' ) . '<br/>' . date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
strtotime( $item['date'] ) ) . $this->row_actions($actions);
strtotime( $item['date'] ) ) . $this->row_actions( $actions );
}
/**
* Label column
*
* @param $item
*
* @return string
* @since 2.0.4
*/
function column_label( $item ) {
return strtoupper($item['label']);
return strtoupper( $item['label'] );
}
......@@ -206,13 +209,14 @@ class CampaignsTable extends WP_List_Table {
* Transactions column
*
* @param $item
*
* @return string
* @since 2.0.4
*/
function column_transactions($item) {
return sprintf('<a href=%1$s>%2$s</a>',
sprintf(admin_url( 'admin.php?page=kudos-transactions&s=%s'), urlencode( $item['label'] ) ),
strtoupper($item['transactions'])
function column_transactions( $item ) {
return sprintf( '<a href=%1$s>%2$s</a>',
sprintf( admin_url( 'admin.php?page=kudos-transactions&s=%s' ), urlencode( $item['label'] ) ),
strtoupper( $item['transactions'] )
);
}
......@@ -273,7 +277,7 @@ class CampaignsTable extends WP_List_Table {
if ( isset( $_REQUEST['bulk-action'] ) ) {
$labels = esc_sql( $_REQUEST['bulk-action'] );
foreach ( $labels as $label ) {
self::delete_record( sanitize_text_field($label) );
self::delete_record( sanitize_text_field( $label ) );
}
}
break;
......@@ -290,12 +294,13 @@ class CampaignsTable extends WP_List_Table {
*/
protected function delete_record( string $label ) {
$labels = Settings::get_setting('campaign_labels');
$labels = array_filter($labels, function ($a) use ($label) {
return !in_array($label, $a);
});
$labels = Settings::get_setting( 'campaign_labels' );
$labels = array_filter( $labels,
function ( $a ) use ( $label ) {
return ! in_array( $label, $a );
} );
return Settings::update_setting('campaign_labels', $labels);
return Settings::update_setting( 'campaign_labels', $labels );
}
}
\ No newline at end of file
......@@ -369,14 +369,15 @@ class TransactionsTable extends WP_List_Table {
* Return campaign label as a search link
*
* @param array $item
*
* @return string
* @since 2.0.2
*/
function column_campaign_label( array $item ) {
return sprintf('<a href=%1$s>%2$s</a>',
sprintf(admin_url( 'admin.php?page=kudos-transactions&s=%s'), urlencode( $item['campaign_label'] ) ),
strtoupper($item['campaign_label'])
return sprintf( '<a href=%1$s>%2$s</a>',
sprintf( admin_url( 'admin.php?page=kudos-transactions&s=%s' ), urlencode( $item['campaign_label'] ) ),
strtoupper( $item['campaign_label'] )
);
}
......
......@@ -20,7 +20,8 @@ switch ( $action ) {
case 'bulk-delete':
$records = count( $_REQUEST['bulk-action'] );
/* translators: %s: Number of records */
$message = sprintf( _n( 'Deleted %s campaign', 'Deleted %s campaigns', $records, 'kudos-donations' ), $records );
$message = sprintf( _n( 'Deleted %s campaign', 'Deleted %s campaigns', $records, 'kudos-donations' ),
$records );
break;
}
......
......@@ -35,11 +35,11 @@ $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $default_tab;
<?php
$url = admin_url( 'admin.php?page=kudos-debug' );
$url = admin_url( 'admin.php?page=kudos-debug' );
switch ( $tab ):
case 'actions':
$url = add_query_arg('tab', 'actions', $url);
$url = add_query_arg( 'tab', 'actions', $url );
?>
<p>Please use the following actions only if you are having issues. Remember to backup your data before
performing any of these actions.</p>
......@@ -92,8 +92,8 @@ $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $default_tab;
break;
case 'log':
$url = add_query_arg('tab', 'log', $url);
$file = LoggerService::LOG_FILE;
$url = add_query_arg( 'tab', 'log', $url );
$file = LoggerService::LOG_FILE;
// Quit if file does not exist
if ( ! file_exists( $file ) ) {
......
......@@ -21,14 +21,19 @@ switch ( $action ) {
$message = __( 'Subscription deleted', 'kudos-donations' );
break;
case 'bulk-cancel':
$records = count( $_REQUEST['bulk-action']);
$records = count( $_REQUEST['bulk-action'] );
/* translators: %s: Number of records */
$message = sprintf( _n( 'Cancelled %s subscription', 'Cancelled %s subscriptions', $records, 'kudos-donations' ), $records );
$message = sprintf( _n( 'Cancelled %s subscription',
'Cancelled %s subscriptions',
$records,
'kudos-donations' ),
$records );
break;
case 'bulk-delete':
/* translators: %s: Number of records */
$records = count( $_REQUEST['bulk-action']);
$message = sprintf( _n( 'Deleted %s subscription', 'Deleted %s subscriptions', $records, 'kudos-donations' ), $records );
$records = count( $_REQUEST['bulk-action'] );
$message = sprintf( _n( 'Deleted %s subscription', 'Deleted %s subscriptions', $records, 'kudos-donations' ),
$records );
break;
}
......
......@@ -18,9 +18,10 @@ switch ( $action ) {
$message = __( 'Transaction deleted', 'kudos-donations' );
break;
case 'bulk-delete':
$records = count( $_REQUEST['bulk-action'] );
$records = count( $_REQUEST['bulk-action'] );
/* translators: %s: Number of records */
$message = sprintf( _n( 'Deleted %s transaction', 'Deleted %s transactions', $records, 'kudos-donations' ), $records );
$message = sprintf( _n( 'Deleted %s transaction', 'Deleted %s transactions', $records, 'kudos-donations' ),
$records );
break;
}
......
......@@ -108,7 +108,7 @@ class KudosButton {
$data = [
'modal_id' => $this->id,
'modal_title' => $this->title,
'modal_text' => $this->text,
'modal_text' => $this->text,
'amount' => [
'type' => $this->amount_type,
'fixed_values' => explode( ',', $this->fixed_amounts ),
......
......@@ -30,20 +30,6 @@ class KudosModal {
}
/**
* Renders the modal using twig
*
* @param string $template
* @param array $data
*
* @return bool
*/
private function render_modal( string $template, array $data ) {
return $this->twig->render( $template, $data );
}
/**
* Get message modal markup
*
......@@ -55,15 +41,29 @@ class KudosModal {
public function get_message_modal( array $atts ) {
$data = [
'modal_id' => uniqid( 'kudos_modal-message-' ),
'modal_title' => $atts['modal_title'],
'modal_text' => $atts['modal_text'],
'modal_id' => uniqid( 'kudos_modal-message-' ),
'modal_title' => $atts['modal_title'],
'modal_text' => $atts['modal_text'],
];
return $this->render_modal( '/public/modal/message.modal.html.twig', $data );
}
/**
* Renders the modal using twig
*
* @param string $template
* @param array $data
*
* @return bool
*/
private function render_modal( string $template, array $data ) {
return $this->twig->render( $template, $data );
}
/**
* Get the donate modal markup
*
......@@ -94,7 +94,9 @@ class KudosModal {
$out = $this->twig->render( '/public/modal/donate.modal.html.twig', $data );
if ( $echo ) echo $out;
if ( $echo ) {
echo $out;
}
return $out;
......
......@@ -167,18 +167,18 @@ class Settings {
'type' => 'array',
'show_in_rest' => [
'schema' => [
'type' => 'array',
'type' => 'array',
'items' => [
'type' => 'object',
'type' => 'object',
'properties' => [
'date' => [
'type' => 'string'
'date' => [
'type' => 'string',
],
'label' => [
'type' => 'string'
]
'type' => 'string',
],
],
]
],
],
],
],
......
......@@ -102,7 +102,7 @@ class Utils {
public static function color_luminance( string $hex, float $percent ) {
// Remove leading '#' if present
$hex = ltrim($hex, '#');
$hex = ltrim( $hex, '#' );
// Expand to 6 character hex code (e.g. FFF -> FFFFFF)
if ( strlen( $hex ) == 3 ) {
......@@ -110,17 +110,17 @@ class Utils {
}
// Convert to decimal
$hex = array_map('hexdec', str_split($hex, 2));
$hex = array_map( 'hexdec', str_split( $hex, 2 ) );
// Change luminosity of decimal colour
foreach ( $hex as & $color ) {
$adjustableLimit = $percent < 0 ? $color : 255 - $color;
$adjustAmount = ceil($adjustableLimit * $percent);
$adjustAmount = ceil( $adjustableLimit * $percent );
$color = str_pad(dechex($color + $adjustAmount), 2, '0', STR_PAD_LEFT);
$color = str_pad( dechex( $color + $adjustAmount ), 2, '0', STR_PAD_LEFT );
}
return '#' . implode($hex);
return '#' . implode( $hex );
}
/**
......
......@@ -83,7 +83,7 @@ class KudosDonations {
$this->define_admin_hooks();
$this->define_public_hooks();
if ( Settings::get_setting( 'action_scheduler' ) && file_exists(KUDOS_PLUGIN_DIR . '/libraries/action-scheduler/action-scheduler.php') ) {
if ( Settings::get_setting( 'action_scheduler' ) && file_exists( KUDOS_PLUGIN_DIR . '/libraries/action-scheduler/action-scheduler.php' ) ) {
require_once( KUDOS_PLUGIN_DIR . '/libraries/action-scheduler/action-scheduler.php' );
}
......@@ -209,7 +209,7 @@ class KudosDonations {
$db_version = get_option( '_kudos_donations_version' );
if ( $db_version !== $this->get_version() ) {
ActivatorService::activate($db_version);
ActivatorService::activate( $db_version );
}
}
......
......@@ -27,9 +27,10 @@ class ActivatorService {
* Runs all activation functions
*
* @param $old_version
*
* @since 1.0.0
*/
public static function activate($old_version = null) {
public static function activate( $old_version = null ) {
$logger = new LoggerService();
......@@ -40,10 +41,12 @@ class ActivatorService {
self::create_subscriptions_table();
self::set_defaults();
if ( $old_version && version_compare($old_version, '2.0.4', '<' ) ) {
$logger->info('Upgrading to version 2.0.4', ['previous_version' => $old_version]);
if ( $old_version && 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');
if ( $result ) {
$logger->info( 'Updated campaign labels from transactions' );
}
}
$logger->info( 'Kudos Donations plugin activated' );
......
......@@ -25,5 +25,4 @@ class I18nService extends AbstractService {
}
}
\ No newline at end of file
......@@ -23,7 +23,7 @@ class MailerService extends AbstractService {
*/
public function __construct() {
$from_name = apply_filters('kudos_email_from_name', __("Kudos Donations", 'kudos-donations'));
$from_name = apply_filters( 'kudos_email_from_name', __( "Kudos Donations", 'kudos-donations' ) );
$this->from = "From: $from_name " . ' <' . ( Settings::get_setting( 'smtp_from' ) ?: Settings::get_setting( 'smtp_username' ) ) . '>';
parent::__construct();
......
......@@ -73,7 +73,7 @@ class MapperService {
[ 'id' => $entity->id ]
);
if($result) {
if ( $result ) {
do_action( $entity::TABLE . '_update', 'id', $entity->id );
}
......@@ -83,7 +83,7 @@ class MapperService {
// Otherwise insert new row
$entity->created = current_time( 'mysql' );
$result = $wpdb->insert(
$result = $wpdb->insert(
$table,
array_filter( $entity->to_array(), [ $this, 'remove_empty' ] )
);
......@@ -91,7 +91,7 @@ class MapperService {
$this->logger->debug( 'Creating entity.', [ $entity ] );
// If successful log and do action
if ($result) {
if ( $result ) {
do_action( $entity::TABLE . '_add', 'id', $entity->id );
}
......@@ -248,7 +248,7 @@ class MapperService {
[ $column => $value ]
);
if($deleted) {
if ( $deleted ) {
do_action( $this->repository::TABLE . '_delete', $column, $value );
}
......
......@@ -468,25 +468,26 @@ class MollieService extends AbstractService {
] );
// Add campaign label to recurring payments
if($payment->hasSequenceTypeRecurring()) {
if ( $payment->hasSequenceTypeRecurring() ) {
$subscription_id = $payment->subscriptionId;
$customer_id = $payment->customerId;
$customer_id = $payment->customerId;
try {
$customer = $this->mollieApi->customers->get($customer_id);
$subscription_meta = $customer->getSubscription($subscription_id)->metadata;
if(array_key_exists('campaign_label', $subscription_meta)) {
$customer = $this->mollieApi->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;
$transaction->set_fields([
'campaign_label' => $campaign_label
]);
$transaction->set_fields( [
'campaign_label' => $campaign_label,
] );
} else {
$this->logger->info('No campaign label found for recurring payment', ['customer_id' => $customer_id, 'subscription_id' => $subscription_id]);
$this->logger->info( 'No campaign label found for recurring payment',
[ 'customer_id' => $customer_id, 'subscription_id' => $subscription_id ] );
}
} catch (ApiException $e) {
$this->logger->warning($e->getMessage());
} catch ( ApiException $e ) {
$this->logger->warning( $e->getMessage() );
}
}
......@@ -586,8 +587,8 @@ class MollieService extends AbstractService {
$interval,
$transaction->order_id ),
"metadata" => [
"campaign_label" => $transaction->campaign_label
]
"campaign_label" => $transaction->campaign_label,
],
];
if ( $transaction->mode === "test" ) {
......
......@@ -39,13 +39,13 @@ class TwigService extends AbstractService {
*
* @since 1.0.0
*/
public function __construct( $templates_dir=[], $options=[] ) {
public function __construct( $templates_dir = [], $options = [] ) {
parent::__construct();
$this->templates_dir = $templates_dir;
$this->templates_dir[] = KUDOS_PLUGIN_DIR . '/templates/';
$this->options = $options;
$this->templates_dir = $templates_dir;
$this->templates_dir[] = KUDOS_PLUGIN_DIR . '/templates/';
$this->options = $options;
$this->options['cache'] = KUDOS_DEBUG ? false : self::CACHE_DIR;
$this->initializeTwig();
$this->initializeTwigFunctions();
......@@ -53,29 +53,10 @@ class TwigService extends AbstractService {
}
/**
* Create the twig cache directory
*
* @since 2.0.0
*/
public static function initCache() {
$logger = new LoggerService();
if ( wp_mkdir_p( self::CACHE_DIR ) ) {
$logger->info( 'Twig cache directory created successfully' );
return;
}
$logger->error( 'Unable to create Kudos Donations Twig cache directory', [ self::CACHE_DIR ] );
}
public function initializeTwig() {
$loader = new FilesystemLoader( $this->templates_dir );
$this->twig = new Environment( $loader, $this->options );
$loader = new FilesystemLoader( $this->templates_dir );
$this->twig = new Environment( $loader, $this->options );
}
......@@ -133,6 +114,25 @@ class TwigService extends AbstractService {
}
/**
* Create the twig cache directory
*
* @since 2.0.0
*/
public static function initCache() {
$logger = new LoggerService();
if ( wp_mkdir_p( self::CACHE_DIR ) ) {
$logger->info( 'Twig cache directory created successfully' );
return;
}
$logger->error( 'Unable to create Kudos Donations Twig cache directory', [ self::CACHE_DIR ] );
}
/**
* Render the provided template
*
......@@ -161,14 +161,15 @@ class TwigService extends AbstractService {
*/
public function clearCache() {
$di = new RecursiveDirectoryIterator(self::CACHE_DIR, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
$files=0;