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

More changes

parent 1f1a4af7
......@@ -153,7 +153,9 @@
</interpreter>
</phpInfoCache>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.2" />
<component name="PhpProjectSharedConfiguration" php_language_level="7.2">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>
<component name="PhpRuntimeConfiguration">
<extensions>
<extension name="Ev" enabled="false" />
......
......@@ -2,6 +2,7 @@
namespace Kudos\Admin;
use Kudos\Entity\CampaignEntity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
......@@ -13,6 +14,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;
......
<?php
namespace Kudos\Entity;
use Kudos\Service\MapperService;
class CampaignEntity extends AbstractEntity {
/**
* Table name without prefix
*
* @var string
*/
protected const TABLE = 'kudos_campaigns';
/**
* @var string
*/
public $slug;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $modal_title;
/**
* @var string
*/
public $welcome_text;
/**
* @var string
*/
public $donation_type;
/**
* @var string
*/
public $amount_type;
/**
* @var string
*/
public $fixed_amounts;
/**
* @var bool
*/
public $protected;
/**
* Gets all transactions for current user
*
* @return array|null
*/
public function get_transactions(): ?array {
$mapper = new MapperService( TransactionEntity::class );
return $mapper->get_all_by( [ 'campaign_label' => $this->slug ] );
}
}
......@@ -191,6 +191,9 @@ class Settings {
'donation_type' => [
'type' => 'string',
],
'protected' => [
'type' => 'boolean'
]
],
],
],
......@@ -216,11 +219,13 @@ class Settings {
// Loop through each of the options sanitizing the data
foreach ($forms as $key=>$form) {
foreach ($form as $option=>$value) {
switch ($option) {
case 'address_enabled':
case 'address_required':
case 'modal_title':
case 'welcome_text':
$output[$key][$option] = sanitize_text_field($value);
break;
case 'amount_type':
case 'donation_type':
$output[$key][$option] = sanitize_key($value);
......@@ -229,10 +234,9 @@ class Settings {
$output[$key][$option] = sanitize_title($value);
break;
default:
$output[$key][$option] = sanitize_text_field($value);
$output[$key][$option] = $value;
}
}
}
return $output;
......@@ -313,9 +317,25 @@ class Settings {
public function add_defaults() {
foreach ( $this->settings as $name => $setting ) {
add_option( self::PREFIX . $name, $setting['default'] ?? '' );
if(isset($setting['default'])) {
add_option( self::PREFIX . $name, $setting['default']);
}
}
$default_campaign[0] = [
'slug' => 'kudos_default_campaign',
'name' => 'Default',
'modal_title' => 'Hello',
'welcome_text' => 'Welcome text',
'address_required' => true,
'amount_type' => 'both',
'fixed_amounts' => '1,5,20,50,100',
'donation_type' => 'both',
'protected' => true
];
add_option(self::PREFIX . 'campaigns', $default_campaign);
}
/**
......
......@@ -2,6 +2,7 @@
namespace Kudos\Service;
use Kudos\Entity\CampaignEntity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
......@@ -175,6 +176,37 @@ class ActivatorService {
}
/**
* Creates the subscription table
*
* @since 1.1.0
*/
private static function create_campaigns_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = CampaignEntity::get_table_name();
$sql = "CREATE TABLE $table_name (
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
slug VARCHAR(255),
name VARCHAR(255),
modal_title VARCHAR(255),
welcome_text VARCHAR(255),
donation_type VARCHAR(255),
amount_type VARCHAR(255),
fixed_amounts VARCHAR(255),
protected BIT,
secret VARCHAR(255),
PRIMARY KEY (id)
) $charset_collate";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Adds default options if not already set
*
......
......@@ -7,6 +7,7 @@ use Kudos\Entity\EntityInterface;
use Kudos\Exceptions\MapperException;
use ReflectionClass;
use ReflectionException;
use WP_REST_Response;
use wpdb;
class MapperService extends AbstractService {
......
<?php
namespace Kudos\Service;
use Kudos\Entity\AbstractEntity;
use Kudos\Entity\CampaignEntity;
use Kudos\Entity\EntityInterface;
use Kudos\Exceptions\MapperException;
use ReflectionClass;
use ReflectionException;
use WP_REST_Request;
use WP_REST_Response;
use wpdb;
class RestMapperService extends AbstractService {
/**
* WordPress database global
*
* @var wpdb
*/
protected $wpdb;
/**
* Repository class
*
* @var string
*/
protected $repository;
/**
* Entity object constructor.
*
* @param string |null $repository Repository class.
*
* @since 2.0.0
*/
public function __construct( string $repository = null ) {
parent::__construct();
global $wpdb;
$this->wpdb = $wpdb;
if ( null !== $repository ) {
try {
$this->set_repository( $repository );
} catch ( MapperException $e ) {
$this->logger->error( 'Could not set repository', [ 'message' => $e->getMessage() ] );
}
}
}
/**
* Returns current repository table name
*
* @param bool $prefix Whether to return the prefix or not.
*
* @return string|false
* @since 2.0.0
*/
public function get_table_name( $prefix = true ) {
return $this->get_repository()::get_table_name( $prefix );
}
/**
* Gets the current repository
*
* @return AbstractEntity|string
* @since 2.0.5
*/
public function get_repository() {
try {
if ( null === $this->repository ) {
throw new MapperException( 'No repository specified' );
}
} catch ( MapperException $e ) {
$this->logger->warning( 'Failed to get repository.', [ 'message' => $e->getMessage() ] );
}
return $this->repository;
}
/**
* Specify the repository to use
*
* @param string $class Class of repository to use.
*
* @throws MapperException
* @since 2.0.0
*/
public function set_repository( string $class ) {
try {
$reflection = new ReflectionClass( $class );
if ( $reflection->implementsInterface( 'Kudos\Entity\EntityInterface' ) ) {
$this->repository = $class;
} else {
throw new MapperException( 'Repository must implement Kudos\Entity\EntityInterface', 0, $class );
}
} catch ( ReflectionException $e ) {
$this->logger->error( $e->getMessage() );
}
}
/**
* Maps array of current repository objects to instance
* of current repository
*
* @param array $results Array of properties and values to map.
*
* @return array
* @since 2.0.0
*/
private function map_to_class( array $results ): array {
return array_map(
function ( $result ) {
return new $this->repository( $result );
},
$results
);
}
/**
* Removes empty values from array
*
* @param string|null $value Array value to check.
*
* @return bool
* @since 2.0.0
*/
private function remove_empty( ?string $value ): bool {
return ! is_null( $value ) && '' !== $value;
}
/**
* Updates existing record
*
* @param WP_REST_Request $entity An instance of EntityInterface.
*
* @return WP_REST_Response Returns the id of the record if successful
* and false if not
*/
public function update_record( WP_REST_Request $entity ): WP_REST_Response {
$table_name = CampaignEntity::get_table_name();
$wpdb = $this->wpdb;
$entity = json_decode($entity);
$result = $wpdb->update(
$table_name,
$entity,
[ 'id' => $entity['id'] ]
);
return new WP_REST_Response(
[
'status' => 200,
'response' => 'hello',
'body_response' => $entity,
]
);
// $wpdb = $this->wpdb;
// $table_name = $entity::get_table_name();
// $id = $entity->id;
//
// $this->logger->debug( 'Updating entity.', [ $entity ] );
//
// $result = $wpdb->update(
// $table_name,
// $ignore_null ? array_filter( $entity->to_array(), [ $this, 'remove_empty' ] ) : $entity->to_array(),
// [ 'id' => $id ]
// );
//
// if ( $result ) {
// do_action( $entity::get_table_name( false ) . '_updated', 'id', $id );
//
// return $id;
// }
//
// return $result;
}
public function get_all(): WP_REST_Response {
$table = $this->get_table_name();
$query = "SELECT $table.* FROM $table";
$results = $this->get_results( $query, ARRAY_A );
if ( ! empty( $results ) ) {
return new WP_REST_Response(
[
'status' => 200,
'response' => 'hello',
'body_response' => json_encode($this->map_to_class( $results ))
]
);
}
return new WP_REST_Response(
[
'status' => 200,
'response' => 'hello',
'body_response' => '',
]
);
}
}
import { CampaignPanel } from "./CampaignPanel"
const { __, sprintf } = wp.i18n;
const { PanelBody, BaseControl, TextControl, Button } = wp.components;
const { useState } = wp.element;
const AddCampaignPanel = ( props ) => {
const [ addFormValue, setAddFormValue ] = useState('');
const addCampaign = ( name ) => {
let current = props.settings._kudos_campaigns;
// Bail if name is empty
if( '' === name.trim() ) {
props.showNotice(__('Campaign name empty.', 'kudos-donations'))
return;
}
// Bail if duplicate found
if(current.find( x => x.name.toLowerCase() === name.toLowerCase().trim() )) {
props.showNotice(__('Duplicate campaign name', 'kudos-donations'))
return;
}
// Add new campaign with defaults to top of array using unshift
current.unshift({
slug: name,
name: name,
modal_title: __( 'Support us!', 'kudos-donations' ),
welcome_text: __( 'Your support is greatly appreciated and will help to keep us going.', 'kudos-donations' ),
donation_type: 'both',
amount_type: 'both',
fixed_amounts: '5,10,20,50'
})
// Save changes and show notice
props.updateSetting('_kudos_campaigns', current );
props.showNotice( sprintf(__('Added campaign "%s".', 'kudos-donations'), name) )
};
return (
<div>
<PanelBody
title={ __( 'Campaigns', 'kudos-donations' ) }
initialOpen={ true }
>
<BaseControl
help={__("Give your campaign a unique name to identify it.", 'kudos-donations')}
>
<TextControl
label={ __(
'Add campaign',
'kudos-donations'
) }
id={'kudos_new_campaign'}
className={'kd-inline'}
type={ 'text' }
value={ addFormValue }
onChange={ (newValue) => setAddFormValue( newValue ) }
/>
<Button
isPrimary
isSmall
onClick={
() => addCampaign(document.getElementById('kudos_new_campaign').value)
}
>
{__('Add campaign', 'kudos-donations')}
</Button>
</BaseControl>
</PanelBody>
{ props.settings._kudos_campaigns.map((form, i) => {
return(
<CampaignPanel
key={ form.name }
allowDelete={ !form.protected }
settings={ props.settings }
campaign={ props.settings._kudos_campaigns[i] }
updateSetting={ props.updateSetting }
showNotice={ props.showNotice }
handleInputChange={ props.handleInputChange }
/>
)
})}
</div>
);
};
export { AddCampaignPanel };
const { __ } = wp.i18n;
const { CheckboxControl, PanelBody, BaseControl, RadioControl, ToggleControl, TextControl, Button } = wp.components;
const CampaignPanel = ( { settings, campaign, showNotice, updateSetting, handleInputChange, allowDelete = false } ) => {
const saveCampaigns = () => {
handleInputChange('_kudos_campaigns', settings._kudos_campaigns)
}
const removeCampaign = ( slug ) => {
let current = settings._kudos_campaigns
let updated = current.filter( function (value) {
return value.slug !== slug
} )
updateSetting('_kudos_campaigns', _.uniq(updated, 'slug') )
showNotice( __('Campaign deleted.', 'kudos-donations') )
}
return (
<PanelBody
title={ __('Campaign', 'kudos-donations') + ' - ' + campaign.name }
initialOpen={ false }
>
<BaseControl
id={ campaign.slug + "-modal-text" }
help={'Customize the text of the form'}
>
<TextControl
label={ __( 'Header', 'kudos-donations' ) }
id={ 'modal_title' + '-' + campaign.name }
className={ 'kd-inline' }
type={ 'text' }
value={ campaign.modal_title || '' }
onChange={ (value) => {
campaign.modal_title = value
saveCampaigns()
} }
/>
<TextControl
label={ __( 'Welcome text', 'kudos-donations' ) }
id={ 'welcome_text' + '-' + campaign.name }
className={ 'kd-inline' }
type={ 'text' }
value={ campaign.welcome_text || '' }
onChange={ (value) => {
campaign.welcome_text = value
saveCampaigns()
} }
/>
</BaseControl>
<BaseControl
id={ campaign.slug + "-address" }
label="Address"
help={'Configure the address fields'}
>
<ToggleControl
label={__("Enabled", 'kudos-donations')}
checked={ campaign.address_enabled || '' }
onChange={ (value) => {
campaign.address_enabled = value
saveCampaigns()
} }
/>
{ campaign.address_enabled ?
<CheckboxControl
label={__("Required", "kudos-donations")}
checked={ campaign.address_required || '' }
onChange={ (value) => {
campaign.address_required = value