__( 'Insufficient permissions for a site-wide change.', 'ai-eshop-optimizer' ) ) ); } } public function ajax_goals_save() { $this->guard_manager(); if ( ! class_exists( 'AIEO_Goal_Tracker' ) ) { require_once AIEO_DIR . 'includes/class-aieo-goal-tracker.php'; } $forms = isset( $_POST['forms'] ) ? json_decode( wp_unslash( $_POST['forms'] ), true ) : array(); $builtin = isset( $_POST['builtin'] ) ? json_decode( wp_unslash( $_POST['builtin'] ), true ) : array(); $values = isset( $_POST['values'] ) ? json_decode( wp_unslash( $_POST['values'] ), true ) : array(); AIEO_Goal_Tracker::save_config( is_array( $forms ) ? $forms : array(), is_array( $builtin ) ? $builtin : array(), is_array( $values ) ? $values : array() ); wp_send_json_success( array( 'message' => __( 'Goals saved.', 'ai-eshop-optimizer' ) ) ); } public function ajax_goal_analyze() { $this->guard_manager(); if ( ! class_exists( 'AIEO_Goal_Tracker' ) ) { require_once AIEO_DIR . 'includes/class-aieo-goal-tracker.php'; } $provider = isset( $_POST['provider'] ) ? sanitize_key( wp_unslash( $_POST['provider'] ) ) : ''; $form_id = isset( $_POST['form_id'] ) ? (int) $_POST['form_id'] : 0; if ( 'gravity' !== $provider || $form_id <= 0 ) { wp_send_json_error( array( 'message' => __( 'Invalid form.', 'ai-eshop-optimizer' ) ) ); } $res = AIEO_Goal_Tracker::analyze_form( $provider, $form_id ); if ( is_wp_error( $res ) ) { wp_send_json_error( array( 'message' => $res->get_error_message() ) ); } wp_send_json_success( $res ); } public function ajax_goals_form_insights() { $this->guard_manager(); $this->load_form_insights(); if ( ! class_exists( 'AIEO_Form_Insights' ) || ! AIEO_Form_Insights::available() ) { wp_send_json_error( array( 'message' => __( 'Gravity Forms is not active, so there are no entries to read.', 'ai-eshop-optimizer' ) ) ); } $form_id = isset( $_POST['form_id'] ) ? (int) $_POST['form_id'] : 0; $fresh = ! empty( $_POST['fresh'] ); if ( $form_id <= 0 ) { wp_send_json_error( array( 'message' => __( 'Invalid form.', 'ai-eshop-optimizer' ) ) ); } $report = AIEO_Form_Insights::distributions( $form_id, $fresh ); if ( is_wp_error( $report ) ) { wp_send_json_error( array( 'message' => $report->get_error_message() ) ); } update_user_meta( get_current_user_id(), self::INSIGHTS_USER_META, $form_id ); $report['html'] = self::render_insight_body( $report ); $report['pages_html'] = class_exists( 'AIEO_Page_Actions', false ) ? AIEO_Page_Actions::form_pages( 'gravity_' . $form_id ) : ''; wp_send_json_success( $report ); } private static function render_insight_body( array $report ) { $path = AIEO_DIR . 'assets/pages/dmm/form-insights-body.php'; if ( empty( $report['fields'] ) || ! file_exists( $path ) ) { return ''; } $insight_report = $report; ob_start(); require $path; return (string) ob_get_clean(); } private function load_form_insights() { if ( ! class_exists( 'AIEO_Form_Insights' ) && file_exists( AIEO_DIR . 'includes/class-aieo-form-insights.php' ) ) { require_once AIEO_DIR . 'includes/class-aieo-form-insights.php'; } } public function preferred_insight_form_public( array $forms ) { return $this->preferred_insight_form( $forms ); } private function preferred_insight_form( array $forms ) { if ( ! $forms ) { return 0; } $last = (int) get_user_meta( get_current_user_id(), self::INSIGHTS_USER_META, true ); $best = 0; $most = -1; foreach ( $forms as $f ) { $fid = (int) $f['id']; if ( $last > 0 && $fid === $last ) { return $fid; } if ( (int) $f['entries'] > $most ) { $most = (int) $f['entries']; $best = $fid; } } return $best; } public function render_subtab() { if ( ! class_exists( 'AIEO_Goal_Tracker' ) ) { require_once AIEO_DIR . 'includes/class-aieo-goal-tracker.php'; } $this->render_template( 'conversion-goals.php', array( 'goal_forms' => AIEO_Goal_Tracker::available_forms(), 'goal_config' => AIEO_Goal_Tracker::config(), 'nonce' => wp_create_nonce( AIEO_DMM::NONCE_ACTION ), 'ajax' => admin_url( 'admin-ajax.php' ), ) ); } }