Integrations
6 years ago
Products
6 years ago
Utilities
6 years ago
test
6 years ago
AJAX.php
6 years ago
Admin.php
6 years ago
Lifecycle.php
6 years ago
Products.php
6 years ago
fbasync.php
6 years ago
fbbackground.php
6 years ago
fbgraph.php
6 years ago
fbinfobanner.php
6 years ago
fbproduct.php
6 years ago
fbproductfeed.php
6 years ago
fbutils.php
6 years ago
fbwpml.php
6 years ago
fbinfobanner.php
272 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 4 | * |
| 5 | * This source code is licensed under the license found in the |
| 6 | * LICENSE file in the root directory of this source tree. |
| 7 | * |
| 8 | * @package FacebookCommerce |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'WC_Facebookcommerce_Utils' ) ) { |
| 16 | include_once 'includes/fbutils.php'; |
| 17 | } |
| 18 | |
| 19 | if ( ! class_exists( 'WC_Facebookcommerce_Info_Banner' ) ) : |
| 20 | |
| 21 | /** |
| 22 | * FB Info Banner class |
| 23 | */ |
| 24 | class WC_Facebookcommerce_Info_Banner { |
| 25 | |
| 26 | const FB_NO_TIP_EXISTS = 'No Tip Exist!'; |
| 27 | const DEFAULT_TIP_IMG_URL_PREFIX = 'https://www.facebook.com'; |
| 28 | const CHANNEL_ID = 2087541767986590; |
| 29 | |
| 30 | /** @var object Class Instance */ |
| 31 | private static $instance; |
| 32 | |
| 33 | /** @var string If the banner has been dismissed */ |
| 34 | private $external_merchant_settings_id; |
| 35 | private $fbgraph; |
| 36 | private $should_query_tip; |
| 37 | |
| 38 | /** |
| 39 | * Get the class instance |
| 40 | */ |
| 41 | public static function get_instance( |
| 42 | $external_merchant_settings_id, |
| 43 | $fbgraph, |
| 44 | $should_query_tip = false ) { |
| 45 | return null === self::$instance |
| 46 | ? ( self::$instance = new self( |
| 47 | $external_merchant_settings_id, |
| 48 | $fbgraph, |
| 49 | $should_query_tip |
| 50 | ) ) |
| 51 | : self::$instance; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Constructor |
| 56 | */ |
| 57 | public function __construct( |
| 58 | $external_merchant_settings_id, |
| 59 | $fbgraph, |
| 60 | $should_query_tip = false ) { |
| 61 | $this->should_query_tip = $should_query_tip; |
| 62 | $this->external_merchant_settings_id = $external_merchant_settings_id; |
| 63 | $this->fbgraph = $fbgraph; |
| 64 | add_action( 'wp_ajax_ajax_woo_infobanner_post_click', array( $this, 'ajax_woo_infobanner_post_click' ) ); |
| 65 | add_action( 'wp_ajax_ajax_woo_infobanner_post_xout', array( $this, 'ajax_woo_infobanner_post_xout' ) ); |
| 66 | add_action( 'admin_notices', array( $this, 'banner' ) ); |
| 67 | add_action( 'admin_init', array( $this, 'dismiss_banner' ) ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Post click event when hit primary button. |
| 72 | */ |
| 73 | function ajax_woo_infobanner_post_click() { |
| 74 | WC_Facebookcommerce_Utils::check_woo_ajax_permissions( |
| 75 | 'post tip click event', |
| 76 | true |
| 77 | ); |
| 78 | check_ajax_referer( 'wc_facebook_infobanner_jsx' ); |
| 79 | $tip_info = WC_Facebookcommerce_Utils::get_cached_best_tip(); |
| 80 | $tip_id = isset( $tip_info->tip_id ) |
| 81 | ? $tip_info->tip_id |
| 82 | : null; |
| 83 | if ( $tip_id == null ) { |
| 84 | WC_Facebookcommerce_Utils::fblog( |
| 85 | 'Do not have tip id when click, sth went wrong', |
| 86 | array( 'tip_info' => $tip_info ), |
| 87 | true |
| 88 | ); |
| 89 | } else { |
| 90 | WC_Facebookcommerce_Utils::tip_events_log( |
| 91 | $tip_id, |
| 92 | self::CHANNEL_ID, |
| 93 | 'click' |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Post xout event when hit dismiss button. |
| 100 | */ |
| 101 | function ajax_woo_infobanner_post_xout() { |
| 102 | WC_Facebookcommerce_Utils::check_woo_ajax_permissions( |
| 103 | 'post tip xout event', |
| 104 | true |
| 105 | ); |
| 106 | check_ajax_referer( 'wc_facebook_infobanner_jsx' ); |
| 107 | $tip_info = WC_Facebookcommerce_Utils::get_cached_best_tip(); |
| 108 | $tip_id = isset( $tip_info->tip_id ) |
| 109 | ? $tip_info->tip_id |
| 110 | : null; |
| 111 | // Delete cached tip if xout. |
| 112 | update_option( 'fb_info_banner_last_best_tip', '' ); |
| 113 | if ( $tip_id == null ) { |
| 114 | WC_Facebookcommerce_Utils::fblog( |
| 115 | 'Do not have tip id when xout, sth went wrong', |
| 116 | array( 'tip_info' => $tip_info ), |
| 117 | true |
| 118 | ); |
| 119 | } else { |
| 120 | WC_Facebookcommerce_Utils::tip_events_log( |
| 121 | $tip_id, |
| 122 | self::CHANNEL_ID, |
| 123 | 'xout' |
| 124 | ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Display a info banner on Woocommerce pages. |
| 130 | */ |
| 131 | public function banner() { |
| 132 | $screen = get_current_screen(); |
| 133 | if ( ! in_array( |
| 134 | $screen->base, |
| 135 | array( |
| 136 | 'woocommerce_page_wc-reports', |
| 137 | 'woocommerce_page_wc-settings', |
| 138 | 'woocommerce_page_wc-status', |
| 139 | ) |
| 140 | ) || |
| 141 | $screen->is_network || $screen->action ) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | $tip_info = null; |
| 146 | if ( ! $this->should_query_tip ) { |
| 147 | // If last query is less than 1 day, either has last best tip or default |
| 148 | // tip pass time cap. |
| 149 | $tip_info = WC_Facebookcommerce_Utils::get_cached_best_tip(); |
| 150 | } else { |
| 151 | $tip_info = $this->fbgraph->get_tip_info( |
| 152 | $this->external_merchant_settings_id |
| 153 | ); |
| 154 | update_option( 'fb_info_banner_last_query_time', current_time( 'mysql' ) ); |
| 155 | } |
| 156 | |
| 157 | // Not render if no cached best tip, or no best tip returned from FB. |
| 158 | if ( ! $tip_info || ( $tip_info === self::FB_NO_TIP_EXISTS ) ) { |
| 159 | // Delete cached tip if should query and get no tip. |
| 160 | delete_option( 'fb_info_banner_last_best_tip' ); |
| 161 | return; |
| 162 | } else { |
| 163 | // Get tip creatives via API |
| 164 | if ( is_string( $tip_info ) ) { |
| 165 | $tip_info = WC_Facebookcommerce_Utils::decode_json( $tip_info ); |
| 166 | } |
| 167 | $tip_title = isset( $tip_info->tip_title->__html ) |
| 168 | ? $tip_info->tip_title->__html |
| 169 | : null; |
| 170 | |
| 171 | $tip_body = isset( $tip_info->tip_body->__html ) |
| 172 | ? $tip_info->tip_body->__html |
| 173 | : null; |
| 174 | |
| 175 | $tip_action_link = isset( $tip_info->tip_action_link ) |
| 176 | ? $tip_info->tip_action_link |
| 177 | : null; |
| 178 | |
| 179 | $tip_action = isset( $tip_info->tip_action->__html ) |
| 180 | ? $tip_info->tip_action->__html |
| 181 | : null; |
| 182 | |
| 183 | $tip_img_url = isset( $tip_info->tip_img_url ) |
| 184 | ? self::DEFAULT_TIP_IMG_URL_PREFIX . $tip_info->tip_img_url |
| 185 | : null; |
| 186 | |
| 187 | if ( $tip_title == null || $tip_body == null || $tip_action_link == null |
| 188 | || $tip_action == null || $tip_action == null ) { |
| 189 | WC_Facebookcommerce_Utils::fblog( |
| 190 | 'Unexpected response from FB for tip info.', |
| 191 | array( 'tip_info' => $tip_info ), |
| 192 | true |
| 193 | ); |
| 194 | return; |
| 195 | } |
| 196 | update_option( |
| 197 | 'fb_info_banner_last_best_tip', |
| 198 | is_object( $tip_info ) || is_array( $tip_info ) |
| 199 | ? json_encode( $tip_info ) : $tip_info |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | $dismiss_url = $this->dismiss_url(); |
| 204 | |
| 205 | echo '<div class="updated fade">'; |
| 206 | echo '<div id="fbinfobanner">'; |
| 207 | echo '<div><img src="' . esc_url( $tip_img_url ) . '" class="iconDetails"></div>'; |
| 208 | echo '<p class = "tipTitle"><strong>' . esc_html( $tip_title ) . "</strong></p>\n"; |
| 209 | echo '<p class = "tipContent">' . esc_html( $tip_body ) . '</p>'; |
| 210 | echo '<p class = "tipButton">'; |
| 211 | echo '<a href="' . esc_url( $tip_action_link ) . '" class = "btn" onclick="fb_woo_infobanner_post_click(); return true;" title="' . esc_attr__( 'Click and redirect.', 'facebook-for-woocommerce' ) . '"> ' . esc_html( $tip_action ) . '</a>'; |
| 212 | echo '<a href="' . esc_url( $dismiss_url ) . '" class = "btn dismiss grey" onclick="fb_woo_infobanner_post_xout(); return true;" title="' . esc_attr__( 'Dismiss this notice.', 'facebook-for-woocommerce' ) . '"> ' . esc_html__( 'Dismiss', 'facebook-for-woocommerce' ) . '</a>'; |
| 213 | echo '</p></div></div>'; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns the url that the user clicks to remove the info banner |
| 218 | * |
| 219 | * @return (string) |
| 220 | */ |
| 221 | private function dismiss_url() { |
| 222 | $url = admin_url( 'admin.php' ); |
| 223 | |
| 224 | $url = add_query_arg( |
| 225 | array( |
| 226 | 'page' => 'wc-settings', |
| 227 | 'tab' => 'integration', |
| 228 | 'wc-notice' => 'dismiss-fb-info-banner', |
| 229 | ), |
| 230 | $url |
| 231 | ); |
| 232 | |
| 233 | return wp_nonce_url( $url, 'woocommerce_info_banner_dismiss' ); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * Handles the action that dismisses the info banner. |
| 239 | * |
| 240 | * The banner will remain dismissed for at least one day and until a new info tip can be retrieved. |
| 241 | * |
| 242 | * @see \WC_Facebookcommerce_Integration::FB_TIP_QUERY |
| 243 | * @see \WC_Facebookcommerce_Graph_API::get_tip_info() |
| 244 | */ |
| 245 | public function dismiss_banner() { |
| 246 | |
| 247 | if ( ! isset( $_GET['wc-notice'] ) ) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | if ( 'dismiss-fb-info-banner' !== $_GET['wc-notice'] ) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if ( ! check_admin_referer( 'woocommerce_info_banner_dismiss' ) ) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | // Delete cached tip if xout. |
| 260 | delete_option( 'fb_info_banner_last_best_tip' ); |
| 261 | if ( wp_get_referer() ) { |
| 262 | wp_safe_redirect( wp_get_referer() ); |
| 263 | } else { |
| 264 | wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=integration' ) ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | } |
| 270 | |
| 271 | endif; |
| 272 |