settings = Data::instance(); add_action( 'wp_enqueue_scripts', [ $this, 'frontend_script' ], 99 ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); } public static function instance() { return self::$instance == null ? self::$instance = new self() : self::$instance; } public static function remove_other_script( $pattern = '', $style = false ) { if ( ! $pattern ) { $pattern = '/^(\/wp-content\/plugins|\/wp-content\/themes)/i'; } if ( $style ) { global $wp_styles; $styles = $wp_styles->registered; foreach ( $styles as $style ) { preg_match( $pattern, $style->src, $result1 ); if ( count( array_filter( $result1 ) ) ) { wp_dequeue_style( $style->handle ); } } } global $wp_scripts; $scripts = $wp_scripts->registered; if ( is_admin() ) { foreach ( $scripts as $k => $script ) { if ( in_array( $script->handle, array( 'query-monitor', 'uip-app', 'uip-vue', 'uip-toolbar-app' ) ) ) { continue; } preg_match( '/^\/wp-/i', $script->src, $result ); if ( count( array_filter( $result ) ) ) { preg_match( $pattern, $script->src, $result1 ); if ( count( array_filter( $result1 ) ) ) { wp_dequeue_script( $script->handle ); } } else { wp_dequeue_script( $script->handle ); } } } else { foreach ( $scripts as $k => $script ) { preg_match( $pattern, $script->src, $result1 ); if ( count( array_filter( $result1 ) ) ) { wp_dequeue_script( $script->handle ); } } } } public function register_scripts( $type = 'admin' ) { $suffix = WP_DEBUG ? '' : '.min'; switch ( $type ) { case 'admin': $lib_styles = [ 'button', 'tab', 'input', 'icon', 'segment', 'image', 'modal', 'dimmer', 'transition', 'menu', 'grid', 'search', 'message', 'loader', 'label', 'select2', 'header', 'accordion', 'dropdown', 'checkbox', 'form', 'step', 'table', 'chart', 'dataTables', 'dataTables.semanticui', 'affi-icon', ]; foreach ( $lib_styles as $style ) { wp_register_style( $this->slug . $style, AffiEnv::get( 'libs_url' ) . $style . '.min.css', '', AffiEnv::get( 'version' ) ); } $styles = [ 'settings', 'notifications' ]; foreach ( $styles as $style ) { wp_register_style( $this->slug . $style, AffiEnv::get( 'css_url' ) . $style . $suffix . '.css', '', AffiEnv::get( 'version' ) ); } $lib_scripts = [ 'select2', 'transition', 'dimmer', 'accordion', 'dropdown', 'jqColorPicker', 'jquery.address', 'tab', 'modal', 'chart', 'dataTables', 'dataTables.semanticui' ]; foreach ( $lib_scripts as $script ) { wp_register_script( $this->slug . $script, AffiEnv::get( 'libs_url' ) . $script . '.min.js', [ 'jquery' ], AffiEnv::get( 'version' ) );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter } $scripts = [ 'af-settings' => [ 'jquery' ], 'af-affiliates' => [ 'jquery' ], 'af-ranks' => [ 'jquery' ], 'af-reports' => [ 'jquery' ], 'af-payout' => [ 'jquery' ], 'af-notifications' => [ 'jquery' ], 'affi_my_affiliate_account' => [ 'jquery' ], ]; foreach ( $scripts as $script => $depend ) { if ( is_string( $depend ) ) { $script = $depend; $depend = []; } wp_register_script( $this->slug . $script, AffiEnv::get( 'js_url' ) . $script . $suffix . '.js', $depend, AffiEnv::get( 'version' ) );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter } break; default: $lib_styles = [ 'chart', 'dataTables', 'dataTables.semanticui', 'affi-icon', ]; foreach ( $lib_styles as $style ) { wp_register_style( $this->slug . $style, AffiEnv::get( 'libs_url' ) . $style . '.min.css', '', AffiEnv::get( 'version' ) ); } $styles = [ 'affi_my_affiliate_account', 'frontend', 'policy' ]; foreach ( $styles as $style ) { if ( false !== strpos( $style, 'affi' ) ) { wp_register_style( $style, AffiEnv::get( 'css_url' ) . $style . $suffix . '.css', '', AffiEnv::get( 'version' ) ); } else { wp_register_style( $this->slug . $style, AffiEnv::get( 'css_url' ) . $style . $suffix . '.css', '', AffiEnv::get( 'version' ) ); } } $lib_scripts = [ 'chart', 'dataTables', 'dataTables.semanticui' ]; foreach ( $lib_scripts as $script ) { wp_register_script( $this->slug . $script, AffiEnv::get( 'libs_url' ) . $script . '.min.js', [ 'jquery' ], AffiEnv::get( 'version' ) );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter } $scripts = [ 'affi_my_affiliate_account' => [ 'jquery' ], 'frontend' => [ 'jquery' ], 'af-policy' => [ 'jquery' ], ]; foreach ( $scripts as $script => $depend ) { if ( false !== strpos( $script, 'affi' ) ) { wp_register_script( $script, AffiEnv::get( 'js_url' ) . $script . $suffix . '.js', $depend, AffiEnv::get( 'version' ) );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter } else { wp_register_script( $this->slug . $script, AffiEnv::get( 'js_url' ) . $script . $suffix . '.js', $depend, AffiEnv::get( 'version' ) );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter } } } } public function admin_enqueue_scripts() { $screen_id = get_current_screen()->id; $pages = AffiEnv::get_pages(); if ( ! in_array( $screen_id, $pages ) ) { return; } $this->register_scripts(); /*Remove all other script*/ $this->remove_other_script(); $enqueue_scripts = $enqueue_styles = []; $localize_script = $inline_handle = $css = ''; $params = [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'affi_security' ), 'currency' => get_woocommerce_currency_symbol(), 'total_text' => esc_html__( 'Total', 'affi-affiliate-marketing-for-woo' ), 'time_text' => esc_html__( 'Time', 'affi-affiliate-marketing-for-woo' ), 'isRTL' => is_rtl(), 'warning_gdpr' => esc_html__( 'Please agree with our term and policy.', 'affi-affiliate-marketing-for-woo' ), ]; switch ( $screen_id ) { case $pages['settings_page']: wp_enqueue_editor(); $enqueue_scripts = [ 'af-settings', 'select2', 'transition', 'dropdown', 'tab', 'accordion', 'jqColorPicker', 'jquery.address' ]; $enqueue_styles = [ 'settings', 'select2', 'segment', 'menu', 'tab', 'accordion', 'table', 'form', 'button', 'icon', 'checkbox', 'message', 'transition', 'dropdown', 'label', 'grid', 'input' ]; $localize_script = 'af-settings'; break; case $pages['setting_affiliates']: $enqueue_scripts = [ 'af-affiliates', 'select2', 'dimmer', 'transition', 'dropdown', 'modal' ]; $enqueue_styles = [ 'settings', 'select2', 'dimmer', 'transition', 'dropdown', 'loader', 'modal', 'form', 'button', 'icon', 'affi-icon', 'table', 'input' ]; $localize_script = 'af-affiliates'; break; case $pages['setting_rank']: $enqueue_scripts = [ 'af-ranks', 'select2', 'transition', 'dropdown', 'tab', 'accordion', 'jqColorPicker', 'jquery.address' ]; $enqueue_styles = [ 'settings', 'select2', 'segment', 'menu', 'tab', 'accordion', 'table', 'form', 'button', 'icon', 'checkbox', 'message', 'transition', 'dropdown', 'label', 'grid', 'input' ]; $localize_script = 'af-ranks'; wp_enqueue_media(); break; case $pages['setting_report']: $enqueue_scripts = [ 'af-reports', 'chart', 'select2', 'transition', 'tab', 'accordion', 'jquery.address' ]; $enqueue_styles = [ 'settings', 'chart', 'segment', 'menu', 'tab', 'icon', ]; $params['text_total_aff'] = esc_html__( 'Total affiliates', 'affi-affiliate-marketing-for-woo' ); $params['text_new_aff'] = esc_html__( 'New Affiliates', 'affi-affiliate-marketing-for-woo' ); $params['text_top_earn_aff'] = esc_html__( 'Top Earning Affiliate', 'affi-affiliate-marketing-for-woo' ); $params['text_total_ref'] = esc_html__( 'Total Referrals', 'affi-affiliate-marketing-for-woo' ); $params['text_total_aff_val'] = esc_html__( 'Total Affiliate Value', 'affi-affiliate-marketing-for-woo' ); $params['text_total_aff_earn'] = esc_html__( 'Total Affiliate Earning', 'affi-affiliate-marketing-for-woo' ); $params['text_high_aff_user'] = esc_html__( 'Highest Affiliate User', 'affi-affiliate-marketing-for-woo' ); $params['text_total_visit'] = esc_html__( 'Total Visits', 'affi-affiliate-marketing-for-woo' ); $params['text_most_visit_url'] = esc_html__( 'Most Visit URL', 'affi-affiliate-marketing-for-woo' ); $params['text_payout_paid'] = esc_html__( 'Payout Paid', 'affi-affiliate-marketing-for-woo' ); $params['text_payout_count'] = esc_html__( 'Payouts Count', 'affi-affiliate-marketing-for-woo' ); $params['text_new_aff'] = esc_html__( 'New affiliates', 'affi-affiliate-marketing-for-woo' ); $params['text_total_click'] = esc_html__( 'Total Clicks', 'affi-affiliate-marketing-for-woo' ); $params['text_'] = esc_html__( 'Affiliate', 'affi-affiliate-marketing-for-woo' ); $params['text_'] = esc_html__( 'Affiliate', 'affi-affiliate-marketing-for-woo' ); $localize_script = 'af-reports'; break; case $pages['setting_request']: $enqueue_scripts = [ 'af-payout', 'select2', 'transition', 'tab', 'dropdown', 'accordion', 'jquery.address' ]; $enqueue_styles = [ // 'select2', // 'dropdown', // 'settings', // 'segment', // 'menu', // 'form', // 'button', // 'tab', // 'icon', 'settings', 'select2', 'segment', 'menu', 'tab', 'accordion', 'table', 'form', 'button', 'icon', 'checkbox', 'message', 'transition', 'dropdown', 'label', 'grid', 'input' ]; $localize_script = 'af-payout'; break; case $pages['setting_notify']: $enqueue_scripts = [ 'af-notifications', 'select2', 'dimmer', 'transition', 'dropdown', 'modal' ]; $enqueue_styles = [ 'settings', 'notifications', 'select2', 'dimmer', 'transition', 'dropdown', 'loader', 'modal', 'form', 'button', 'icon', 'affi-icon', 'table', 'input', 'checkbox', ]; $localize_script = 'af-notifications'; break; default: break; } foreach ( $enqueue_scripts as $script ) { wp_enqueue_script( $this->slug . $script ); } foreach ( $enqueue_styles as $style ) { wp_enqueue_style( $this->slug . $style ); } if ( $localize_script ) { wp_localize_script( $this->slug . $localize_script, 'affiParams', $params ); } if ( $inline_handle ) { wp_add_inline_style( $this->slug . $inline_handle, $css ); } } public function frontend_script() { /*Remove all other script*/ $this->remove_other_script(); $suffix = defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min'; // wp_register_style( $this->slug . 'problem_form', AffiEnv::get( 'css_url' ) . 'problem-form' . $suffix . '.css', '', AffiEnv::get( 'version' ) ); // wp_register_style( $this->slug . 'my_affiliate_account', AffiEnv::get( 'css_url' ) . 'affi_my_affiliate_account' . $suffix . '.css', '', AffiEnv::get( 'version' ) ); // wp_register_style( $this->slug . 'my-account-icon', AffiEnv::get( 'libs_url' ) . 'affi-icon.css', '', AffiEnv::get( 'version' ) ); $this->register_scripts( 'frontend' ); $enqueue_scripts = $enqueue_styles = []; $localize = [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'affi_security' ), 'isRTL' => is_rtl(), 'currency' => get_woocommerce_currency_symbol(), 'total_text' => esc_html__( 'Total', 'affi-affiliate-marketing-for-woo' ), 'time_text' => esc_html__( 'Time', 'affi-affiliate-marketing-for-woo' ), 'visit' => esc_html__( 'Visit', 'affi-affiliate-marketing-for-woo' ), 'count' => esc_html__( 'Order count', 'affi-affiliate-marketing-for-woo' ), 'total' => esc_html__( 'Order total', 'affi-affiliate-marketing-for-woo' ), 'o_balance' => esc_html__( 'Outstanding balance', 'affi-affiliate-marketing-for-woo' ), 'balance' => esc_html__( 'Balance', 'affi-affiliate-marketing-for-woo' ), ]; // $enqueue_scripts = [ 'dataTables', 'chart', 'affi_my_affiliate_account' ]; $enqueue_styles = [ 'dataTables', /*'chart',*/ 'affi-icon', 'my_affiliate_account' ]; foreach ( $enqueue_scripts as $script ) { wp_enqueue_script( $this->slug . $script ); } // foreach ( $enqueue_styles as $style ) { // wp_enqueue_style( $this->slug . $style ); // } wp_enqueue_style( 'affi-frontend-style', AffiEnv::get( 'css_url' ) . 'frontend' . $suffix . '.css', '', AffiEnv::get( 'version' ) ); $css = ''; $css = str_replace( array( "\r", "\n", '\r', '\n' ), ' ', $css ); $css .= $this->settings->get_param( 'custom_css' ); wp_add_inline_style( 'affi-frontend-style', $css ); wp_enqueue_script( $this->slug . 'frontend', AffiEnv::get( 'js_url' ) . 'frontend' . $suffix . '.js', array( 'jquery' ), AffiEnv::get( 'version' ),true );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter wp_enqueue_style( $this->slug . 'my-account-icon', AffiEnv::get( 'libs_url' ) . 'affi-icon.min.css', '', AffiEnv::get( 'version' ) ); // wp_localize_script( $this->slug . 'frontend', 'affiParams', $localize ); } }