function display_shortcodes_meta_box() { global $post; echo "

" . __('Add this shortcode to the page where you would like to display this goal:', 'before-and-after') . "

"; echo $this->get_goal_shortcode_textbox($post->ID); echo "

" . __('If you\'re using Contact Form 7 or Gravity Form, the goal will be automatically completed when the form is submitted.', 'before-and-after') . "

"; echo "

" . __('Otherwise, you can add this shortcode to any page (e.g., your Thank You page) to manually mark the goal as complete:', 'before-and-after') . "

"; echo $this->get_complete_goal_shortcode_textbox($post->ID); } /* Displays a meta box allowing the user to protect the current page with a Goal*/ function display_protection_meta_box() { // init global $post; $has_b_a_hook = get_post_meta($post->ID, '_b_a_required_goal', true); $enabled = !empty($has_b_a_hook) && !empty($has_b_a_hook['enabled']) ? 1 : 0; $current_goal_id = !empty($has_b_a_hook) && !empty($has_b_a_hook['goal_id']) ? $has_b_a_hook['goal_id'] : 0; $bounce_url = !empty($has_b_a_hook) && !empty($has_b_a_hook['bounce_url']) ? $has_b_a_hook['bounce_url'] : ''; // output form inputs echo '

Page Protection

'; echo '

Enable Page Protection to require your visitors to complete one of your Goals before they can access this page. User who have not completed the Goal will be directed to the URL of your choice.

'; echo '

'; $checked = $enabled ? 'checked="checked"' : ''; echo ''; printf( '', $checked ); echo '

'; echo '
'; echo '

'; printf( '', $current_goal_id ); echo '
'; $this->generate_post_select( 'b_a_protection[goal_id]', 'b_a_protection_goal_id', self::post_type, $current_goal_id); echo '

'; echo '

'; echo '
'; printf( '', htmlentities($bounce_url) ); printf( '', htmlentities($bounce_url) ); echo '

'; echo '
'; echo ''; } function generate_post_select($select_id, $select_name, $post_type, $selected = 0, $include_select_all_option = false) { $post_type_object = get_post_type_object($post_type); $label = $post_type_object->label; $args = array( 'post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=> -1 ); $posts = get_posts($args); echo ''; } /* * Returns true/false, indicating whether the specified goal has been completed * based on the database / cookies / session. * * NOTE: Whichever method (database, cookies, session) is found will update the others. * * @param string $goalName Required. The name of the goal to check. * @param int $goalId Optional. The ID of the goal to check. Needed to check * cookies as well as session. Cookie check will be ignored * if omitted, but session check will remain in place. */ public function wasGoalCompleted($goalName, $goalId = 0) { $goal_completed = false; if ($goalId == 0) { $goalId = filter_var($goalName, FILTER_SANITIZE_NUMBER_INT); } // check database (post meta) first if ( $goalId > 0 && $this->check_goal_complete_in_post_meta($goalId) ) { // update cookies if needed if ( !$this->check_cookie_for_goal($goalId) ) { $this->store_goal_completion_cookies($goalName, $goalId); } // update session if needed if ( !$this->check_goal_complete_in_session($goalName) ) { $this->mark_goal_complete_in_session($goalName); } $goal_completed = true; } // if not found in goal's post meta, check in cookies else if ( $goalId > 0 && $this->check_cookie_for_goal($goalId) ) { // mark complete in post meta $this->mark_goal_complete_in_post_meta($goalId); // update session if needed if ( !$this->check_goal_complete_in_session($goalName) ) { $this->mark_goal_complete_in_session($goalName); } $goal_completed = true; } // if not found in the cookies, check $_SESSION else if ( $this->check_goal_complete_in_session($goalName) ) { // mark complete in post meta and cookies $this->mark_goal_complete_in_post_meta($goalId); $this->store_goal_completion_cookies($goalName, $goalId); $goal_completed = true; } // allow the user to modify the result with a filter, then return it return apply_filters('b_a_was_goal_complete', $goal_completed, $goalName, $goalId); } /* Place a session variable that marks the current visitor as having completed the specified goal * Note: this function does not support Conversion logging or server side goal completion. * Use completeGoalById for that (names were only used in v1, before these features were added) */ public function completeGoal($goalName) { $alreadyCompleted = $this->wasGoalCompleted($goalName); if (!$alreadyCompleted) { $params = array( 'goal_name' => $goalName, 'goal_id' => false, ); do_action('b_a_goal_complete', $params); } $this->store_goal_completion_cookies($goalName); return ''; } /* Place a session variable that marks the current visitor as having completed the specified goal * Note: Will also log a conversion (Pro only) */ function completeGoalById($goalId, $goal_complete_url = '') { $goalName = 'Goal_ID_' . $goalId; $alreadyCompleted = $this->wasGoalCompleted($goalName, $goalId); $goal_after_action = get_post_meta($goalId, '_goal_after_action', true); if (!$alreadyCompleted) { $params = array( 'goal_name' => $goalName, 'goal_id' => $goalId, 'goal_complete_url' => $goal_complete_url, 'goal_after_action' => $goal_after_action ); do_action('b_a_goal_complete', $params); do_action('b_a_goal_complete_' . $goalId, $params); } // mark this goal as completed (in the session and in post meta) $this->store_goal_completion_cookies($goalName, $goalId); $this->mark_goal_complete_in_post_meta($goalId); return ''; } // saves the value of a POST variable to the database // Important: WILL save blank values private function update_goal_setting_from_post($post_id, $request_key, $meta_key) { $post_val = isset($_POST[$request_key]) ? $_POST[$request_key] : ''; return update_post_meta($post_id, $meta_key, $post_val); } // returns the "checked" attribute for a radio button, depending on whether the setting specified matches the test value specified // returns either the string 'checked="checked"', or an empty string ''. These are intended to be used inside an HTML tag private function is_radio_checked($goal_id, $setting_name, $setting_value) { $val = ''; if ($setting_name == 'before-action') { $val = get_post_meta($goal_id, '_goal_before_action', true); } else if ($setting_name == 'after-action') { $val = get_post_meta($goal_id, '_goal_after_action', true); } if ( !empty($val) && ($val == $setting_value) ) { return 'checked="checked"'; } else { return ''; } } // returns the "selected" attribute for an