| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Template_Functions |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Render an achievement |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param integer $achievement The Achievement's ID |
| 18 |
* @param array $template_args Template args |
| 19 |
* |
| 20 |
* @return string The Achievement's output |
| 21 |
*/ |
| 22 |
function gamipress_render_achievement( $achievement = 0, $template_args = array() ) { |
| 23 |
|
| 24 |
global $post, $gamipress_template_args; |
| 25 |
|
| 26 |
// Initialize GamiPress template args global |
| 27 |
$gamipress_template_args = wp_parse_args( $template_args, gamipress_achievement_shortcode_defaults() ); |
| 28 |
|
| 29 |
// If we were given an ID, get the post |
| 30 |
if ( is_numeric( $achievement ) ) { |
| 31 |
$post = gamipress_get_post( $achievement ); |
| 32 |
} else { |
| 33 |
$post = $achievement; |
| 34 |
} |
| 35 |
|
| 36 |
setup_postdata( $post ); |
| 37 |
|
| 38 |
// Enqueue assets |
| 39 |
if( ! (bool) gamipress_get_option( 'disable_css', false ) ) |
| 40 |
wp_enqueue_style( 'gamipress-css' ); |
| 41 |
|
| 42 |
if( ! (bool) gamipress_get_option( 'disable_js', false ) ) |
| 43 |
wp_enqueue_script( 'gamipress-js' ); |
| 44 |
|
| 45 |
// Try to load achievement-{post_type}.php, if not exists then load achievement.php |
| 46 |
ob_start(); |
| 47 |
gamipress_get_template_part( 'achievement', $post->post_type ); |
| 48 |
$output = ob_get_clean(); |
| 49 |
|
| 50 |
$output = apply_filters( 'gamipress_render_achievement', $output, $post->ID, $post ); |
| 51 |
|
| 52 |
wp_reset_postdata(); |
| 53 |
|
| 54 |
// Return our filterable markup |
| 55 |
return $output; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Render a rank |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param integer $rank The Rank's ID |
| 65 |
* @param array $template_args Template args |
| 66 |
* |
| 67 |
* @return string The Achievement's output |
| 68 |
*/ |
| 69 |
function gamipress_render_rank( $rank = 0, $template_args = array() ) { |
| 70 |
|
| 71 |
global $post, $gamipress_template_args; |
| 72 |
|
| 73 |
// Initialize GamiPress template args global |
| 74 |
$gamipress_template_args = wp_parse_args( $template_args, gamipress_rank_shortcode_defaults() ); |
| 75 |
|
| 76 |
// If we were given an ID, get the post |
| 77 |
if ( is_numeric( $rank ) ) { |
| 78 |
$post = gamipress_get_post( $rank ); |
| 79 |
} else { |
| 80 |
$post = $rank; |
| 81 |
} |
| 82 |
|
| 83 |
setup_postdata( $post ); |
| 84 |
|
| 85 |
// Enqueue assets |
| 86 |
if( ! (bool) gamipress_get_option( 'disable_css', false ) ) { |
| 87 |
wp_enqueue_style( 'gamipress-css' ); |
| 88 |
} |
| 89 |
|
| 90 |
if( ! (bool) gamipress_get_option( 'disable_js', false ) ) { |
| 91 |
wp_enqueue_script( 'gamipress-js' ); |
| 92 |
} |
| 93 |
|
| 94 |
// Try to load rank-{type}.php, if not exists then load rank.php |
| 95 |
ob_start(); |
| 96 |
gamipress_get_template_part( 'rank', $post->post_type ); |
| 97 |
$output = ob_get_clean(); |
| 98 |
|
| 99 |
$output = apply_filters( 'gamipress_render_rank', $output, $post->ID, $post ); |
| 100 |
|
| 101 |
wp_reset_postdata(); |
| 102 |
|
| 103 |
// Return our filterable markup |
| 104 |
return $output; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieves a template part |
| 110 |
* |
| 111 |
* Taken from Easy Digital Downloads |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @param string $slug |
| 116 |
* @param string $name Optional. Default null |
| 117 |
* @param bool $load |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
function gamipress_get_template_part( $slug, $name = null, $load = true ) { |
| 122 |
|
| 123 |
// Execute code for this part |
| 124 |
do_action( 'get_template_part_' . $slug, $slug, $name ); |
| 125 |
|
| 126 |
$load_template = apply_filters( 'gamipress_allow_template_part_' . $slug . '_' . $name, true ); |
| 127 |
if ( false === $load_template ) { |
| 128 |
return ''; |
| 129 |
} |
| 130 |
|
| 131 |
// Setup possible parts |
| 132 |
$templates = array(); |
| 133 |
if ( isset( $name ) ) |
| 134 |
$templates[] = $slug . '-' . $name . '.php'; |
| 135 |
$templates[] = $slug . '.php'; |
| 136 |
|
| 137 |
// Allow template parts to be filtered |
| 138 |
$templates = apply_filters( 'gamipress_get_template_part', $templates, $slug, $name ); |
| 139 |
|
| 140 |
// Return the part that is found |
| 141 |
return gamipress_locate_template( $templates, $load, false ); |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Retrieve the name of the highest priority template file that exists. |
| 147 |
* |
| 148 |
* Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which |
| 149 |
* inherit from a parent theme can just overload one file. If the template is |
| 150 |
* not found in either of those, it looks in the theme-compat folder last. |
| 151 |
* |
| 152 |
* Taken from Easy Digital Downloads |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
* |
| 156 |
* @param string|array $template_names Template file(s) to search for, in order. |
| 157 |
* @param bool $load If true the template file will be loaded if it is found. |
| 158 |
* @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false. |
| 159 |
* |
| 160 |
* @return string The template filename if one is located. |
| 161 |
*/ |
| 162 |
function gamipress_locate_template( $template_names, $load = false, $require_once = true ) { |
| 163 |
|
| 164 |
// No file found yet |
| 165 |
$located = false; |
| 166 |
|
| 167 |
// Try to find a template file |
| 168 |
foreach ( (array) $template_names as $template_name ) { |
| 169 |
|
| 170 |
// Continue if template is empty |
| 171 |
if ( empty( $template_name ) ) |
| 172 |
continue; |
| 173 |
|
| 174 |
// Trim off any slashes from the template name |
| 175 |
$template_name = ltrim( $template_name, '/' ); |
| 176 |
|
| 177 |
// try locating this template file by looping through the template paths |
| 178 |
foreach( gamipress_get_theme_template_paths() as $template_path ) { |
| 179 |
|
| 180 |
if( file_exists( $template_path . $template_name ) ) { |
| 181 |
$located = $template_path . $template_name; |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if( $located ) { |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if ( ( true == $load ) && ! empty( $located ) ) |
| 192 |
load_template( $located, $require_once ); |
| 193 |
|
| 194 |
return $located; |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns a list of paths to check for template locations |
| 200 |
* |
| 201 |
* @since 1.0.0 |
| 202 |
* |
| 203 |
* @return array |
| 204 |
*/ |
| 205 |
function gamipress_get_theme_template_paths() { |
| 206 |
|
| 207 |
$file_paths = array(); |
| 208 |
|
| 209 |
// Lets add-ons register template paths |
| 210 |
$file_paths = apply_filters( 'gamipress_template_paths', $file_paths ); |
| 211 |
|
| 212 |
// Prepend theme template paths |
| 213 |
array_unshift( |
| 214 |
$file_paths, |
| 215 |
trailingslashit( get_stylesheet_directory() ) . 'gamipress/', |
| 216 |
trailingslashit( get_template_directory() ) . 'gamipress/' |
| 217 |
); |
| 218 |
|
| 219 |
// GamiPress template path in last position |
| 220 |
$file_paths[] = GAMIPRESS_DIR . 'templates/'; |
| 221 |
|
| 222 |
return array_map( 'trailingslashit', $file_paths ); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Helper function to parse inline shortcode outputs |
| 228 |
* |
| 229 |
* @since 1.0.0 |
| 230 |
* |
| 231 |
* @return array |
| 232 |
*/ |
| 233 |
function gamipress_parse_inline_output( $output ) { |
| 234 |
|
| 235 |
// Remove line breaks |
| 236 |
$output = str_replace( "\r", "", $output ); |
| 237 |
$output = str_replace( "\n", "", $output ); |
| 238 |
|
| 239 |
// Remove tabs |
| 240 |
$output = str_replace( "\t", "", $output ); |
| 241 |
|
| 242 |
// Remove double spaces |
| 243 |
$output = str_replace( " ", "", $output ); |
| 244 |
|
| 245 |
// Remove spaces between tags |
| 246 |
$output = str_replace( "> <", "><", $output ); |
| 247 |
|
| 248 |
|
| 249 |
return $output; |
| 250 |
} |