| @@ -1,0 +1,277 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * WooCommerce Extension | |
| 4 | + * | |
| 5 | + * @package NotificationX\Extensions | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace NotificationX\Extensions\WooCommerce; | |
| 9 | + | |
| 10 | +use NotificationX\Admin\Settings; | |
| 11 | +use NotificationX\Core\Modules; | |
| 12 | +use NotificationX\Core\PostType; | |
| 13 | +use NotificationX\Core\Rules; | |
| 14 | +use NotificationX\GetInstance; | |
| 15 | +use NotificationX\Extensions\Extension; | |
| 16 | +use NotificationX\Extensions\GlobalFields; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * WooCommerce Extension Class | |
| 20 | + */ | |
| 21 | +class WooInline extends WooCommerce { | |
| 22 | + /** | |
| 23 | + * Instance of WooInline | |
| 24 | + * | |
| 25 | + * @var WooInline | |
| 26 | + */ | |
| 27 | + protected static $instance = null; | |
| 28 | + public $priority = 5; | |
| 29 | + public $id = 'woo_inline'; | |
| 30 | + public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/woocommerce.png'; | |
| 31 | + public $doc_link = 'https://notificationx.com/docs/woocommerce-sales-notifications/'; | |
| 32 | + public $types = 'inline'; | |
| 33 | + public $module = 'modules_woocommerce'; | |
| 34 | + public $module_priority = 3; | |
| 35 | + public $class = '\WooCommerce'; | |
| 36 | + public $is_pro = true; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Get the instance of called class. | |
| 40 | + * | |
| 41 | + * @return WooInline | |
| 42 | + */ | |
| 43 | + public static function get_instance($args = null){ | |
| 44 | + if ( is_null( static::$instance ) || ! static::$instance instanceof self ) { | |
| 45 | + $class = __CLASS__; | |
| 46 | + if(strpos($class, "NotificationX\\") === 0){ | |
| 47 | + $pro_class = str_replace("NotificationX\\", "NotificationXPro\\", $class); | |
| 48 | + if(class_exists($pro_class)){ | |
| 49 | + $class = $pro_class; | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + if(!empty($args)){ | |
| 54 | + static::$instance = new $class($args); | |
| 55 | + } | |
| 56 | + else{ | |
| 57 | + static::$instance = new $class; | |
| 58 | + } | |
| 59 | + } | |
| 60 | + return static::$instance; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Initially Invoked when initialized. | |
| 65 | + */ | |
| 66 | + public function __construct(){ | |
| 67 | + parent::__construct(); | |
| 68 | + add_filter( 'nx_show_on_exclude', array( $this, 'show_on_exclude' ), 10, 4 ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + public function init_extension() | |
| 72 | + { | |
| 73 | + | |
| 74 | + $this->themes = [ | |
| 75 | + 'conv-theme-seven' => array( | |
| 76 | + 'is_pro' => true, | |
| 77 | + 'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/pro/woo-inline.jpg', | |
| 78 | + 'image_shape' => 'rounded', | |
| 79 | + 'inline_location' => [ 'woocommerce_before_add_to_cart_form' ], | |
| 80 | + 'template' => [ | |
| 81 | + 'first_param' => 'tag_sales_count', | |
| 82 | + 'custom_first_param' => __( '99', 'notificationx' ), | |
| 83 | + 'second_param' => __( 'people purchased', 'notificationx' ), | |
| 84 | + 'third_param' => 'tag_product_title', | |
| 85 | + 'custom_third_param' => ' ', | |
| 86 | + 'fourth_param' => 'tag_7days', | |
| 87 | + 'custom_fourth_param' => __( 'in last {{day:7}}', 'notificationx' ), | |
| 88 | + ], | |
| 89 | + ), | |
| 90 | + 'stock-theme-one' => array( | |
| 91 | + 'is_pro' => true, | |
| 92 | + 'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/pro/woo-inline-2.jpg', | |
| 93 | + 'image_shape' => 'rounded', | |
| 94 | + 'inline_location' => [ 'woocommerce_before_add_to_cart_form' ], | |
| 95 | + 'template' => [ | |
| 96 | + // 'first_param' => 'tag_sales_count', | |
| 97 | + // 'custom_first_param' => __( 'Someone', 'notificationx' ), | |
| 98 | + 'second_param' => __( 'Only', 'notificationx' ), | |
| 99 | + 'third_param' => 'tag_stock_count', | |
| 100 | + 'custom_third_param' => 10, | |
| 101 | + 'fourth_param' => 'tag_left_in_stock', | |
| 102 | + 'custom_fourth_param' => __( 'left in stock', 'notificationx' ), | |
| 103 | + 'fifth_param' => 'tag_order_soon', | |
| 104 | + 'custom_fifth_param' => __( '- order soon.', 'notificationx' ), | |
| 105 | + ], | |
| 106 | + ), | |
| 107 | + 'stock-theme-two' => array( | |
| 108 | + 'is_pro' => true, | |
| 109 | + 'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/pro/woo-inline-3.jpg', | |
| 110 | + 'image_shape' => 'rounded', | |
| 111 | + 'inline_location' => [ 'woocommerce_after_cart_item_name' ], | |
| 112 | + 'template' => [ | |
| 113 | + // 'first_param' => 'tag_sales_count', | |
| 114 | + // 'custom_first_param' => __( 'Someone', 'notificationx' ), | |
| 115 | + 'second_param' => __( 'In high demand - only', 'notificationx' ), | |
| 116 | + 'third_param' => 'tag_stock_count', | |
| 117 | + 'custom_third_param' => 10, | |
| 118 | + 'fourth_param' => 'tag_left', | |
| 119 | + 'custom_fourth_param' => __( 'left', 'notificationx' ), | |
| 120 | + 'fifth_param' => 'tag_on_our_site', | |
| 121 | + 'custom_fifth_param' => __( 'on our site!', 'notificationx' ), | |
| 122 | + ], | |
| 123 | + ), | |
| 124 | + ]; | |
| 125 | + // Google Analytics live-viewer design. Only offered when the Google | |
| 126 | + // Analytics module is switched on — the number comes from a connected | |
| 127 | + // GA4 property, so without it the design could never render anything. | |
| 128 | + if ( Modules::get_instance()->is_enabled( 'modules_google_analytics' ) ) { | |
| 129 | + $this->themes['live-viewers'] = array( | |
| 130 | + 'is_pro' => true, | |
| 131 | + 'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/pro/ga-live-viewers.jpg', | |
| 132 | + 'image_shape' => 'rounded', | |
| 133 | + 'inline_location' => [ 'woocommerce_before_add_to_cart_form' ], | |
| 134 | + 'template' => [ | |
| 135 | + 'first_param' => 'tag_live_viewers', | |
| 136 | + 'second_param' => __( 'people are viewing', 'notificationx' ), | |
| 137 | + 'third_param' => 'tag_product_title', | |
| 138 | + 'custom_third_param' => ' ', | |
| 139 | + // A real tag rather than `tag_custom`: `custom_fourth_param` | |
| 140 | + // is one text field shared by every design, so seeding it is | |
| 141 | + // not reliable — switching in from the sales-count design can | |
| 142 | + // leave its "in last {{day:7}}" text behind. The tag renders | |
| 143 | + // from `fallback_data()` and cannot go stale; merchants who | |
| 144 | + // want their own wording pick "Custom" in the select, which | |
| 145 | + // seeds from `custom_fourth_param` below. | |
| 146 | + 'fourth_param' => 'tag_right_now', | |
| 147 | + 'custom_fourth_param' => __( 'right now', 'notificationx' ), | |
| 148 | + ], | |
| 149 | + ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + $this->templates = [ | |
| 153 | + 'woo_template_sales_count' => [ | |
| 154 | + 'first_param' => [ | |
| 155 | + 'tag_sales_count' => __( 'Sales Count', 'notificationx' ), | |
| 156 | + ], | |
| 157 | + 'third_param' => [ | |
| 158 | + 'tag_product_title' => __( 'Product Title', 'notificationx' ), | |
| 159 | + ], | |
| 160 | + 'fourth_param' => [ | |
| 161 | + 'tag_1day' => __( 'In last 1 day', 'notificationx' ), | |
| 162 | + 'tag_7days' => __( 'In last 7 days', 'notificationx' ), | |
| 163 | + 'tag_30days' => __( 'In last 30 days', 'notificationx' ), | |
| 164 | + ], | |
| 165 | + '_themes' => [ | |
| 166 | + "{$this->id}_conv-theme-seven", | |
| 167 | + ], | |
| 168 | + ], | |
| 169 | + 'inline_stock_template' => [ | |
| 170 | + 'third_param' => [ | |
| 171 | + 'tag_stock_count' => __( 'Stock Count', 'notificationx' ), | |
| 172 | + ], | |
| 173 | + 'fourth_param' => [ | |
| 174 | + 'tag_left_in_stock' => __( 'left in stock', 'notificationx' ), | |
| 175 | + 'tag_left' => __( 'left', 'notificationx' ), | |
| 176 | + ], | |
| 177 | + 'fifth_param' => [ | |
| 178 | + 'tag_order_soon' => __( 'order soon.', 'notificationx' ), | |
| 179 | + 'tag_on_our_site' => __( 'on our site!', 'notificationx' ), | |
| 180 | + ], | |
| 181 | + '_themes' => [ | |
| 182 | + "{$this->id}_stock-theme-one", | |
| 183 | + "{$this->id}_stock-theme-two", | |
| 184 | + ], | |
| 185 | + ], | |
| 186 | + ]; | |
| 187 | + | |
| 188 | + if ( Modules::get_instance()->is_enabled( 'modules_google_analytics' ) ) { | |
| 189 | + $this->templates['woo_live_viewers_template'] = [ | |
| 190 | + 'first_param' => [ | |
| 191 | + 'tag_live_viewers' => __( 'Live Product Viewers', 'notificationx' ), | |
| 192 | + ], | |
| 193 | + 'third_param' => [ | |
| 194 | + 'tag_product_title' => __( 'Product Title', 'notificationx' ), | |
| 195 | + ], | |
| 196 | + 'fourth_param' => [ | |
| 197 | + 'tag_right_now' => __( 'right now', 'notificationx' ), | |
| 198 | + ], | |
| 199 | + '_themes' => [ | |
| 200 | + "{$this->id}_live-viewers", | |
| 201 | + ], | |
| 202 | + ]; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * The live-viewer design needs a connected GA4 property on top of | |
| 208 | + * WooCommerce, so it gets its own message keyed separately from the | |
| 209 | + * parent's "install WooCommerce" one and scoped to that theme. | |
| 210 | + * | |
| 211 | + * @param array $messages | |
| 212 | + * @return array | |
| 213 | + */ | |
| 214 | + public function source_error_message( $messages ) { | |
| 215 | + $messages = parent::source_error_message( $messages ); | |
| 216 | + | |
| 217 | + $url = admin_url( 'admin.php?page=nx-settings&tab=tab-api-integrations#google_analytics_settings_section' ); | |
| 218 | + $pa_settings = Settings::get_instance()->get( 'settings.nx_pa_settings' ); | |
| 219 | + $profile = Settings::get_instance()->get( 'settings.ga_profile' ); | |
| 220 | + $message = ''; | |
| 221 | + | |
| 222 | + if ( empty( $pa_settings ) ) { | |
| 223 | + /* translators: %1$s: leading sentence, %2$s: settings URL, %3$s: link text, %4$s: trailing word. */ | |
| 224 | + $message = sprintf( '%1$s <a href="%2$s" target="_blank">%3$s</a> %4$s.', | |
| 225 | + __( 'This design needs live traffic data. Connect your', 'notificationx' ), | |
| 226 | + $url, | |
| 227 | + __( 'Google Analytics Account', 'notificationx' ), | |
| 228 | + __( 'first', 'notificationx' ) | |
| 229 | + ); | |
| 230 | + } elseif ( empty( $profile ) || strpos( $profile, 'properties/' ) !== 0 ) { | |
| 231 | + // Realtime per-page data only exists on GA4 properties; legacy | |
| 232 | + // Universal Analytics views cannot serve this design. | |
| 233 | + /* translators: %1$s: leading sentence, %2$s: settings URL, %3$s: link text. */ | |
| 234 | + $message = sprintf( '%1$s <a href="%2$s" target="_blank">%3$s</a>.', | |
| 235 | + __( 'This design needs a Google Analytics 4 property. Select one in', 'notificationx' ), | |
| 236 | + $url, | |
| 237 | + __( 'API Integrations', 'notificationx' ) | |
| 238 | + ); | |
| 239 | + } | |
| 240 | + | |
| 241 | + if ( $message ) { | |
| 242 | + $messages[ "{$this->id}_ga" ] = [ | |
| 243 | + 'message' => $message, | |
| 244 | + 'html' => true, | |
| 245 | + 'type' => 'error', | |
| 246 | + 'rules' => Rules::includes( 'themes', [ "{$this->id}_live-viewers" ] ), | |
| 247 | + ]; | |
| 248 | + } | |
| 249 | + | |
| 250 | + return $messages; | |
| 251 | + } | |
| 252 | + | |
| 253 | + /** | |
| 254 | + * Keep this source's notifications out of the popup loop in | |
| 255 | + * FrontEnd::get_notifications_ids(). They render inline at the hooks | |
| 256 | + * chosen in `inline_location`, never as a floating popup. | |
| 257 | + * | |
| 258 | + * `inline_location` is not read here on purpose: it can be saved as '' | |
| 259 | + * (MCP or Quick Builder create without the field), and passing that to | |
| 260 | + * array_diff() is a TypeError on PHP 8 that white-screens every page. | |
| 261 | + * | |
| 262 | + * @param bool $exclude Whether an earlier callback already excluded it. | |
| 263 | + * @param array $settings Notification settings. | |
| 264 | + * @return bool | |
| 265 | + */ | |
| 266 | + public function show_on_exclude( $exclude, $settings ) { | |
| 267 | + if ( isset( $settings['source'] ) && $this->id === $settings['source'] ) { | |
| 268 | + return true; | |
| 269 | + } | |
| 270 | + return $exclude; | |
| 271 | + } | |
| 272 | + | |
| 273 | + public function content_fields($fields){ | |
| 274 | + return $fields; | |
| 275 | + } | |
| 276 | + | |
| 277 | +} | |