← All changes
|
integrations/wordpress/triggers/view-post-category.php
+139
-0
5.7.8
→
6.0.3
View file →
| @@ -1,0 +1,139 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * View Post Category | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Triggers\View_Post_Category | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_WordPress_View_Post_Category extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Initialize the trigger | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + */ | |
| 19 | + public function __construct( $integration ) { | |
| 20 | + | |
| 21 | + $this->integration = $integration; | |
| 22 | + $this->trigger = $integration . '_view_post_category'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register the trigger | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + */ | |
| 33 | + public function register() { | |
| 34 | + | |
| 35 | + automatorwp_register_trigger( $this->trigger, array( | |
| 36 | + 'integration' => $this->integration, | |
| 37 | + 'label' => __( 'User views a post of a category', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'User views a post of <strong>a category</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Term title. %2$s: Number of times. */ | |
| 40 | + 'edit_label' => sprintf( __( 'User views a post of %1$s %2$s time(s)', 'automatorwp' ), '{term}', '{times}' ), | |
| 41 | + /* translators: %1$s: Term title. */ | |
| 42 | + 'log_label' => sprintf( __( 'User views a post of %1$s', 'automatorwp' ), '{term}' ), | |
| 43 | + 'action' => 'template_redirect', | |
| 44 | + 'function' => array( $this, 'listener' ), | |
| 45 | + 'priority' => 10, | |
| 46 | + 'accepted_args' => 1, | |
| 47 | + 'options' => array( | |
| 48 | + 'term' => automatorwp_utilities_term_option(), | |
| 49 | + 'times' => automatorwp_utilities_times_option(), | |
| 50 | + ), | |
| 51 | + 'tags' => array_merge( | |
| 52 | + automatorwp_utilities_post_tags(), | |
| 53 | + automatorwp_utilities_times_tag() | |
| 54 | + ) | |
| 55 | + ) ); | |
| 56 | + | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Trigger listener | |
| 61 | + * | |
| 62 | + * @since 1.0.0 | |
| 63 | + */ | |
| 64 | + public function listener() { | |
| 65 | + | |
| 66 | + global $post; | |
| 67 | + | |
| 68 | + // Bail if in admin area | |
| 69 | + if( is_admin() ) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + | |
| 73 | + // Bail if not post instanced | |
| 74 | + if( ! $post instanceof WP_Post ) { | |
| 75 | + return; | |
| 76 | + } | |
| 77 | + | |
| 78 | + // Bail if post type is not a post | |
| 79 | + if( $post->post_type !== 'post' ) { | |
| 80 | + return; | |
| 81 | + } | |
| 82 | + | |
| 83 | + $terms_ids = automatorwp_get_term_ids( $post->ID, 'category' ); | |
| 84 | + | |
| 85 | + // Bail if post isn't assigned to any category | |
| 86 | + if( empty( $terms_ids ) ) { | |
| 87 | + return; | |
| 88 | + } | |
| 89 | + | |
| 90 | + $user_id = get_current_user_id(); | |
| 91 | + | |
| 92 | + // Bail if user is not logged in | |
| 93 | + if( $user_id === 0 ) { | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + | |
| 97 | + automatorwp_trigger_event( array( | |
| 98 | + 'trigger' => $this->trigger, | |
| 99 | + 'user_id' => $user_id, | |
| 100 | + 'post_id' => $post->ID, | |
| 101 | + 'terms_ids' => $terms_ids, | |
| 102 | + ) ); | |
| 103 | + | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * User deserves check | |
| 108 | + * | |
| 109 | + * @since 1.0.0 | |
| 110 | + * | |
| 111 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 112 | + * @param stdClass $trigger The trigger object | |
| 113 | + * @param int $user_id The user ID | |
| 114 | + * @param array $event Event information | |
| 115 | + * @param array $trigger_options The trigger's stored options | |
| 116 | + * @param stdClass $automation The trigger's automation object | |
| 117 | + * | |
| 118 | + * @return bool True if user deserves trigger, false otherwise | |
| 119 | + */ | |
| 120 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 121 | + | |
| 122 | + // Don't deserve if post and terms IDs are not received | |
| 123 | + if( ! isset( $event['post_id'] ) && ! isset( $event['terms_ids'] ) ) { | |
| 124 | + return false; | |
| 125 | + } | |
| 126 | + | |
| 127 | + // Don't deserve if term doesn't match with the trigger option | |
| 128 | + if( ! automatorwp_terms_matches( $event['terms_ids'], $trigger_options['term'] ) ) { | |
| 129 | + return false; | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $deserves_trigger; | |
| 133 | + | |
| 134 | + } | |
| 135 | + | |
| 136 | +} | |
| 137 | + | |
| 138 | +new AutomatorWP_WordPress_View_Post_Category( 'wordpress' ); | |
| 139 | +new AutomatorWP_WordPress_View_Post_Category( 'posts' ); | |