get-option-scf-value.php
1 month ago
get-post-scf-value.php
1 month ago
get-user-scf-value.php
1 month ago
update-group-field-value.php
1 month ago
update-option-scf-value.php
1 month ago
update-post-scf-value.php
1 month ago
update-repeater-field-value.php
1 month ago
update-user-scf-value.php
1 month ago
update-post-scf-value.php
98 lines
| 1 | <?php |
| 2 | /** |
| 3 | * UpdatePostScfValue. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category UpdatePostScfValue |
| 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 | namespace SureTriggers\Integrations\SecureCustomFields\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | use SureTriggers\Integrations\WordPress\WordPress; |
| 20 | |
| 21 | /** |
| 22 | * UpdatePostScfValue |
| 23 | * |
| 24 | * @category UpdatePostScfValue |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class UpdatePostScfValue extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'SecureCustomFields'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'update_post_scf_value'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Update Post Value', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * @psalm-suppress UndefinedMethod |
| 72 | * @throws Exception Exception. |
| 73 | * |
| 74 | * @return array|bool|void |
| 75 | */ |
| 76 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 77 | $field_name = $selected_options['field_id']; |
| 78 | $field_value = $selected_options['meta_value']; |
| 79 | $post_id = (int) $selected_options['wp_post']; |
| 80 | |
| 81 | if ( ! function_exists( 'update_field' ) || ! function_exists( 'get_field' ) ) { |
| 82 | throw new Exception( 'Secure Custom Fields update_field() function not found.' ); |
| 83 | } |
| 84 | |
| 85 | if ( is_array( json_decode( $field_value, true ) ) ) { |
| 86 | $field_value = json_decode( $field_value, true ); |
| 87 | } |
| 88 | $response_array = []; |
| 89 | if ( update_field( $field_name, $field_value, $post_id ) ) { |
| 90 | $response_array[ $field_name ] = get_field( $field_name, $post_id, true ); |
| 91 | $response_array['post'] = WordPress::get_post_context( $post_id ); |
| 92 | } |
| 93 | return $response_array; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | UpdatePostScfValue::get_instance(); |
| 98 |