| @@ -1,0 +1,309 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace NotificationX\Core; | |
| 4 | + | |
| 5 | +use NotificationX\Core\PostType; | |
| 6 | +use NotificationX\FrontEnd\FrontEnd; | |
| 7 | +use NotificationX\FrontEnd\Preview; | |
| 8 | +use NotificationX\GetInstance; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Class Shortcode For NotificationX Pro | |
| 12 | + * @method static Inline get_instance($args = null) | |
| 13 | + * | |
| 14 | + * @since 1.2.3 | |
| 15 | + */ | |
| 16 | +class Inline { | |
| 17 | + /** | |
| 18 | + * Instance of Shortcode | |
| 19 | + * | |
| 20 | + * @var Shortcode | |
| 21 | + */ | |
| 22 | + use GetInstance; | |
| 23 | + | |
| 24 | + public $notifications_data = []; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Request-level cache of notifications data, keyed by source. | |
| 28 | + * Inline hooks fire once per product in shop/archive loops; the | |
| 29 | + * result only varies by $source, so compute it once per request. | |
| 30 | + * | |
| 31 | + * @var array | |
| 32 | + */ | |
| 33 | + protected $notifications_cache = []; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * __construct__ is for revoke first time to get ready | |
| 37 | + * | |
| 38 | + * @return void | |
| 39 | + */ | |
| 40 | + public function __construct() { | |
| 41 | + // InlineWooCommerce::get_instance(); | |
| 42 | + // InlineEDD::get_instance(); | |
| 43 | + } | |
| 44 | + | |
| 45 | + | |
| 46 | + public function get_notifications_data( $source, $id = null, $settings = [] ) { | |
| 47 | + | |
| 48 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 49 | + | |
| 50 | + $exit = apply_filters('nx_inline_notifications_data', null, $source, $id, $settings); | |
| 51 | + if($exit){ | |
| 52 | + return $exit; | |
| 53 | + } | |
| 54 | + | |
| 55 | + if ( ! isset( $this->notifications_cache[ $source ] ) ) { | |
| 56 | + $data = array( 'shortcode' => array() ); | |
| 57 | + $notifications = PostType::get_instance()->get_posts( | |
| 58 | + array( | |
| 59 | + 'source' => $source, | |
| 60 | + 'enabled' => true, | |
| 61 | + 'is_inline' => true, | |
| 62 | + ) | |
| 63 | + ); | |
| 64 | + | |
| 65 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 66 | + do_action( 'nx_inline' ); | |
| 67 | + | |
| 68 | + if ( ! empty( $notifications ) ) { | |
| 69 | + $data = FrontEnd::get_instance()->get_notifications_data( | |
| 70 | + array( | |
| 71 | + 'shortcode' => array_column( $notifications, 'nx_id' ), | |
| 72 | + 'inline_shortcode' => true, | |
| 73 | + ) | |
| 74 | + ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + $this->notifications_cache[ $source ] = $data; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $this->notifications_data = $this->notifications_cache[ $source ]; | |
| 81 | + return $this->notifications_data; | |
| 82 | + } | |
| 83 | + | |
| 84 | + | |
| 85 | + public function get_template( $settings ) { | |
| 86 | + if ( ! empty( $settings['template_adv'] ) && ! empty( $settings['advanced_template'] ) ) { | |
| 87 | + if( strpos($settings['advanced_template'], '{{random:') !== false ) { | |
| 88 | + $productId = get_the_ID(); | |
| 89 | + return $this->generateRandomNumber( $settings['advanced_template'], $productId ); | |
| 90 | + } | |
| 91 | + return $settings['advanced_template']; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $template = ''; | |
| 95 | + $defaults = array( | |
| 96 | + 'first_param' => '', | |
| 97 | + 'second_param' => '', | |
| 98 | + 'third_param' => '', | |
| 99 | + 'fourth_param' => '', | |
| 100 | + 'fifth_param' => '', | |
| 101 | + 'sixth_param' => '', | |
| 102 | + 'map_fourth_param' => '', | |
| 103 | + 'ga_fourth_param' => '', | |
| 104 | + 'ga_fifth_param' => '', | |
| 105 | + 'review_fourth_param' => '', | |
| 106 | + 'freemius_fifth_param' => '', | |
| 107 | + 'freemius_sixth_param' => '', | |
| 108 | + 'freemius_seventh_param' => '', | |
| 109 | + ); | |
| 110 | + | |
| 111 | + $theme_name = $settings['themes']; | |
| 112 | + $theme_name = str_replace( $settings['source'] . '_', '', $theme_name ); | |
| 113 | + $theme_name = str_replace( $settings['type'] . '_', '', $theme_name ); | |
| 114 | + if ( ! empty( $settings['custom_type'] ) ) { | |
| 115 | + $theme_name = str_replace( $settings['custom_type'] . '_', '', $theme_name ); | |
| 116 | + } | |
| 117 | + $params = wp_parse_args( isset( $settings['notification-template'] ) && is_array( $settings['notification-template'] ) ? $settings['notification-template'] : array(), $defaults ); | |
| 118 | + | |
| 119 | + foreach ( $params as $param => $element ) { | |
| 120 | + if ( $element == 'tag_custom' && isset( $params[ 'custom_' . $param ] ) ) { | |
| 121 | + // getting value of custom params. | |
| 122 | + $element = $params[ 'custom_' . $param ]; | |
| 123 | + } | |
| 124 | + if ( $element == 'tag_siteview' || $element == 'tag_realtime_siteview' ) { | |
| 125 | + $params[ $param ] = '{{views}}'; | |
| 126 | + } elseif ( $element == 'ga_title' ) { | |
| 127 | + $params[ $param ] = '{{title}}'; | |
| 128 | + } elseif ( strpos( $element, 'tag_product_' ) === 0 ) { | |
| 129 | + $params[ $param ] = '{{' . str_replace( 'tag_product_', '', $element ) . '}}'; | |
| 130 | + } elseif ( strpos( $element, 'tag_' ) === 0 ) { | |
| 131 | + $params[ $param ] = '{{' . str_replace( 'tag_', '', $element ) . '}}'; | |
| 132 | + } elseif ( strpos( $element, 'product_' ) === 0 ) { | |
| 133 | + $params[ $param ] = '{{' . str_replace( 'product_', '', $element ) . '}}'; | |
| 134 | + } else { | |
| 135 | + $params[ $param ] = $element; | |
| 136 | + } | |
| 137 | + if(Preview::get_instance()->is_preview()){ | |
| 138 | + $params[ $param ] = "<span class='$param'>{$params[ $param ]}</span>"; | |
| 139 | + } | |
| 140 | + } | |
| 141 | + | |
| 142 | + switch ( $settings['themes'] ) { | |
| 143 | + case 'donation_theme-one': | |
| 144 | + case 'donation_theme-two': | |
| 145 | + case 'donation_theme-three': | |
| 146 | + case 'donation_theme-four': | |
| 147 | + case 'donation_theme-five': | |
| 148 | + return "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']} {$params['fifth_param']}"; | |
| 149 | + case 'donation_conv-theme-seven': | |
| 150 | + case 'donation_conv-theme-eight': | |
| 151 | + case 'donation_conv-theme-nine': | |
| 152 | + return "{$params['first_param']} {$params['second_param']} in {$params['third_param']} {$params['fourth_param']}"; | |
| 153 | + case 'google_reviews_maps_theme': | |
| 154 | + return "{$params['first_param']} {$params['second_param']} in {$params['third_param']} {$params['fourth_param']}"; | |
| 155 | + case 'woocommerce_sales_inline_conv-theme-seven': | |
| 156 | + case 'fluentcart_inline_conv-theme-seven': | |
| 157 | + case 'woo_inline_conv-theme-seven': | |
| 158 | + return "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 159 | + case 'youtube_channel-1': | |
| 160 | + return "{$params['second_param']} {$params['third_param']} {$params['yt_third_label']}, {$params['fourth_param']} {$params['yt_fourth_label']} {$params['fifth_param']} {$params['yt_fifth_label']}"; | |
| 161 | + case 'youtube_channel-2': | |
| 162 | + return "{$params['second_param']} {$params['third_param']} {$params['yt_third_label']}, {$params['fourth_param']} {$params['fifth_param']}"; | |
| 163 | + case 'youtube_video-1': | |
| 164 | + case 'youtube_video-3': | |
| 165 | + return "{$params['second_param']}, {$params['third_param']} {$params['fourth_param']} {$params['fifth_param']}"; | |
| 166 | + case 'youtube_video-2': | |
| 167 | + case 'youtube_video-4': | |
| 168 | + return "{$params['second_param']}, {$params['third_param']} {$params['yt_third_label']} {$params['fourth_param']} {$params['yt_fourth_label']} {$params['fifth_param']} {$params['yt_fifth_label']}"; | |
| 169 | + | |
| 170 | + } | |
| 171 | + | |
| 172 | + if ($settings['source'] === 'freemius_conversions') { | |
| 173 | + switch ( $theme_name ) { | |
| 174 | + case "theme-one": | |
| 175 | + case "theme-two": | |
| 176 | + case "theme-three": | |
| 177 | + case "theme-four": | |
| 178 | + case "theme-five": | |
| 179 | + return ["{$params['first_param']} {$params['second_param']}", "{$params['third_param']} {$params['freemius_fifth_param']} {$params['fifth_param']} {$params['freemius_sixth_param']} {$params['freemius_seventh_param']}", "{$params['fourth_param']}"]; | |
| 180 | + case "conv-theme-ten": | |
| 181 | + case "conv-theme-eleven": | |
| 182 | + return ["{$params['first_param']} {$params['second_param']}", "{$params['third_param']} {$params['freemius_fifth_param']} {$params['freemius_sixth_param']} {$params['freemius_seventh_param']}", "{$params['fourth_param']}"]; | |
| 183 | + // conversion start | |
| 184 | + case "conv-theme-six": | |
| 185 | + return ["{$params['first_param']} {$params['second_param']} {$params['third_param']}", "{$params['map_fourth_param']} {$params['fourth_param']} {$params['freemius_fifth_param']} {$params['freemius_sixth_param']} {$params['freemius_seventh_param']}", "{$params['fifth_param']}"]; | |
| 186 | + case "conv-theme-seven": | |
| 187 | + case "conv-theme-eight": | |
| 188 | + case "conv-theme-nine": | |
| 189 | + return ["{$params['first_param']} {$params['second_param']}", "{$params['third_param']} {$params['fourth_param']} {$params['freemius_fifth_param']} {$params['freemius_sixth_param']} {$params['freemius_seventh_param']}"]; | |
| 190 | + } | |
| 191 | + } | |
| 192 | + switch ( $theme_name ) { | |
| 193 | + case 'theme-one': | |
| 194 | + case 'theme-two': | |
| 195 | + case 'theme-three': | |
| 196 | + case 'theme-four': | |
| 197 | + case 'theme-five': | |
| 198 | + case "conv-theme-ten": | |
| 199 | + case "conv-theme-eleven": | |
| 200 | + case 'conv-theme-seven': | |
| 201 | + case 'conv-theme-eight': | |
| 202 | + case 'conv-theme-nine': | |
| 203 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 204 | + break; | |
| 205 | + // conversion start | |
| 206 | + case 'conv-theme-six': | |
| 207 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['map_fourth_param']} {$params['fourth_param']} {$params['fifth_param']}"; | |
| 208 | + break; | |
| 209 | + // conversion end | |
| 210 | + // comments theme start. | |
| 211 | + case 'theme-six-free': | |
| 212 | + case 'theme-seven-free': | |
| 213 | + case 'theme-eight-free': | |
| 214 | + // review themes | |
| 215 | + case 'review-comment': | |
| 216 | + case 'review-comment-2': | |
| 217 | + case 'review-comment-3': | |
| 218 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 219 | + break; | |
| 220 | + // comments theme end. | |
| 221 | + | |
| 222 | + // reviews theme start. | |
| 223 | + case 'total-rated': | |
| 224 | + case 'reviewed': | |
| 225 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 226 | + break; | |
| 227 | + case 'review_saying': | |
| 228 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['review_fourth_param']} {$params['fifth_param']} {$params['sixth_param']}"; | |
| 229 | + break; | |
| 230 | + // reviews theme end. | |
| 231 | + // start download stats | |
| 232 | + case 'today-download': | |
| 233 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 234 | + break; | |
| 235 | + case '7day-download': | |
| 236 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 237 | + break; | |
| 238 | + case 'actively_using': | |
| 239 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']}"; | |
| 240 | + break; | |
| 241 | + case 'total-download': | |
| 242 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 243 | + break; | |
| 244 | + // end download stats | |
| 245 | + | |
| 246 | + case 'maps_theme': | |
| 247 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['map_fourth_param']} {$params['fourth_param']} {$params['fifth_param']}"; | |
| 248 | + break; | |
| 249 | + // PA | |
| 250 | + case 'pa-theme-one': | |
| 251 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['ga_fourth_param']} {$params['ga_fifth_param']} {$params['sixth_param']}"; | |
| 252 | + break; | |
| 253 | + case 'pa-theme-two': | |
| 254 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['ga_fourth_param']} {$params['ga_fifth_param']} {$params['sixth_param']}"; | |
| 255 | + break; | |
| 256 | + case 'pa-theme-three': | |
| 257 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['ga_fourth_param']}"; | |
| 258 | + break; | |
| 259 | + case 'stock-theme-one': | |
| 260 | + case 'stock-theme-two': | |
| 261 | + $template = "{$params['second_param']} {$params['third_param']} {$params['fourth_param']} {$params['fifth_param']}"; | |
| 262 | + break; | |
| 263 | + default: | |
| 264 | + $template = "{$params['first_param']} {$params['second_param']} {$params['third_param']} {$params['fourth_param']}"; | |
| 265 | + break; | |
| 266 | + } | |
| 267 | + return $template; | |
| 268 | + } | |
| 269 | + | |
| 270 | + function generateRandomNumber($content, $productId) { | |
| 271 | + // Use a unique cookie name per product | |
| 272 | + $cookieNamePrefix = 'randomNumber_'; | |
| 273 | + | |
| 274 | + // Use a regex to find all {{random:min-max:range:expiry}} placeholders | |
| 275 | + return preg_replace_callback('/{{random:(\d+)-(\d+)(?::(\d+))?(?::(\d+))?}}/', function ($matches) use ($cookieNamePrefix, $productId) { | |
| 276 | + $min = (int)$matches[1]; | |
| 277 | + $max = (int)$matches[2]; | |
| 278 | + $range = isset($matches[3]) ? (int)$matches[3] : 10; // Default range to 10 | |
| 279 | + $expiry = isset($matches[4]) ? (int)$matches[4] : 15; // Default expiry to 15 minutes | |
| 280 | + | |
| 281 | + // Create a unique cookie name for this product | |
| 282 | + $cookieName = $cookieNamePrefix . $productId; | |
| 283 | + | |
| 284 | + // Check if a cookie value exists | |
| 285 | + $cookieValue = isset($_COOKIE[$cookieName]) ? (int)$_COOKIE[$cookieName] : null; | |
| 286 | + | |
| 287 | + // Determine the random number | |
| 288 | + if ($cookieValue !== null) { | |
| 289 | + $randomNumber = $cookieValue; | |
| 290 | + } else { | |
| 291 | + $randomNumber = wp_rand($min, $max); | |
| 292 | + | |
| 293 | + // If cookieValue existed, ensure new number is within the allowed range | |
| 294 | + if ($cookieValue !== null) { | |
| 295 | + while (abs($randomNumber - $cookieValue) > $range) { | |
| 296 | + $randomNumber = wp_rand($min, $max); | |
| 297 | + } | |
| 298 | + } | |
| 299 | + | |
| 300 | + // Set the cookie for this product | |
| 301 | + setcookie($cookieName, (string)$randomNumber, time() + ($expiry * 60), "/"); | |
| 302 | + } | |
| 303 | + | |
| 304 | + return $randomNumber; | |
| 305 | + }, $content); | |
| 306 | + } | |
| 307 | + | |
| 308 | + | |
| 309 | +} | |