isBundledFeatureEnabled() && $this->isUserAllowed() ) { // extract form fields $fields = WPAF_Meta_Boxes_Fields_Pro::getFields(); $preview_metabox = new WPAF_Preview_Metabox(); // extract coupon preview inline html elements and scripts ob_start(); $preview_metabox->generate_fields( new stdClass() ); $coupon_preview = ob_get_contents(); ob_end_clean(); // removing any script tag from the previews $coupon_preview = preg_replace( '#(.*?)#is', '', $coupon_preview ); $template_names = static::getAvailableTemplateNames( $fields ); // splitting the html file into our templates $split_html = $this->splitHtml( $coupon_preview, '', '', $template_names ); // including the necessary js files for front end display $js_pure_path = '/js/formShortcode.bundle.js'; $js_asset_uri_path = WPAF_Plugin::instance()->plugin_assets . $js_pure_path; $js_asset_dir_path = WPAF_Plugin::instance()->plugin_dir_path . '/assets' . $js_pure_path; $js_version = filemtime( $js_asset_dir_path ); $css_pure_path = '/admin/css/dist/admin.min.css'; $css_asset_uri_path = WPAF_Plugin::instance()->plugin_assets . $css_pure_path; $css_asset_dir_path = WPAF_Plugin::instance()->plugin_dir_path . '/assets' . $css_pure_path; $css_version = filemtime( $css_asset_dir_path ); // including necesssary translation strings $extras = new stdClass(); $extras->strings = []; $extras->strings['expire_text'] = $this->_c()->get_option( 'wpaf_expire-text', 'Expires on: ' ); $extras->strings['expired_text'] = $this->_c()->get_option( 'wpaf_expired-text', 'Expired on: ' ); // custom taxonomy terms $extras->terms = static::getCouponTerms(); // Literal __() calls so the translation parser can extract each string. $batch_translations = array( 'offer_expired_text' => __( 'This offer has expired!', 'affcoupon' ), 'second' => __( 'second', 'affcoupon' ), 'seconds' => __( 'seconds', 'affcoupon' ), 'minute' => __( 'minute', 'affcoupon' ), 'minutes' => __( 'minutes', 'affcoupon' ), 'hour' => __( 'hour', 'affcoupon' ), 'hours' => __( 'hours', 'affcoupon' ), 'day' => __( 'day', 'affcoupon' ), 'days' => __( 'days', 'affcoupon' ), 'week' => __( 'week', 'affcoupon' ), 'weeks' => __( 'weeks', 'affcoupon' ), 'month' => __( 'month', 'affcoupon' ), 'months' => __( 'months', 'affcoupon' ), 'year' => __( 'year', 'affcoupon' ), 'years' => __( 'years', 'affcoupon' ), 'submit' => __( 'Submit', 'affcoupon' ), 'select_an_image' => __( 'Select an image', 'affcoupon' ), 'Add' => __( 'Add', 'affcoupon' ), 'set_featured_image' => __( 'Set a featured image', 'affcoupon' ), 'featured_image' => __( 'featured image', 'affcoupon' ), 'use_image' => __( 'use this image', 'affcoupon' ), 'enter_title' => __( 'enter your coupon title', 'affcoupon' ), 'coupon_title' => __( 'coupon title', 'affcoupon' ), 'coupon_type' => __( 'coupon type', 'affcoupon' ), 'your_coupons' => __( 'your coupons', 'affcoupon' ), 'add_new' => __( 'add new', 'affcoupon' ), 'edit' => __( 'edit your coupon', 'affcoupon' ), 'back_to_coupons' => __( 'back to coupons', 'affcoupon' ), 'add_a_coupon' => __( 'add a coupon', 'affcoupon' ), 'update' => __( 'update', 'affcoupon' ), 'shortcode_copy' => __( 'shortcode copied', 'affcoupon' ), 'expired' => __( 'expired', 'affcoupon' ), 'no_expire' => __( "doesn't expire", 'affcoupon' ), 'expires_on' => __( 'expires on', 'affcoupon' ), 'options' => __( 'options', 'affcoupon' ), 'basic_options' => __( 'basic options', 'affcoupon' ), 'advanced_options' => __( 'advanced options', 'affcoupon' ), 'search' => __( 'search', 'affcoupon' ), 'nothing' => __( 'nothing found', 'affcoupon' ), ); $extras->strings = array_merge( $extras->strings, $batch_translations ); $extras->options = []; $protocol = wp_get_server_protocol(); $extras->options['ajax_url'] = admin_url( 'admin-ajax.php', $protocol ); $extras->options['form_action'] = $this->name; $extras->options['coupons_action'] = $this->name . '_coupons'; $extras->options['nonce'] = $this->_c()->wp_create_nonce( 'wpaf_shortcode_form' ); $extras->options['thrash_enable'] = get_option( 'wpaf_form-shortcode-enable-thrash', '' ); $extras->options['shortcode'] = '[affcoupon id=:id]'; $extras->options['split_form'] = get_option( 'wpaf_form-shortcode-split-form', 'split' ); $extras->options['new_terms'] = get_option( 'wpaf_form-shortcode-enable-new-terms', '' ); $extras->options['default_featured_url'] = WPAF_Plugin::instance()->plugin_assets . 'img/coupon-200x200.png'; $extras->options['default_template'] = get_option( 'wpaf_form-shortcode-default-template', 'all' ); // enqueue scripts/styles step $this->_c()->add_action( 'wp_enqueue_scripts', function () use ( $css_version, $css_asset_uri_path, $extras, $js_asset_uri_path, $js_version, $fields, $split_html ) { //only enqueue necessary files if current post have the short-code if ( $this->haveShortcode() ) { $this->_c()->wp_enqueue_media(); $this->_c()->wp_enqueue_script( 'form_shortcode_script', $js_asset_uri_path, array( 'wp-tinymce' ), $js_version, WPAF_Assets::wpaf_script_args( true ) ); $this->_c()->wp_enqueue_style( 'form_shortcode_style', $css_asset_uri_path, array(), $css_version ); // send fields as a localized script to front end $this->_c()->wp_localize_script( 'form_shortcode_script', 'wpaf_fs_fields', $fields ); $this->_c()->wp_localize_script( 'form_shortcode_script', 'couponPreview', $split_html ); $this->_c()->wp_localize_script( 'form_shortcode_script', 'wpaf_fs_extras', (array) $extras ); } } ); // add ajax to WordPress hook $ajax_hook_nopriv = new WPAF_Formshortcode_Ajax_Pro( $this->name, false ); $ajax_hook_nopriv->add(); $ajax_hook_priv = new WPAF_Formshortcode_Ajax_Pro( $this->name, true ); $ajax_hook_priv->add(); $ajax_hook_coupons_nopriv = new WPAF_Formshortcode_Coupons_Ajax_Pro( $this->name . '_coupons', false ); $ajax_hook_coupons_nopriv->add(); $ajax_hook_coupons_priv = new WPAF_Formshortcode_Coupons_Ajax_Pro( $this->name . '_coupons', true ); $ajax_hook_coupons_priv->add(); } parent::add(); } /** * @param $data string raw data to be searched for * @param $split_clause_start string starting split clause * @param $split_clause_end string ending split clause * @param $split_array array array of clauses that will be replaced in ':split' variables in start/end parameters * * @return array array of found matches */ public function splitHtml( $data, $split_clause_start, $split_clause_end, $split_array ) { $result = []; foreach ( $split_array as $split ) { $temp = []; $start = str_replace( ':split', $split, $split_clause_start ); $end = str_replace( ':split', $split, $split_clause_end ); preg_match( "/$start(.+)$end/s", $data, $temp ); $result[ $split ] = $temp[1]; } return $result; } public function logic( $attrs ) { $enabled_html = "
"; $disabled_html = " "; // bundled feature check return $this->isBundledFeatureEnabled() ? $enabled_html : $disabled_html; } /** * retrieve defined terms for custom coupon type taxonomies * @return array terms array */ public static function getCouponTerms() { $terms_filters = [ 'count', 'name', 'parent', 'term_id' ]; $coupon_categories = get_terms( [ 'taxonomy' => [ WPAF_Plugin::CUSTOM_TAXONOMY ], 'hide_empty' => false, 'orderby' => 'term_id', 'order' => 'ASC', ] ); $vendor_categories = get_terms( [ 'taxonomy' => [ WPAF_Plugin::VENDOR_TAXONOMY ], 'hide_empty' => false, 'orderby' => 'term_id', 'order' => 'ASC', ] ); $coupon_categories = WPAF_Form_Shortcode_Pro::filter_keys( $coupon_categories, $terms_filters ); $vendor_categories = WPAF_Form_Shortcode_Pro::filter_keys( $vendor_categories, $terms_filters ); return [ WPAF_Plugin::CUSTOM_TAXONOMY => $coupon_categories, WPAF_Plugin::VENDOR_TAXONOMY => $vendor_categories ]; } /** * filter the keys of an array * * @param $array object array * @param $filters array filter array * * @return array filtered array */ public static function filter_keys( $array, $filters ) { $temp_array = []; foreach ( $array as $term ) { $filtered_terms = array_filter( (array) $term, function ( $k ) use ( $filters ) { return in_array( $k, $filters ); }, ARRAY_FILTER_USE_KEY ); $temp_array[] = $filtered_terms; } return $temp_array; } /** * Whether the front-end Vue coupon-submission form is available. * * The form is rendered by a compiled Vue bundle (assets/js/formShortcode.bundle.js) * whose build source is not distributed in this package, so the feature is not * shipped here. Returning false disables the form end to end: no bundle/style is * enqueued, its AJAX endpoints are not registered, and the [wpaf_form] shortcode * renders nothing. * * @return bool */ private function isBundledFeatureEnabled() { return false; } /** * check if the current user is allowed to submit coupons from frontend * @return bool result */ private function isUserAllowed() { $current_user = $this->_c()->wp_get_current_user(); $current_role = isset( $current_user->roles[0] ) ? $current_user->roles[0] : ''; $allowed_roles = $this->_c()->get_option( 'wpaf_form-shortcode-allowed-roles', [ 'administrator' ] ); if ( $allowed_roles === '' ) { $allowed_roles = [ 'administrator' ]; } return in_array( $current_role, $allowed_roles ); } /** * get available template names from current meta box fields * * @param array $meta_array array of meta fields to be searched * * @return array template names array */ public static function getAvailableTemplateNames( $meta_array ) { $template_meta_field = []; foreach ( $meta_array as $m ) { if ( $m['id'] === 'coupon-template' ) { $template_meta_field = $m; } } return $template_meta_field['options']; } }