hooks(); } } /** * Hooks. * * @since 1.5.0 */ public function hooks() { \add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); \add_action( 'wpforms_builder_init', array( $this, 'start_challenge' ) ); \add_action( 'admin_footer', array( $this, 'challenge_html' ) ); \add_action( 'wpforms_welcome_intro_after', array( $this, 'welcome_html' ) ); \add_action( 'wp_ajax_wpforms_challenge_embed_page_url', array( $this, 'get_embed_page_url_ajax' ) ); \add_action( 'wp_ajax_wpforms_challenge_save_option', array( $this, 'save_challenge_option_ajax' ) ); \add_action( 'wp_ajax_wpforms_challenge_send_contact_form', array( $this, 'send_contact_form_ajax' ) ); } /** * Check if the current page is related to Challenge. * * @since 1.5.0 */ public function is_challenge_page() { return \wpforms_is_admin_page() || $this->is_builder_page() || $this->is_form_embed_page(); } /** * Check if the current page is a forms builder page related to Challenge. * * @since 1.5.0 */ public function is_builder_page() { if ( ! \wpforms_is_admin_page( 'builder' ) ) { return false; } if ( ! $this->challenge_active() ) { return false; } $step = \absint( $this->get_challenge_option( 'step' ) ); $form_id = \absint( $this->get_challenge_option( 'form_id' ) ); if ( $form_id && $step < 2 ) { return false; } $current_form_id = isset( $_GET['form_id'] ) ? \absint( $_GET['form_id'] ) : 0; $is_new_form = isset( $_GET['newform'] ) ? \absint( $_GET['newform'] ) : 0; if ( $is_new_form && 2 !== $step ) { return false; } if ( ! $is_new_form && $form_id !== $current_form_id && $step >= 2 ) { return false; } return true; } /** * Check if the current page is a form embed page edit related to Challenge. * * @since 1.5.0 */ public function is_form_embed_page() { if ( ! \is_admin() || ! \is_user_logged_in() ) { return false; } $screen = \get_current_screen(); if ( ! isset( $screen->id ) || 'page' !== $screen->id ) { return false; } if ( ! $this->challenge_active() ) { return false; } $step = $this->get_challenge_option( 'step' ); if ( ! \in_array( $step, array( 4, 5 ), true ) ) { return false; } $embed_page = $this->get_challenge_option( 'embed_page' ); if ( isset( $screen->action ) && 'add' === $screen->action && 0 === $embed_page ) { return true; } if ( isset( $_GET['post'] ) && $embed_page === \absint( $_GET['post'] ) ) { return true; } return false; } /** * Load scripts and styles. * * @since 1.5.0 */ public function enqueue_scripts() { if ( $this->challenge_finished() ) { return; } $min = \wpforms_get_min_suffix(); if ( $this->is_challenge_page() ) { \wp_enqueue_style( 'wpforms-challenge', \WPFORMS_PLUGIN_URL . "assets/css/challenge{$min}.css", array(), \WPFORMS_VERSION ); \wp_enqueue_script( 'wpforms-challenge-admin', \WPFORMS_PLUGIN_URL . "assets/js/components/admin/challenge/challenge-admin{$min}.js", array( 'jquery' ), \WPFORMS_VERSION, true ); \wp_localize_script( 'wpforms-challenge-admin', 'wpforms_challenge_admin', array( 'nonce' => \wp_create_nonce( 'wpforms_challenge_ajax_nonce' ), 'minutes_left' => \absint( $this->minutes ), ) ); } if ( $this->is_builder_page() || $this->is_form_embed_page() ) { \wp_enqueue_style( 'tooltipster', \WPFORMS_PLUGIN_URL . 'assets/css/tooltipster.css', null, '4.2.6' ); \wp_enqueue_script( 'tooltipster', \WPFORMS_PLUGIN_URL . 'assets/js/jquery.tooltipster.min.js', array( 'jquery' ), '4.2.6', true ); \wp_enqueue_script( 'wpforms-challenge-core', \WPFORMS_PLUGIN_URL . "assets/js/components/admin/challenge/challenge-core{$min}.js", array( 'jquery', 'tooltipster', 'wpforms-challenge-admin' ), \WPFORMS_VERSION, true ); } if ( $this->is_builder_page() ) { \wp_enqueue_script( 'wpforms-challenge-builder', \WPFORMS_PLUGIN_URL . "assets/js/components/admin/challenge/challenge-builder{$min}.js", array( 'jquery', 'tooltipster', 'wpforms-challenge-core' ), \WPFORMS_VERSION, true ); } if ( $this->is_form_embed_page() ) { \wp_enqueue_style( 'wpforms-font-awesome', \WPFORMS_PLUGIN_URL . 'assets/css/font-awesome.min.css', null, '4.7.0' ); \wp_enqueue_script( 'wpforms-challenge-embed', \WPFORMS_PLUGIN_URL . "assets/js/components/admin/challenge/challenge-embed{$min}.js", array( 'jquery', 'tooltipster', 'wpforms-challenge-core' ), \WPFORMS_VERSION, true ); } } /** * Get 'wpforms_challenge' option schema. * * @since 1.5.0 */ public function get_challenge_option_schema() { return array( 'status' => '', 'step' => 0, 'user_id' => \get_current_user_id(), 'form_id' => 0, 'embed_page' => 0, 'started_date_gmt' => '', 'finished_date_gmt' => '', 'seconds_spent' => 0, 'seconds_left' => 0, 'feedback_sent' => false, 'feedback_contact_me' => false, ); } /** * Get Challenge parameter(s) from Challenge option. * * @since 1.5.0 * * @param array|string|null $query Query using 'wpforms_challenge' schema keys. * * @return array|mixed */ public function get_challenge_option( $query = null ) { if ( ! $query ) { return \get_option( 'wpforms_challenge' ); } if ( ! \is_array( $query ) ) { $return_single = true; $query = array( $query ); } $query = \array_flip( $query ); $option = \get_option( 'wpforms_challenge' ); if ( ! $option || ! \is_array( $option ) ) { return \array_intersect_key( $this->get_challenge_option_schema(), $query ); } $result = \array_intersect_key( $option, $query ); if ( $return_single ) { $result = \reset( $result ); } return $result; } /** * Set Challenge parameter(s) to Challenge option. * * @since 1.5.0 * * @param array $query Query using 'wpforms_challenge' schema keys. */ public function set_challenge_option( $query ) { if ( empty( $query ) || ! \is_array( $query ) ) { return; } $schema = $this->get_challenge_option_schema(); $replace = \array_intersect_key( $query, $schema ); if ( ! $replace ) { return; } // Validate and sanitize the data. foreach ( $replace as $key => $value ) { if ( \in_array( $key, array( 'step', 'user_id', 'form_id', 'embed_page', 'seconds_spent', 'seconds_left' ), true ) ) { $replace[ $key ] = \absint( $value ); continue; } if ( \in_array( $key, array( 'feedback_sent', 'feedback_contact_me' ), true ) ) { $replace[ $key ] = \wp_validate_boolean( $value ); continue; } $replace[ $key ] = \sanitize_text_field( $value ); } $option = \get_option( 'wpforms_challenge' ); if ( ! $option || ! \is_array( $option ) ) { \update_option( 'wpforms_challenge', \array_merge( $schema, $replace ) ); return; } \update_option( 'wpforms_challenge', \array_merge( $option, $replace ) ); } /** * Check if any forms are present on a site. * * @since 1.5.0 */ public function website_has_forms() { return (bool) \wpforms()->form->get( '', array( 'numberposts' => 1 ) ); } /** * Check if Challenge was started. * * @since 1.5.0 */ public function challenge_started() { return 'started' === $this->get_challenge_option( 'status' ); } /** * Check if Challenge was finished. * * @since 1.5.0 */ public function challenge_finished() { $status = $this->get_challenge_option( 'status' ); return \in_array( $status, array( 'completed', 'canceled', 'skipped' ), true ); } /** * Check if Challenge is in progress. * * @since 1.5.0 */ public function challenge_active() { return $this->challenge_started() && ! $this->challenge_finished(); } /** * Check if Challenge can be started. * * @since 1.5.0 */ public function challenge_can_start() { if ( $this->website_has_forms() ) { return false; } if ( $this->challenge_started() || $this->challenge_finished() ) { return false; } return true; } /** * Start the Challenge in Form Builder. * * @since 1.5.0 */ public function start_challenge() { if ( ! isset( $_GET['challenge'] ) || 'start' !== $_GET['challenge'] ) { return; } if ( ! $this->challenge_can_start() ) { return; } $this->set_challenge_option( array( 'status' => 'started', 'started_date_gmt' => \current_time( 'mysql', true ), ) ); \wp_safe_redirect( \remove_query_arg( 'challenge' ) ); } /** * Include Challenge HTML. * * @since 1.5.0 */ public function challenge_html() { if ( $this->challenge_finished() ) { return; } if ( \wpforms_is_admin_page() && ! \wpforms_is_admin_page( 'getting-started' ) && $this->challenge_can_start() ) { $this->challenge_modal_html( 'start' ); } if ( $this->is_builder_page() ) { $this->challenge_modal_html( 'progress' ); $this->challenge_builder_templates_html(); } if ( $this->is_form_embed_page() ) { $this->challenge_modal_html( 'progress' ); $this->challenge_embed_templates_html(); } } /** * Include Challenge main modal window HTML. * * @since 1.5.0 * * @param string $state State of Challenge ('start' or 'progress'). */ public function challenge_modal_html( $state ) { ?>
WPForms Challenge and get up and running within %1$d %2$s.', 'wpforms-lite' ), \absint( $this->minutes ), \_n( 'minute', 'minutes', \absint( $this->minutes ), 'wpforms-lite' ) ), array( 'b' => array() ) ); ?>
' . \absint( $this->minutes ) .':00' ); ?>
%1$s %2$s %3$s %4$s. Share your success story with other WPForms users and help us spread the word by giving WPForms a 5-star rating (%5$s) on WordPress.org. Thanks for your support and we look forward to bringing more awesome features.', 'wpforms-lite' ), '', \_n( 'minute', 'minutes', \absint( $this->minutes ), 'wpforms-lite' ), '', \_n( 'second', 'seconds', \absint( $this->minutes ), 'wpforms-lite' ), '' ), array( 'span' => array( 'id' => array(), 'class' => array(), ), 'b' => array(), 'i' => array( 'class' => array(), ), ) ); ?>