suredash-comment-deleted.php
8 months ago
suredash-comment-edited.php
8 months ago
suredash-comment-submitted.php
8 months ago
suredash-course-completed.php
10 months ago
suredash-item-bookmarked.php
8 months ago
suredash-post-deleted.php
8 months ago
suredash-post-edited.php
8 months ago
suredash-post-liked.php
10 months ago
suredash-post-submitted.php
8 months ago
suredash-space-deleted.php
8 months ago
suredash-post-liked.php
147 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SureDashPostLiked trigger for handling post like events. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SureDashPostLiked |
| 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\SureDash\Triggers; |
| 15 | |
| 16 | use SureTriggers\Controllers\AutomationController; |
| 17 | use SureTriggers\Integrations\WordPress\WordPress; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | use WP_REST_Request; |
| 20 | use WP_REST_Server; |
| 21 | |
| 22 | /** |
| 23 | * SureDashPostLiked |
| 24 | * |
| 25 | * @category SureDashPostLiked |
| 26 | * @package SureTriggers |
| 27 | * @author BSF <username@example.com> |
| 28 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 29 | * @link https://www.brainstormforce.com/ |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | class SureDashPostLiked { |
| 33 | |
| 34 | /** |
| 35 | * Integration type. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $integration = 'SureDash'; |
| 40 | |
| 41 | /** |
| 42 | * Trigger name. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $trigger = 'suredash_post_liked'; |
| 47 | |
| 48 | use SingletonLoader; |
| 49 | |
| 50 | /** |
| 51 | * Constructor |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public function __construct() { |
| 56 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Register a trigger. |
| 61 | * |
| 62 | * @param array $triggers triggers. |
| 63 | * @return array |
| 64 | */ |
| 65 | public function register( $triggers ) { |
| 66 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 67 | 'label' => __( 'User likes a post', 'suretriggers' ), |
| 68 | 'action' => 'rest_pre_dispatch', |
| 69 | 'function' => [ $this, 'trigger_listener' ], |
| 70 | 'priority' => 10, |
| 71 | 'accepted_args' => 3, |
| 72 | ]; |
| 73 | return $triggers; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Trigger listener for post like events. |
| 78 | * |
| 79 | * @param mixed $result Response to replace the requested version with. |
| 80 | * @param WP_REST_Server $server Server instance. |
| 81 | * @param WP_REST_Request $request Request used to generate the response. |
| 82 | * @since 1.0.0 |
| 83 | * |
| 84 | * @return mixed |
| 85 | */ |
| 86 | public function trigger_listener( $result, WP_REST_Server $server, WP_REST_Request $request ) { |
| 87 | // Check if this is a SureDash entity-reaction endpoint. |
| 88 | if ( false === strpos( $request->get_route(), 'entity-reaction' ) ) { |
| 89 | return $result; |
| 90 | } |
| 91 | |
| 92 | $params = $request->get_params(); |
| 93 | |
| 94 | // Check if this is a post like action. |
| 95 | if ( empty( $params['entity'] ) || 'post' !== $params['entity'] ) { |
| 96 | return $result; |
| 97 | } |
| 98 | |
| 99 | $entity_id = ! empty( $params['entity_id'] ) ? absint( $params['entity_id'] ) : 0; |
| 100 | $user_id = get_current_user_id(); |
| 101 | |
| 102 | if ( ! $entity_id || ! $user_id ) { |
| 103 | return $result; |
| 104 | } |
| 105 | |
| 106 | // Get current likes before the action. |
| 107 | $current_likes = get_post_meta( $entity_id, 'portal_post_likes', true ); |
| 108 | $current_likes = is_array( $current_likes ) ? $current_likes : []; |
| 109 | $user_already_liked = in_array( $user_id, $current_likes, true ); |
| 110 | |
| 111 | // Use shutdown hook to check final like status after processing. |
| 112 | add_action( |
| 113 | 'shutdown', |
| 114 | function() use ( $entity_id, $user_id, $user_already_liked ) { |
| 115 | $new_likes = get_post_meta( $entity_id, 'portal_post_likes', true ); |
| 116 | $new_likes = is_array( $new_likes ) ? $new_likes : []; |
| 117 | $user_now_likes = in_array( $user_id, $new_likes, true ); |
| 118 | |
| 119 | // If user didn't like before but likes now, trigger the event. |
| 120 | if ( ! $user_already_liked && $user_now_likes ) { |
| 121 | $post = get_post( $entity_id ); |
| 122 | if ( ! $post instanceof \WP_Post ) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | $context = WordPress::get_user_context( $user_id ); |
| 127 | $context['post_id'] = $entity_id; |
| 128 | $context['post_title'] = $post->post_title; |
| 129 | $context['post_author'] = $post->post_author; |
| 130 | $context['suredash_post'] = $entity_id; |
| 131 | |
| 132 | AutomationController::sure_trigger_handle_trigger( |
| 133 | [ |
| 134 | 'trigger' => $this->trigger, |
| 135 | 'context' => $context, |
| 136 | ] |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | ); |
| 141 | |
| 142 | return $result; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | SureDashPostLiked::get_instance(); |
| 147 |