AuthController.php
2 years ago
AutomationController.php
3 years ago
EventController.php
3 years ago
EventHelperController.php
3 years ago
GlobalSearchController.php
2 years ago
IntegrationsController.php
3 years ago
OptionController.php
3 years ago
RestController.php
3 years ago
RoutesController.php
3 years ago
SettingsController.php
3 years ago
EventHelperController.php
176 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EventHelperController. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category EventHelperController |
| 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\Controllers; |
| 15 | |
| 16 | use SureTriggers\Traits\SingletonLoader; |
| 17 | |
| 18 | /** |
| 19 | * EventHelperController |
| 20 | * |
| 21 | * @category EventHelperController |
| 22 | * @package SureTriggers |
| 23 | * @author BSF <username@example.com> |
| 24 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 25 | * @link https://www.brainstormforce.com/ |
| 26 | * @since 1.0.0 |
| 27 | * |
| 28 | * @psalm-suppress UndefinedTrait |
| 29 | */ |
| 30 | class EventHelperController { |
| 31 | |
| 32 | use SingletonLoader; |
| 33 | |
| 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | add_shortcode( 'sure_trigger_link', [ $this, 'sure_trigger_link' ] ); |
| 41 | add_action( 'init', [ $this, 'sure_trigger_link_click_action' ] ); |
| 42 | add_shortcode( 'sure_trigger_btn', [ $this, 'sure_trigger_btn' ] ); |
| 43 | add_action( 'init', [ $this, 'sure_trigger_btn_click_action' ] ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Return the HTML template that is displayed by the shortcode |
| 48 | * |
| 49 | * @param array $atts The attributes passed in the the shortcode. |
| 50 | * @param string $content The content contained by the shortcode. |
| 51 | * |
| 52 | * @return string|void|bool |
| 53 | */ |
| 54 | public static function sure_trigger_btn( $atts, $content = null ) { |
| 55 | $atts = shortcode_atts( |
| 56 | [ |
| 57 | 'id' => 0, |
| 58 | 'label' => __( 'Click here', 'suretriggers' ), |
| 59 | ], |
| 60 | $atts, |
| 61 | 'sure_trigger_btn' |
| 62 | ); |
| 63 | |
| 64 | if ( empty( $atts['id'] ) || 0 === intval( $atts['id'] ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | global $post; |
| 69 | $button_args = ''; |
| 70 | if ( ! empty( $post ) && isset( $post->ID ) && isset( $post->post_title ) ) { |
| 71 | $button_args = '<input type="hidden" name="sure_trigger_button_post_id" value="' . $post->ID . '" />'; |
| 72 | $button_args .= '<input type="hidden" name="sure_trigger_button_post_title" value="' . $post->post_title . '" />'; |
| 73 | } |
| 74 | |
| 75 | ob_start(); |
| 76 | ?> |
| 77 | <form method="post" class="sure_trigger_button_form" id="sure_trigger_button_form_<?php echo esc_attr( $atts['id'] ); ?>"> |
| 78 | <input type="hidden" name="sure_trigger_id" value="<?php echo esc_attr( $atts['id'] ); ?>"/><?php echo esc_html( $button_args ); ?> |
| 79 | <input type="hidden" name="sure_trigger_nonce" value="<?php echo esc_attr( wp_create_nonce( 'sure_trigger_btn_nonce' ) ); ?>"/> |
| 80 | <button type="submit" class="sure_trigger_button"><?php echo esc_html( $atts['label'] ); ?></button> |
| 81 | </form> |
| 82 | <?php |
| 83 | return ob_get_clean(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Handle button clicked action. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public static function sure_trigger_btn_click_action() { |
| 92 | if ( ! isset( $_POST['sure_trigger_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['sure_trigger_nonce'] ) ), 'sure_trigger_btn_nonce' ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $user_id = ap_get_current_user_id(); |
| 97 | $ap_btn_trigger_id = isset( $_POST['sure_trigger_id'] ) ? sanitize_text_field( wp_unslash( $_POST['sure_trigger_id'] ) ) : ''; |
| 98 | |
| 99 | do_action( 'sure_trigger_user_clicks_btn', $ap_btn_trigger_id, $user_id ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Return the HTML template that is displayed by the shortcode |
| 104 | * |
| 105 | * @param array $atts The attributes passed in the the shortcode. |
| 106 | * @param string $content The content contained by the shortcode. |
| 107 | * |
| 108 | * @return string|void |
| 109 | */ |
| 110 | public static function sure_trigger_link( $atts, $content = null ) { |
| 111 | $atts = shortcode_atts( |
| 112 | [ |
| 113 | 'id' => 0, |
| 114 | 'text' => __( 'Click here', 'suretriggers' ), |
| 115 | ], |
| 116 | $atts, |
| 117 | 'sure_trigger_link' |
| 118 | ); |
| 119 | |
| 120 | if ( empty( $atts['id'] ) || 0 === intval( $atts['id'] ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $query_args = []; |
| 125 | global $post; |
| 126 | if ( ! empty( $post ) && isset( $post->ID ) && isset( $post->post_title ) ) { |
| 127 | $query_args['ap_link_post_id'] = $post->ID; |
| 128 | } |
| 129 | $query_args['ap_link_trigger_id'] = $atts['id']; |
| 130 | $query_args['ap_link_nonce'] = wp_create_nonce( 'sure_trigger_link_nonce' ); |
| 131 | $link = add_query_arg( $query_args ); |
| 132 | |
| 133 | return '<a class="sure_trigger_link" href="' . $link . '">' . $atts['text'] . '</a>'; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Handle link clicked action. |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | public static function sure_trigger_link_click_action() { |
| 142 | $nonce = isset( $_GET['ap_link_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['ap_link_nonce'] ) ) : ''; |
| 143 | |
| 144 | if ( ! $nonce || ! wp_verify_nonce( $nonce, 'sure_trigger_link_nonce' ) ) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | $user_id = ap_get_current_user_id(); |
| 149 | |
| 150 | $ap_link_trigger_id = absint( isset( $_GET['ap_link_trigger_id'] ) ? sanitize_text_field( wp_unslash( $_GET['ap_link_trigger_id'] ) ) : '' ); |
| 151 | |
| 152 | do_action( 'sure_trigger_user_clicks_link', $ap_link_trigger_id, $user_id ); |
| 153 | $refresh = remove_query_arg( [ 'ap_link_trigger_id', 'ap_link_nonce', 'ap_link_post_id' ] ); |
| 154 | |
| 155 | wp_safe_redirect( $refresh ); |
| 156 | exit(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sure Triggers connection URL. |
| 161 | * |
| 162 | * @return string |
| 163 | */ |
| 164 | public static function get_sure_triggers_url() { |
| 165 | return defined( 'SURE_TRIGGERS_URL' ) ? SURE_TRIGGERS_URL : apply_filters( 'sure_triggers_domain', 'https://staging-sc.bsf.io/' ); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Ignore false positive |
| 172 | * |
| 173 | * @psalm-suppress UndefinedMethod |
| 174 | */ |
| 175 | EventHelperController::get_instance(); |
| 176 |