← All changes
|
integrations/popup-maker/includes/actions/show-popup.php
+139
-0
5.7.4
→
6.0.4
View file →
| @@ -1,0 +1,139 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Role | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Popup_Maker\Actions\Show_Popup | |
| 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_Popup_Maker_Show_Popup extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'popup_maker'; | |
| 15 | + public $action = 'popup_maker_show_popup'; | |
| 16 | + public $meta_key = 'automatorwp_popup_maker_popups'; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Register the trigger | |
| 20 | + * | |
| 21 | + * @since 1.0.0 | |
| 22 | + */ | |
| 23 | + public function register() { | |
| 24 | + | |
| 25 | + automatorwp_register_action( $this->action, array( | |
| 26 | + 'integration' => $this->integration, | |
| 27 | + 'label' => __( 'Show a popup', 'automatorwp' ), | |
| 28 | + 'select_option' => __( 'Show a <strong>popup</strong>', 'automatorwp' ), | |
| 29 | + /* translators: %1$s: Post title. */ | |
| 30 | + 'edit_label' => sprintf( __( 'Show %1$s', 'automatorwp' ), '{post}' ), | |
| 31 | + /* translators: %1$s: Post title. */ | |
| 32 | + 'log_label' => sprintf( __( 'Show %1$s', 'automatorwp' ), '{post}' ), | |
| 33 | + 'options' => array( | |
| 34 | + 'post' => automatorwp_utilities_post_option( array( | |
| 35 | + 'name' => __( 'Popup:', 'automatorwp' ), | |
| 36 | + 'option_default' => __( 'a popup', 'automatorwp' ), | |
| 37 | + 'option_none' => false, | |
| 38 | + 'option_custom' => true, | |
| 39 | + 'option_custom_desc' => __( 'Popup ID', 'automatorwp' ), | |
| 40 | + 'post_type' => 'popup' | |
| 41 | + ) ), | |
| 42 | + ), | |
| 43 | + ) ); | |
| 44 | + | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Register required hooks | |
| 49 | + * | |
| 50 | + * @since 1.0.0 | |
| 51 | + */ | |
| 52 | + public function hooks() { | |
| 53 | + | |
| 54 | + add_filter( 'pum_popup_is_loadable', array( $this, 'maybe_load_popup' ), 10, 2 ); | |
| 55 | + | |
| 56 | + parent::hooks(); | |
| 57 | + | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Action execution function | |
| 62 | + * | |
| 63 | + * @since 1.0.0 | |
| 64 | + * | |
| 65 | + * @param stdClass $action The action object | |
| 66 | + * @param int $user_id The user ID | |
| 67 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 68 | + * @param stdClass $automation The action's automation object | |
| 69 | + */ | |
| 70 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 71 | + | |
| 72 | + // Shorthand | |
| 73 | + $post_id = absint( $action_options['post'] ); | |
| 74 | + | |
| 75 | + $post = get_post( $post_id ); | |
| 76 | + | |
| 77 | + // Bail if post doesn't exists | |
| 78 | + if( ! $post ) { | |
| 79 | + return; | |
| 80 | + } | |
| 81 | + | |
| 82 | + // Get popups to show | |
| 83 | + $popups_to_show = get_user_meta( $user_id, $this->meta_key, true ); | |
| 84 | + | |
| 85 | + if( ! is_array( $popups_to_show ) ) { | |
| 86 | + $popups_to_show = array(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + $popups_to_show[] = $post_id; | |
| 90 | + | |
| 91 | + // Update popups to show | |
| 92 | + update_user_meta( $user_id, $this->meta_key, $popups_to_show ); | |
| 93 | + | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Determine if popup should be displayed to the user | |
| 98 | + * | |
| 99 | + * @since 1.0.0 | |
| 100 | + * | |
| 101 | + * @param bool $loadable | |
| 102 | + * @param int $post_id | |
| 103 | + * | |
| 104 | + * @return bool | |
| 105 | + */ | |
| 106 | + public function maybe_load_popup( $loadable, $post_id ) { | |
| 107 | + | |
| 108 | + global $wpdb; | |
| 109 | + | |
| 110 | + // Ensure post ID as integer | |
| 111 | + $post_id = absint( $post_id ); | |
| 112 | + | |
| 113 | + $actions_meta = AutomatorWP()->db->actions_meta; | |
| 114 | + | |
| 115 | + // Check if any action is using this popup | |
| 116 | + $in_use = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$actions_meta} AS am WHERE am.meta_key = 'post' AND am.meta_value = {$post_id}" ) ); | |
| 117 | + | |
| 118 | + // Bail if none action is using this | |
| 119 | + if( $in_use === 0 ) { | |
| 120 | + return $loadable; | |
| 121 | + } | |
| 122 | + | |
| 123 | + $user_id = get_current_user_id(); | |
| 124 | + | |
| 125 | + // Get popups to show | |
| 126 | + $popups_to_show = get_user_meta( $user_id, $this->meta_key, true ); | |
| 127 | + | |
| 128 | + if( ! is_array( $popups_to_show ) ) { | |
| 129 | + $popups_to_show = array(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + // If popup is on popups to show, then make it loadable | |
| 133 | + return in_array( $post_id, $popups_to_show ); | |
| 134 | + | |
| 135 | + } | |
| 136 | + | |
| 137 | +} | |
| 138 | + | |
| 139 | +new AutomatorWP_Popup_Maker_Show_Popup(); | |