Skip to content

Commit

Permalink
add generic action
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Oct 15, 2023
1 parent 3a3a855 commit b03b09c
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 12 deletions.
6 changes: 6 additions & 0 deletions noptin.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ class Noptin {
*/
public $plugin_url = null;

/**
* If this is a test site.
*/
public $is_test = false;

/**
* White Label
*
Expand Down Expand Up @@ -295,6 +300,7 @@ private function setup_globals() {
// Set up globals;
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->is_test = 'production' !== wp_get_environment_type();

// Form manager.
$this->forms = new Noptin_Form_Manager();
Expand Down
175 changes: 175 additions & 0 deletions src/Objects/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace Hizzle\Noptin\Objects;

/**
* Generic object action.
*
* @since 2.2.0
*/

defined( 'ABSPATH' ) || exit;

/**
* Generic object action.
*/
class Action extends \Noptin_Abstract_Action {

/**
* @var string $object_type The object type.
*/
public $object_type;

/**
* @var string $action_id
*/
public $action_id;

/**
* @var array $action_args
*/
public $action_args;

/**
* Constructor.
*
* @param string $action_id The trigger id.
* @param array $action_args The trigger args.
* @param Collection $collection The collection.
* @since 2.2.0
*/
public function __construct( $action_id, $action_args, $collection ) {
$this->object_type = $collection->type;
$this->action_id = $action_id;
$this->action_args = $action_args;
$this->category = $collection->label;
$this->integration = $collection->integration;
}

/**
* @inheritdoc
*/
public function get_id() {
return $this->action_id;
}

/**
* @inheritdoc
*/
public function get_name() {
return $this->action_args['label'];
}

/**
* @inheritdoc
*/
public function get_description() {
return $this->action_args['description'];
}

/**
* Retrieve the actions's rule table description.
*
* @param \Noptin_Automation_Rule $rule
* @return array
*/
public function get_rule_table_description( $rule ) {
$settings = $rule->action_settings;
$fields = Store::fields( $this->object_type );
$meta = array();

foreach ( $fields as $key => $args ) {
if ( ! empty( $args['required'] ) && empty( $settings[ $key ] ) ) {
return sprintf(
'<span class="noptin-rule-error">%s</span>',
sprintf(
// translators: %s is the field label.
esc_html__( 'Error: "%s" not specified', 'newsletter-optin-box' ),
$args['label']
)
);
}

if ( ! empty( $args['show_in_meta'] ) || ! empty( $args['required'] ) ) {
$meta[ esc_html( $args['label'] ) ] = isset( $settings[ $key ] ) ? esc_html( $settings[ $key ] ) : '';
}
}

return $this->rule_action_meta( $meta, $rule );
}

/**
* @inheritdoc
*/
public function get_settings() {

$settings = array();
$all_fields = Store::fields( $this->object_type );
$fields = isset( $this->action_args['fields'] ) ? $this->action_args['fields'] : array_keys( $all_fields );

// Maybe add extra fields.
if ( ! empty( $this->action_args['extra_settings'] ) ) {
$settings = $this->action_args['extra_settings'];
}

foreach ( $fields as $field_key ) {

if ( ! isset( $all_fields[ $field_key ] ) ) {
continue;
}

$field = $all_fields[ $field_key ];

$settings[ $field_key ] = array(
'type' => 'text',
'el' => 'input',
'label' => $field['label'],
'map_field' => true,
'placeholder' => sprintf(
/* translators: %s: The field name. */
__( 'Enter %s', 'newsletter-optin-box' ),
strtolower( $field['label'] )
),
'description' => empty( $field['description'] ) ? '' : $field['description'],
);
}

return $settings;
}

/**
* Run the action.
*
* @param mixed $subject The subject.
* @param \Noptin_Automation_Rule $rule The automation rule used to trigger the action.
* @param array $args Extra arguments passed to the action.
* @return void
*/
public function run( $subject, $rule, $args ) {

$settings = array();

/** @var \Noptin_Automation_Rules_Smart_Tags $smart_tags */
$smart_tags = $args['smart_tags'];

foreach ( wp_unslash( $rule->action_settings ) as $key => $value ) {

if ( '' === $value ) {
continue;
}

$settings[ $key ] = is_scalar( $value ) ? $smart_tags->replace_in_content( $value ) : $value;
}

call_user_func_array(
array( $this->action_args['callback'] ),
array(
$settings,
$subject,
$rule,
$args,
$smart_tags,
)
);
}
}
1 change: 1 addition & 0 deletions src/Objects/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function __construct() {
add_action( 'noptin_automation_rules_load', array( $this, 'load_automation_rules' ) );
}

// Set automation rule smart tags prefix.
if ( is_null( $this->smart_tags_prefix ) ) {
$this->smart_tags_prefix = $this->type;
}
Expand Down
25 changes: 17 additions & 8 deletions src/Objects/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function smart_tags( $type, $group = '', $prefix = true ) {
$callback = array( __CLASS__, 'handle_field_smart_tag' );
$prefix = $prefix ? self::get_collection_config( $type, 'smart_tags_prefix' ) : false;

return self::convert_fields_to_smart_tags( $fields, $group, $prefix, $callback );
return self::convert_fields_to_smart_tags( $fields, $type, $group, $prefix, $callback );
}

/**
Expand All @@ -106,7 +106,7 @@ public static function smart_tags( $type, $group = '', $prefix = true ) {
* @since 2.2.0
* @return array
*/
public static function convert_fields_to_smart_tags( $fields, $group = '', $prefix = false, $callback = false ) {
public static function convert_fields_to_smart_tags( $fields, $object_type = '', $group = '', $prefix = false, $callback = false ) {

$prepared = array();

Expand All @@ -119,6 +119,11 @@ public static function convert_fields_to_smart_tags( $fields, $group = '', $pref
$smart_tag['example'] = $key . ' ' . $smart_tag['example'];
}

// Add collection.
if ( ! empty( $object_type ) ) {
$smart_tag['object_type'] = $object_type;
}

$prepared[ $key ] = $smart_tag;
}

Expand Down Expand Up @@ -177,32 +182,36 @@ public static function get_collection_config( $type, $field = 'singular_label' )
*
* @param array $args The args.
* @param string $field The field.
* @param array $config The config.
* @return string The smart tag.
*/
public static function handle_field_smart_tag( $args, $field ) {
public static function handle_field_smart_tag( $args, $field, $config = array() ) {
/** @var Record[] $noptin_current_objects */
global $noptin_current_objects;

if ( ! is_array( $noptin_current_objects ) ) {
if ( ! is_array( $noptin_current_objects ) || empty( $config['object_type'] ) ) {
return '';
}

$collection = strtok( $field, '.' );
// Remove prefix.
$field = explode( '.', $field );
array_shift( $field );
$field = implode( '.', $field );

// Bail if the collection doesn't exist.
if ( ! isset( $noptin_current_objects[ $collection ] ) ) {
if ( ! isset( $noptin_current_objects[ $config['object_type'] ] ) || empty( $field ) ) {
return '';
}

// Fetch the raw value.
$raw_value = $noptin_current_objects[ $collection ]->get( strtok( '.' ) );
$raw_value = $noptin_current_objects[ $config['object_type'] ]->get( $field );

// Are we formatting the value?
if ( empty( $args['format'] ) || '' === $raw_value || null === $raw_value ) {
return $raw_value;
}

return $noptin_current_objects[ $collection ]->format( $raw_value, $args );
return $noptin_current_objects[ $config['object_type'] ]->format( $raw_value, $args );
}

}
7 changes: 3 additions & 4 deletions src/Objects/Trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ public function __construct( $trigger_id, $trigger_args, $collection ) {
$this->object_type = $collection->type;
$this->trigger_id = $trigger_id;
$this->trigger_args = $trigger_args;

// Maybe set category and integration.
$this->category = $collection->label;
$this->integration = $collection->integration;
$this->category = $collection->label;
$this->integration = $collection->integration;

if ( empty( $trigger_args['subject'] ) ) {
$this->is_user_based = true;
Expand Down Expand Up @@ -102,6 +100,7 @@ public function get_known_smart_tags() {
$args,
Store::convert_fields_to_smart_tags(
$this->trigger_args['extra_args'],
$this->object_type,
Store::get_collection_config( $this->object_type ),
Store::get_collection_config( $this->object_type, 'smart_tags_prefix' )
)
Expand Down

0 comments on commit b03b09c

Please sign in to comment.