, Ruben Garcia * @since 1.0.0 */ // Exit if accessed directly if( !defined( 'ABSPATH' ) ) exit; /** * Add filters to remove stuff from our singular pages and add back in how we want it * * @since 1.0.0 */ function gamipress_do_single_filters() { // Check we're in the right place if ( gamipress_is_single_achievement() || gamipress_is_single_rank() ) { // Filter out the post title // add_filter( 'the_title', 'gamipress_remove_to_reformat_entries_title', 10, 2 ); // and filter out the post image add_filter( 'post_thumbnail_html', 'gamipress_remove_to_reformat_entries_thumbnail', 10, 2 ); } } add_action( 'wp_enqueue_scripts', 'gamipress_do_single_filters' ); /** * Filter out the post title/post image and add back (later) how we want it * * @since 1.0.0 * * @param string $html The page content prior to filtering * @param int $post_id The post ID. * * @return string The page content after being filtered */ function gamipress_remove_to_reformat_entries_title( $html = '', $post_id = 0 ) { global $gamipress_template_args; // Remove, but only on the main loop! if ( ( gamipress_is_single_achievement( $post_id ) || gamipress_is_single_rank( $post_id ) ) // Ensure that is an achievement or a rank && empty( $GLOBALS['gamipress_reformat_title'] ) // Ensure to pass it only one time && ! is_array( $gamipress_template_args ) // Prevents to pass this check on GamiPress shortcodes ) { // Now that we're where we want to be, tell the filters to stop removing $GLOBALS['gamipress_reformat_title'] = true; return ''; } // Nothing to see here... move along return $html; } /** * Filter out the post title/post image and add back (later) how we want it * * @since 1.0.0 * * @param string $html The page content prior to filtering * @param int $post_id The post ID. * * @return string The page content after being filtered */ function gamipress_remove_to_reformat_entries_thumbnail( $html = '', $post_id = 0 ) { global $gamipress_template_args; // Remove, but only on the main loop! if ( ( gamipress_is_single_achievement( $post_id ) || gamipress_is_single_rank( $post_id ) ) // Ensure that is an achievement or a rank && empty( $GLOBALS['gamipress_reformat_thumbnail'] ) // Ensure to pass it only one time && ! is_array( $gamipress_template_args ) // Prevents to pass this check on GamiPress shortcodes ) { // Now that we're where we want to be, tell the filters to stop removing $GLOBALS['gamipress_reformat_thumbnail'] = true; return ''; } // Nothing to see here... move along return $html; } /** * Filter achievement content to add our removed content back * * @since 1.0.0 * * @param string $content The page content * * @return string The page content after reformat */ function gamipress_reformat_entries( $content ) { if ( gamipress_is_single_achievement( get_the_ID() ) ) { // Filter content, but only is a single achievement! return gamipress_apply_single_template( $content, 'single-achievement' ); } else if ( gamipress_is_single_rank( get_the_ID() ) ) { // Filter content, but only is a single rank! return gamipress_apply_single_template( $content, 'single-rank' ); } return $content; } add_filter( 'the_content', 'gamipress_reformat_entries' ); /** * Apply the given single template * * @since 1.3.1 * * @param string $content * @param string $single_template * * @return string */ function gamipress_apply_single_template( $content, $single_template = '' ) { global $gamipress_template_args; // Now that we're where we want to be, tell the filters to stop removing $GLOBALS['gamipress_reformat_content'] = true; // Initialize GamiPress template args global $gamipress_template_args = array(); // Get the original post content (to use as achievement description) $gamipress_template_args['original_content'] = $content; // Set the configured layout $gamipress_template_args['layout'] = gamipress_get_post_meta( get_the_ID(), '_gamipress_layout' ); // If not layout defined, fallback to left layout if( empty( $gamipress_template_args['layout'] ) ) { $gamipress_template_args['layout'] = 'left'; } // Set the configured alignment $gamipress_template_args['align'] = gamipress_get_post_meta( get_the_ID(), '_gamipress_align' ); // If not alignment defined, fallback to none alignment if( empty( $gamipress_template_args['align'] ) ) { $gamipress_template_args['align'] = 'none'; } /** * Single template arguments * * @since 7.6.5 * * @param array $gamipress_template_args * @param string $single_template */ $gamipress_template_args = apply_filters( 'gamipress_single_template_args', $gamipress_template_args, $single_template ); ob_start(); // Try to load single-{template}-{post_type}.php, if not exists load single-{template}.php gamipress_get_template_part( $single_template, gamipress_get_post_type( get_the_ID() ) ); $new_content = ob_get_clean(); // Ok, we're done reformatting $GLOBALS['gamipress_reformat_content'] = false; return $new_content; } /** * Helper function tests that we're in the main loop * * @since 1.3.1 * * @param bool|integer $id The page id * * @return boolean A boolean determining if the function is in the main loop */ function gamipress_is_single_achievement( $id = false ) { $slugs = gamipress_get_achievement_types_slugs(); // only run our filters on the achievement singular page if ( is_admin() || empty( $slugs ) || ! is_singular( $slugs ) ) return false; // w/o id, we're only checking template context if ( ! $id ) return true; // Checks several variables to be sure we're in the main loop (and won't effect things like post pagination titles) return ( ( $GLOBALS['post']->ID == $id ) && in_the_loop() && empty( $GLOBALS['gamipress_reformat_content'] ) ); } /** * Generate HTML markup for an points type's points awards * * @since 1.0.0 * * @param array $points_awards An points type's points awards * @param int $user_id The given user's ID * @param array $template_args The given template args * * @return string The markup for our list */ function gamipress_get_points_awards_for_points_types_list_markup( $points_awards = array(), $user_id = 0, $template_args = array() ) { // If we don't have any points awards, or our points awards aren't an array, return nothing if ( ! $points_awards || ! is_array( $points_awards ) ) { return null; } $count = count( $points_awards ); // If we have no points awards, return nothing if ( ! $count ) { return null; } // Grab the current user's ID if none was specifed if ( ! $user_id ) { $user_id = get_current_user_id(); } $a = wp_parse_args( $template_args, gamipress_points_types_shortcode_defaults() ); // Setup our variables $output = ''; if( $a['heading'] === 'yes' ) { $points_type = gamipress_get_points_award_points_type( $points_awards[0]->ID ); if( $points_type ) { $plural_name = gamipress_get_post_meta( '_gamipress_plural_name', $points_type->ID ); if( ! $plural_name ) $plural_name = $points_type->post_title; $points_awards_heading = sprintf( '%1$d %2$s %3$s', $count, $plural_name, _n( 'Award', 'Awards', $count, 'gamipress' ) ); // 2 Credits Awards } else { $points_awards_heading = sprintf( '%1$d %2$s', $count, _n( 'Award', 'Awards', $count, 'gamipress' ) ); // 2 Awards } /** * Filters the points award heading text * * @since 1.0.0 * * @param string $points_awards_heading The heading text (eg: 2 Points Awards) * @param array $points_awards The points awards * @param int $user_id The user's ID * @param array $template_args The given template args * * @return string */ $points_awards_heading = apply_filters( 'gamipress_points_awards_heading', $points_awards_heading, $points_awards, $user_id, $template_args ); $output .= '<' . $a['heading_size'] . ' class="gamipress-points-awards-heading">' . $points_awards_heading . ''; } $output .= ''; // Return our output return $output; } /** * Generate HTML markup for an points type's points deducts * * @since 1.3.7 * * @param array $points_deducts An points type's points deducts * @param int $user_id The given user's ID * @param array $template_args The given template args * * @return string The markup for our list */ function gamipress_get_points_deducts_for_points_types_list_markup( $points_deducts = array(), $user_id = 0, $template_args = array() ) { // If we don't have any points deducts, or our points deducts aren't an array, return nothing if ( ! $points_deducts || ! is_array( $points_deducts ) ) { return null; } $count = count( $points_deducts ); // If we have no points deducts, return nothing if ( ! $count ) { return null; } // Grab the current user's ID if none was specifed if ( ! $user_id ) { $user_id = get_current_user_id(); } $a = wp_parse_args( $template_args, gamipress_points_types_shortcode_defaults() ); // Setup our variables $output = ''; if( $a['heading'] === 'yes' ) { $points_type = gamipress_get_points_deduct_points_type( $points_deducts[0]->ID ); if( $points_type ) { $plural_name = gamipress_get_post_meta( '_gamipress_plural_name', $points_type->ID ); if( ! $plural_name ) { $plural_name = $points_type->post_title; } $points_deducts_heading = sprintf( '%1$d %2$s %3$s', $count, $plural_name, _n( 'Deduction', 'Deductions', $count, 'gamipress' ) ); // 2 Credits Deducts } else { $points_deducts_heading = sprintf( '%1$d %2$s', $count, _n( 'Deduction', 'Deductions', $count, 'gamipress' ) ); // 2 Deductions } /** * Filters the points deduct heading text * * @since 1.3.7 * * @param string $points_deducts_heading The heading text (eg: 2 Points Deductions) * @param array $points_deducts The points deducts * @param int $user_id The user's ID * @param array $template_args The given template args * * @return string */ $points_deducts_heading = apply_filters( 'gamipress_points_deducts_heading', $points_deducts_heading, $points_deducts, $user_id, $template_args ); $output .= '<' . $a['heading_size'] . ' class="gamipress-points-deducts-heading">' . $points_deducts_heading . ''; } $output .= ''; // Return our output return $output; } /** * Gets achievement's required steps and returns HTML markup for these steps * * @since 1.0.0 * @param int $achievement_id The given achievement's post ID * @param int $user_id A given user's ID * * @return string The markup for our list */ function gamipress_get_required_achievements_for_achievement_list( $achievement_id = 0, $user_id = 0 ) { // Grab the current post ID if no achievement_id was specified if ( ! $achievement_id ) { global $post; $achievement_id = $post->ID; } // Grab the current user's ID if none was specifed if ( ! $user_id ) $user_id = wp_get_current_user()->ID; // Grab our achievement's required steps $steps = gamipress_get_achievement_steps( $achievement_id ); // Return our markup output return gamipress_get_required_achievements_for_achievement_list_markup( $steps, $user_id ); } /** * Generate HTML markup for an achievement's required steps * * This will generate an unorderd list (