plugin_assets . 'css/' . self::wpaf_version_correct( 'dir' ) . 'style' . self::wpaf_version_correct( 'suffix' ) . '.css', false, WPAF_Plugin::PLUGIN_VERSION ); } $coupon_type_color = self::sanitize_css_color( get_option( 'wpaf_coupon-type-bg-color' ) ); $deals_type_color = self::sanitize_css_color( get_option( 'wpaf_deal-type-bg-color' ) ); $coupon_border_color = self::sanitize_css_color( get_option( 'wpaf_dt-border-color' ) ); $coupon_border_radius = absint( get_option( 'wpaf_coupon-border-radius' ) ); $hide_featured_image = get_option( 'wpaf_hide-archive-thumbnail' ); if ( $amp ) { $output_for_amp = ''; } if ( $hide_featured_image === 'on' ) { $custom_style = " #wpaf_coupon_ul li.wpaf_coupon_li { min-height: initial; } .wpaf_coupon_li_inner { height: auto; } .wpaf_coupon_li_content { height: auto; padding: 10px; } "; $custom_style = preg_replace( '/\s+/', ' ', $custom_style ); if ( ! $amp ) { wp_add_inline_style( 'wpaf-style', wp_strip_all_tags( $custom_style ) ); } else { $output_for_amp .= $custom_style; } } $inline_style = " .coupon-type { background-color: {$coupon_type_color}; } .deal-type { background-color: {$deals_type_color}; } .wpaf-coupon { border-color: {$coupon_border_color}; border-radius: {$coupon_border_radius}px; } "; $inline_style = preg_replace( '/\s+/', ' ', $inline_style ); if ( ! $amp ) { wp_add_inline_style( 'wpaf-style', wp_strip_all_tags( $inline_style ) ); } else { $output_for_amp .= $inline_style; } $hide_coupon_button_color = self::sanitize_css_color( get_option( 'wpaf_hidden-coupon-button-color' ) ); $copy_button_bg_color = self::sanitize_css_color( get_option( 'wpaf_copy-button-bg-color' ) ); if ( ! $amp ) $copy_button_bg_color = $copy_button_bg_color . ' !important'; $coupon_popup_bg_color = self::sanitize_css_color( get_option( 'wpaf_coupon-popup-bg-color' ) ); $pagination_color = self::sanitize_css_color( get_option( 'wpaf_pagination-color' ) ); $wpaf_inline_style = " .wpaf-coupon-button-type .coupon-code-wpaf .get-code-wpaf{ background-color:{$hide_coupon_button_color}; } .wpaf-coupon-button-type .coupon-code-wpaf .get-code-wpaf:after{ border-left-color:{$hide_coupon_button_color}; } span.wpaf_coupon_top_copy_span{ background-color: {$copy_button_bg_color}; } .wpaf_coupon_popup_copy_code_wr { background-color: {$coupon_popup_bg_color}; border-color: {$copy_button_bg_color}; } .wpaf_popup-go-link { background-color: {$copy_button_bg_color}; } .wpaf_popup-go-link:hover { background-color: {$copy_button_bg_color}; } #wpaf_coupon_pagination_wr a, #wpaf_coupon_pagination_wr span { background-color: {$pagination_color}; } "; $wpaf_inline_style = preg_replace( '/\s+/', ' ', $wpaf_inline_style ); //add changes to stylesheet if ( ! $amp ) { wp_add_inline_style( 'wpaf-style', wp_strip_all_tags( $wpaf_inline_style ) ); } else { $output_for_amp .= $wpaf_inline_style; } if ( $amp ) { $coupon_theme_color = ""; if ( $coupon_template === 'Template Five' ) { } elseif ( $coupon_template === 'Template Six' ) { } elseif ( $coupon_template === 'Template Eight' ) { $wpaf_template_eight_theme = self::sanitize_css_color( get_post_meta( $coupon_id, 'coupon_details_template-theme-color', true ) ); $coupon_theme_color = " a.wpaf-new-coupon-code:hover { border-color: {$wpaf_template_eight_theme}; } "; } if ( $coupon_theme_color ) { $coupon_theme_color = preg_replace( '/\s+/', ' ', $coupon_theme_color ); $output_for_amp .= $coupon_theme_color; } } if ( $amp ) { return $output_for_amp; } } /** * Get URL of a frontend CSS file * * @since 3.0.3 */ public static function wpaf_frontend_css_url_get() { return WPAF_Plugin::instance()->plugin_assets . 'css/' . self::wpaf_version_correct( 'dir' ) . 'style' . self::wpaf_version_correct( 'suffix' ) . '.css'; } /** * Scripts for the coupon shortcode. * * @since 1.0 */ public static function wpaf_scripts() { wp_register_script( 'wpaf-main-js', WPAF_Plugin::instance()->plugin_assets . 'js/main.js', array( 'jquery' ), WPAF_Plugin::PLUGIN_VERSION, self::wpaf_script_args( false ) ); wp_register_script( 'wpaf-clipboardjs', includes_url( 'js/clipboard.min.js' ), array(), WPAF_Plugin::PLUGIN_VERSION, self::wpaf_script_args( false ) ); if ( self::should_enqueue_frontend_assets() ) { wp_enqueue_script( 'wpaf-main-js' ); wp_enqueue_script( 'wpaf-clipboardjs' ); } //To make sure that "ajax_url" is defined in main.js wp_localize_script( 'wpaf-main-js', 'wpaf_object', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'security' => wp_create_nonce( 'wpaf-security-nonce' ), ] ); $word_count = get_option( 'wpaf_words-count' ); if ( empty( $word_count ) ) { $word_count = 30; } $copy_button_text = get_option( 'wpaf_copy-button-text' ); $after_copy_text = get_option( 'wpaf_after-copy-text' ); $vote_success = get_option( 'wpaf_coupon-vote-success' ); $vote_failed = get_option( 'wpaf_coupon-vote-fail' ); $vote_already = get_option( 'wpaf_coupon-vote-already' ); if ( ! empty( $vote_success ) ) { $vote_success_message = $vote_success; } else { $vote_success_message = __( 'You have voted successfully!', 'affcoupon' ); } if ( ! empty( $vote_failed ) ) { $vote_failed_message = $vote_failed; } else { $vote_failed_message = __( 'Voting failed!', 'affcoupon' ); } if ( ! empty( $vote_already ) ) { $vote_already_message = $vote_already; } else { $vote_already_message = __( 'You have voted already!', 'affcoupon' ); } if ( ! empty( $copy_button_text ) ) { $button_text = $copy_button_text; } else { $button_text = __( 'Copy', 'affcoupon' ); } if ( ! empty( $after_copy_text ) ) { $after_copy = $after_copy_text; } else { $after_copy = __( 'Copied', 'affcoupon' ); } wp_localize_script( 'wpaf-main-js', 'wpaf_main_js', array( 'minutes' => __( 'minutes', 'affcoupon' ), 'seconds' => __( 'seconds', 'affcoupon' ), 'hours' => __( 'hours', 'affcoupon' ), 'day' => __( 'day', 'affcoupon' ), 'week' => __( 'week', 'affcoupon' ), 'expired_text' => __( 'This offer has expired!', 'affcoupon' ), 'word_count' => $word_count, 'button_text' => $button_text, 'after_copy' => $after_copy, 'vote_success' => $vote_success_message, 'vote_fail' => $vote_failed_message, 'vote_already' => $vote_already_message ) ); // jquery.countdown is a jQuery plugin/addon, not jQuery itself; depend on WordPress core jQuery. wp_register_script( 'wpaf-countdown-js', WPAF_Plugin::instance()->plugin_assets . 'js/jquery.countdown.min.js', array( 'jquery' ), WPAF_Plugin::PLUGIN_VERSION, self::wpaf_script_args( false ) ); if ( self::should_enqueue_frontend_assets() ) { wp_enqueue_script( 'wpaf-countdown-js' ); } } /* * Dashicons Connection * * @since @since 1.0 */ public static function wpaf_load_dashicons_front_end() { if ( is_admin() || self::should_enqueue_frontend_assets() ) { wp_enqueue_style( 'dashicons' ); } } private static function should_enqueue_frontend_assets() { if ( is_admin() ) { return false; } if ( wp_doing_ajax() || is_post_type_archive( 'wpaf_coupons' ) || is_tax( array( WPAF_Plugin::CUSTOM_TAXONOMY, WPAF_Plugin::VENDOR_TAXONOMY ) ) ) { return true; } if ( is_active_widget( false, false, 'wpaf_coupons_deals_widget', true ) ) { return true; } if ( is_singular() ) { $post = get_post(); if ( $post && ( has_shortcode( $post->post_content, 'affcoupon' ) || has_shortcode( $post->post_content, 'aff-coupon' ) || has_shortcode( $post->post_content, 'affcoupon_btn' ) || has_shortcode( $post->post_content, 'affcoupon_coupons' ) || has_shortcode( $post->post_content, 'affcoupon_loop' ) ) ) { return true; } } return false; } private static function sanitize_css_color( $value ) { $value = trim( (string) $value ); if ( '' === $value ) { return ''; } $hex = sanitize_hex_color( $value ); if ( $hex ) { return $hex; } if ( preg_match( '/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)$/', $value ) ) { return $value; } return ''; } public static function wpaf_sanitize_css_color( $value ) { return self::sanitize_css_color( $value ); } public static function wpaf_script_args( $in_footer = false ) { global $wp_version; if ( version_compare( $wp_version, '6.3', '>=' ) ) { return array( 'in_footer' => (bool) $in_footer, ); } return (bool) $in_footer; } /** * Stylesheets for admin area. * * @since 1.0 */ public static function wpaf_admin_stylesheets( $hook_suffix ) { /** * Loading script only where necessary. * * @since 1.0 */ $custom_post_type = 'wpaf_coupons'; if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) { $screen = get_current_screen(); if ( is_object( $screen ) && $custom_post_type == $screen->post_type ) { wp_enqueue_style( 'wpaf-jquery-ui-style', WPAF_Plugin::instance()->plugin_assets . 'admin/css/' . self::wpaf_version_correct( 'dir' ) . 'jquery-ui' . self::wpaf_version_correct( 'suffix' ) . '.css', false, WPAF_Plugin::PLUGIN_VERSION ); } } if ( in_array( $hook_suffix, array( 'edit.php', 'post.php', 'post-new.php' ) ) ) { $screen = get_current_screen(); if ( is_object( $screen ) && $custom_post_type == $screen->post_type ) { wp_enqueue_style( 'wpaf-admin-style', WPAF_Plugin::instance()->plugin_assets . 'admin/css/' . self::wpaf_version_correct( 'dir' ) . 'admin' . self::wpaf_version_correct( 'suffix' ) . '.css', false, WPAF_Plugin::PLUGIN_VERSION ); wp_enqueue_style( 'wpaf-admin-style', WPAF_Plugin::instance()->plugin_assets . 'admin/css/select2.min.css', false, WPAF_Plugin::PLUGIN_VERSION ); } } if ( in_array( $hook_suffix, array( 'edit.php', 'post.php', 'post-new.php' ) ) ) { wp_enqueue_style( 'wpaf-admin-style', WPAF_Plugin::instance()->plugin_assets . 'admin/css/' . self::wpaf_version_correct( 'dir' ) . 'admin' . self::wpaf_version_correct( 'suffix' ) . '.css', false, WPAF_Plugin::PLUGIN_VERSION ); } $coupon_type_color = self::sanitize_css_color( get_option( 'wpaf_coupon-type-bg-color' ) ); $coupon_border_color = self::sanitize_css_color( get_option( 'wpaf_dt-border-color' ) ); $coupon_border_radius = absint( get_option( 'wpaf_coupon-border-radius' ) ); $inline_style = " .coupon-type { background-color: {$coupon_type_color}; } .deal-type { background-color: {$coupon_type_color}; } .wpaf-coupon { border-color: {$coupon_border_color}; border-radius: {$coupon_border_radius}px; } "; $inline_style = preg_replace( '/\s+/', '', $inline_style ); wp_add_inline_style( 'wpaf-admin-style', wp_strip_all_tags( $inline_style ) ); } /** * Scripts for admin area. * * @since 1.0 */ public static function wpaf_admin_scripts( $hook_suffix ) { /** * Loading script only where necessary. * * @since 1.0 */ $custom_post_type = 'wpaf_coupons'; $should_enqueue_admin_js = false; if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) { $should_enqueue_admin_js = true; $screen = get_current_screen(); if ( is_object( $screen ) && $custom_post_type == $screen->post_type ) { wp_enqueue_script( 'jquery-ui-datepicker' ); wp_enqueue_script( 'wpaf-jquery-ui-timepicker', WPAF_Plugin::instance()->plugin_assets . 'admin/js/jquery-ui-timepicker.js', array( 'jquery' ), WPAF_Plugin::PLUGIN_VERSION, self::wpaf_script_args( false ) ); // jquery.countdown is a jQuery plugin/addon, not jQuery itself; depend on WordPress core jQuery. wp_enqueue_script( 'wpaf-countdown-js', WPAF_Plugin::instance()->plugin_assets . 'js/jquery.countdown.min.js', array( 'jquery' ), WPAF_Plugin::PLUGIN_VERSION, self::wpaf_script_args( false ) ); //To add custom javascript code to tinymce editor at initiation add_filter( 'tiny_mce_before_init', array( __CLASS__, 'wpaf_tiny_mce' ) ); // color Picker wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); } // Identify the import page by its admin hook suffix (passed in by // admin_enqueue_scripts) instead of reading $_GET, so no request input // is inspected. The page is registered under edit.php?post_type=wpaf_coupons. } elseif ( 'wpaf_coupons_page_wpaf_coupon_import' === $hook_suffix ) { $should_enqueue_admin_js = true; wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); } if ( 'edit.php' === $hook_suffix ) { $screen = get_current_screen(); if ( is_object( $screen ) && $custom_post_type === $screen->post_type ) { $should_enqueue_admin_js = true; } } // Identify the settings page by its admin hook suffix instead of $_GET. if ( 'wpaf_coupons_page_wpaf_coupon_settings' === $hook_suffix ) { $should_enqueue_admin_js = true; wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); } if ( ! $should_enqueue_admin_js ) { return; } wp_enqueue_script( 'wpaf-admin-js', WPAF_Plugin::instance()->plugin_assets . 'admin/js/admin.js', array( 'jquery', 'jquery-ui-datepicker', 'wp-color-picker' ), WPAF_Plugin::PLUGIN_VERSION, self::wpaf_script_args( false ) ); $ajax_data = array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wpaf-script-nonce' ), ); wp_localize_script( 'wpaf-admin-js', 'wpaf_ajax_script_import', $ajax_data ); } /** * to add custom javascript code tinymce Editor at initiation * * @since 1.0 * @param array $initArray * @return array */ public static function wpaf_tiny_mce( $initArray ) { /* * change description dynamically in live preview * * VERY IMPORTANT: don't change the spaces in this code !!!! * @since 1.0 */ $initArray['toolbar1'] = "bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link,unlink"; $initArray['toolbar2'] = ''; $initArray['setup'] = "[function(ed) {\n" . " ed.on('KeyUp', function (e) {\n" . " var description = tinyMCE.activeEditor.getContent();\n" . " $('.wpaf-coupon-description').html(description);\n" . " });\n" . " ed.on('Change', function (e) {\n" . " var description = tinyMCE.activeEditor.getContent();\n" . " $('.wpaf-coupon-description').html(description);\n" . " }); \n" . " \n" . "}][0]"; return $initArray; } /** * This function checks debug is switch on or switch off * and then return the necessary directory or suffix if it needs ( for minimized version ) * * @since @since 1.0 * @param string $way_correct * @return string */ public static function wpaf_version_correct( $way_correct ) { $correct = array( 'dir' => 'dist/', 'suffix' => '.min', ); if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { $correct['dir'] = ''; $correct['suffix'] = ''; } return array_key_exists( $way_correct, $correct ) ? $correct[$way_correct] : ''; } /** * Print an inline JavaScript block at the current output position. * * Delegates to WordPress core's `wp_print_inline_script_tag()` (available * since WP 5.7, which matches the plugin's "Requires at least" header), so * the `