add-cloneable-field-value.php
11 months ago
get-metabox-value.php
11 months ago
get-object-fields.php
11 months ago
set-metavalue.php
11 months ago
update-cloneable-field-value.php
11 months ago
set-metavalue.php
105 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SetMetaValue. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SetMetaValue |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | use SureTriggers\Integrations\AutomateAction; |
| 15 | use SureTriggers\Traits\SingletonLoader; |
| 16 | |
| 17 | /** |
| 18 | * SetMetaValue |
| 19 | * |
| 20 | * @category SetMetaValue |
| 21 | * @package SureTriggers |
| 22 | * @author BSF <username@example.com> |
| 23 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 24 | * @link https://www.brainstormforce.com/ |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class SetMetaValue extends AutomateAction { |
| 28 | |
| 29 | /** |
| 30 | * Integration type. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $integration = 'MetaBox'; |
| 35 | |
| 36 | /** |
| 37 | * Action name. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $action = 'set_metavalue'; |
| 42 | |
| 43 | use SingletonLoader; |
| 44 | |
| 45 | /** |
| 46 | * Register a action. |
| 47 | * |
| 48 | * @param array $actions actions. |
| 49 | * @return array |
| 50 | */ |
| 51 | public function register( $actions ) { |
| 52 | $actions[ $this->integration ][ $this->action ] = [ |
| 53 | 'label' => __( 'Get Object Fields', 'suretriggers' ), |
| 54 | 'action' => $this->action, |
| 55 | 'function' => [ $this, 'action_listener' ], |
| 56 | ]; |
| 57 | return $actions; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Action listener. |
| 62 | * |
| 63 | * @param int $user_id user_id. |
| 64 | * @param int $automation_id automation_id. |
| 65 | * @param array $fields fields. |
| 66 | * @param array $selected_options selectedOptions. |
| 67 | * @psalm-suppress UndefinedMethod |
| 68 | * @throws Exception Exception. |
| 69 | * |
| 70 | * @return array|bool|void |
| 71 | */ |
| 72 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 73 | $object_id = $selected_options['object_id']; |
| 74 | |
| 75 | $field_id = $selected_options['field_id']; |
| 76 | |
| 77 | $value = $selected_options['meta_value']; |
| 78 | |
| 79 | if ( is_string( $value ) && strpos( $value, ',' ) !== false ) { |
| 80 | $value = array_map( 'trim', explode( ',', $value ) ); |
| 81 | } |
| 82 | |
| 83 | if ( function_exists( 'rwmb_set_meta' ) ) { |
| 84 | rwmb_set_meta( $object_id, $field_id, $value ); |
| 85 | if ( function_exists( 'rwmb_get_value' ) ) { |
| 86 | $values = rwmb_get_value( $field_id, '', $object_id ); |
| 87 | $response = [ |
| 88 | $field_id => $values, |
| 89 | 'object_id' => $object_id, |
| 90 | ]; |
| 91 | return $response; |
| 92 | } |
| 93 | } else { |
| 94 | $response = [ |
| 95 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 96 | 'message' => esc_attr__( 'Function rwmb_set_meta not exists. Please make sure the Metabox plugin is installed and active.', 'suretriggers' ), |
| 97 | |
| 98 | ]; |
| 99 | return $response; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | SetMetaValue::get_instance(); |
| 105 |