| @@ -1,0 +1,388 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Shortcodes | |
| 4 | + * | |
| 5 | + * @package GamiPress\Shortcodes | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +// GamiPress Shortcodes Editor | |
| 13 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/shortcodes-editor.php'; | |
| 14 | + | |
| 15 | +// GamiPress Shortcodes | |
| 16 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/shortcode.php'; | |
| 17 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_achievement.php'; | |
| 18 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_achievements.php'; | |
| 19 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_last_achievements_earned.php'; | |
| 20 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_earnings.php'; | |
| 21 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_logs.php'; | |
| 22 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_user_points.php'; | |
| 23 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_site_points.php'; | |
| 24 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_points.php'; | |
| 25 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_points_types.php'; | |
| 26 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_rank.php'; | |
| 27 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_ranks.php'; | |
| 28 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_user_rank.php'; | |
| 29 | +// Inline Shortcodes | |
| 30 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_inline_achievement.php'; | |
| 31 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_inline_last_achievements_earned.php'; | |
| 32 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_inline_rank.php'; | |
| 33 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_inline_user_rank.php'; | |
| 34 | +// Email settings | |
| 35 | +require_once GAMIPRESS_DIR . 'includes/shortcodes/gamipress_email_settings.php'; | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Register a new GamiPress Shortcode | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + * | |
| 42 | + * @param array $args Shortcode Args. | |
| 43 | + * | |
| 44 | + * @return object Shortcode Object. | |
| 45 | + */ | |
| 46 | +function gamipress_register_shortcode( $shortcode, $args ) { | |
| 47 | + | |
| 48 | + GamiPress()->shortcodes[ $shortcode ] = new GamiPress_Shortcode( $shortcode, $args ); | |
| 49 | + | |
| 50 | + return GamiPress()->shortcodes[ $shortcode ]; | |
| 51 | + | |
| 52 | +} | |
| 53 | + | |
| 54 | +/** | |
| 55 | + * Get all registered GamiPress shortcodes. | |
| 56 | + * | |
| 57 | + * @since 1.0.0 | |
| 58 | + * | |
| 59 | + * @return array Registered GamiPress shortcodes. | |
| 60 | + */ | |
| 61 | +function gamipress_get_shortcodes() { | |
| 62 | + | |
| 63 | + return apply_filters( 'gamipress_shortcodes', GamiPress()->shortcodes ); | |
| 64 | + | |
| 65 | +} | |
| 66 | + | |
| 67 | +/** | |
| 68 | + * Get all registered shortcodes groups. | |
| 69 | + * | |
| 70 | + * @since 1.7.6 | |
| 71 | + * | |
| 72 | + * @return array Registered shortcodes groups. | |
| 73 | + */ | |
| 74 | +function gamipress_get_shortcodes_groups() { | |
| 75 | + | |
| 76 | + return apply_filters( 'gamipress_shortcodes_groups', array( | |
| 77 | + 'gamipress' => 'GamiPress', | |
| 78 | + 'others' => __( 'Others', 'gamipress' ), | |
| 79 | + ) ); | |
| 80 | + | |
| 81 | +} | |
| 82 | + | |
| 83 | +/** | |
| 84 | + * Add all shortcodes to the help page. | |
| 85 | + * | |
| 86 | + * @since 1.0.0 | |
| 87 | + */ | |
| 88 | +function gamipress_help_support_page_shortcodes() { | |
| 89 | + | |
| 90 | + foreach ( gamipress_get_shortcodes() as $shortcode ) { | |
| 91 | + gamipress_shortcode_help_render_help( $shortcode ); | |
| 92 | + } | |
| 93 | + | |
| 94 | +} | |
| 95 | +add_action( 'gamipress_help_support_page_shortcodes', 'gamipress_help_support_page_shortcodes' ); | |
| 96 | + | |
| 97 | +/** | |
| 98 | + * Render help section for a given shortcode. | |
| 99 | + * | |
| 100 | + * @since 1.0.0 | |
| 101 | + * | |
| 102 | + * @param GamiPress_Shortcode $shortcode Shortcode object. | |
| 103 | + */ | |
| 104 | +function gamipress_shortcode_help_render_help( $shortcode ) { | |
| 105 | + printf( | |
| 106 | + '<hr/> | |
| 107 | + <strong style="font-size: 14px;">%1$s – [%2$s]</strong> <a href="#" onclick="jQuery(this).next().slideToggle();return false;">%3$s</a> | |
| 108 | + <div style="display: none;"> | |
| 109 | + <p>%4$s</p> | |
| 110 | + <ul> | |
| 111 | + <li><strong>%5$s</strong></li> | |
| 112 | + %6$s | |
| 113 | + </ul> | |
| 114 | + <p>%7$s</p> | |
| 115 | + </div>', | |
| 116 | + $shortcode->name, | |
| 117 | + $shortcode->slug, | |
| 118 | + __( 'More details', 'gamipress' ), | |
| 119 | + $shortcode->description, | |
| 120 | + __( 'Attributes:', 'gamipress' ), | |
| 121 | + gamipress_shortcode_help_render_fields( $shortcode->fields ), | |
| 122 | + gamipress_shortcode_help_render_example( $shortcode ) | |
| 123 | + ); | |
| 124 | +} | |
| 125 | + | |
| 126 | +/** | |
| 127 | + * Render attributes portion of shortcode help section. | |
| 128 | + * | |
| 129 | + * @since 1.0.0 | |
| 130 | + * | |
| 131 | + * @param array $fields Shortcode fields. | |
| 132 | + * @return string HTML Markup. | |
| 133 | + */ | |
| 134 | +function gamipress_shortcode_help_render_fields( $fields ) { | |
| 135 | + | |
| 136 | + $output = ''; | |
| 137 | + | |
| 138 | + if ( ! empty( $fields ) ) { | |
| 139 | + foreach ( $fields as $field_id => $field ) { | |
| 140 | + | |
| 141 | + if( $field['type'] === 'title' ) { | |
| 142 | + continue; | |
| 143 | + } | |
| 144 | + | |
| 145 | + // Checkboxes without default means 'no' as default | |
| 146 | + if( $field['type'] === 'checkbox' && empty( $field['default'] ) ) { | |
| 147 | + $field['default'] = 'no'; | |
| 148 | + } | |
| 149 | + | |
| 150 | + if( isset( $field['default'] ) && is_array( $field['default'] ) ) { | |
| 151 | + $field['default'] = implode( ',', $field['default'] ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + $accepts = isset( $field['options'] ) && ! empty( $field['options'] ) ? sprintf( __( 'Accepts: %s', 'gamipress' ), '<code>' . implode( '</code>, <code>', array_keys( $field['options'] ) ) . '</code>' ) : ''; | |
| 155 | + $default = isset( $field['default'] ) && ! empty( $field['default'] ) ? sprintf( __( 'Default: %s', 'gamipress' ), '<code>' . $field['default'] . '</code>' ) : ''; | |
| 156 | + | |
| 157 | + // Setup the description (allowing provide it from description, desc and shortcode_desc | |
| 158 | + $description = ''; | |
| 159 | + $description = isset( $field['description'] ) ? $field['description'] : $description; | |
| 160 | + $description = isset( $field['desc'] ) ? $field['desc'] : $description; | |
| 161 | + $description = isset( $field['shortcode_desc'] ) ? $field['shortcode_desc'] : $description; | |
| 162 | + | |
| 163 | + $output .= sprintf( | |
| 164 | + '<li><strong>%1$s</strong> – %2$s <em>%3$s %4$s</em></li>', | |
| 165 | + esc_attr( $field_id ), | |
| 166 | + $description, | |
| 167 | + $accepts, | |
| 168 | + $default | |
| 169 | + ); | |
| 170 | + | |
| 171 | + } | |
| 172 | + } | |
| 173 | + | |
| 174 | + return $output; | |
| 175 | + | |
| 176 | +} | |
| 177 | + | |
| 178 | +/** | |
| 179 | + * Render example shortcode usage for help section. | |
| 180 | + * | |
| 181 | + * @since 1.0.0 | |
| 182 | + * | |
| 183 | + * @param GamiPress_Shortcode $shortcode Shortcode object. | |
| 184 | + * @return string HTML Markup. | |
| 185 | + */ | |
| 186 | +function gamipress_shortcode_help_render_example( $shortcode ) { | |
| 187 | + | |
| 188 | + $fields = @wp_list_pluck( $shortcode->fields, 'default' ); | |
| 189 | + $examples = array_map( 'gamipress_shortcode_help_attributes', array_keys( $fields ), array_values( $fields ) ); | |
| 190 | + $flattened_examples = implode( ' ', $examples ); | |
| 191 | + | |
| 192 | + return sprintf( __( 'Example: %s', 'gamipress' ), "<code>[{$shortcode->slug} {$flattened_examples}]</code>" ); | |
| 193 | + | |
| 194 | +} | |
| 195 | + | |
| 196 | +/** | |
| 197 | + * Render attribute="value" for attributes in shortcode example. | |
| 198 | + * | |
| 199 | + * @since 1.0.0 | |
| 200 | + * | |
| 201 | + * @param string $key Key name. | |
| 202 | + * @param string $value Value. | |
| 203 | + * @return string key="value". | |
| 204 | + */ | |
| 205 | +function gamipress_shortcode_help_attributes( $key, $value ) { | |
| 206 | + | |
| 207 | + switch( $key ) { | |
| 208 | + case 'user_id': | |
| 209 | + $value = get_current_user_id(); | |
| 210 | + break; | |
| 211 | + case 'wpms': | |
| 212 | + $value = is_multisite() ? 'yes' : 'no'; | |
| 213 | + break; | |
| 214 | + } | |
| 215 | + | |
| 216 | + if( is_array( $value ) ) | |
| 217 | + $value = implode( ',', $value ); | |
| 218 | + | |
| 219 | + return "{$key}=\"$value\""; | |
| 220 | + | |
| 221 | +} | |
| 222 | + | |
| 223 | +/** | |
| 224 | + * Remove multisite specific fields | |
| 225 | + * | |
| 226 | + * @since 1.2.0 | |
| 227 | + * @updated 1.4.9 wpms field is removed also if GamiPress is network wide active | |
| 228 | + * | |
| 229 | + * @param array $fields | |
| 230 | + * | |
| 231 | + * @return array | |
| 232 | + */ | |
| 233 | +function gamipress_shortcodes_remove_multisite_fields( $fields ) { | |
| 234 | + | |
| 235 | + if( ! is_multisite() || gamipress_is_network_wide_active() ) { | |
| 236 | + if( isset( $fields['wpms'] ) ) { | |
| 237 | + unset( $fields['wpms'] ); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 241 | + return $fields; | |
| 242 | + | |
| 243 | +} | |
| 244 | +add_filter( 'gamipress_gamipress_achievements_shortcode_fields', 'gamipress_shortcodes_remove_multisite_fields' ); | |
| 245 | +add_filter( 'gamipress_gamipress_points_shortcode_fields', 'gamipress_shortcodes_remove_multisite_fields' ); | |
| 246 | +add_filter( 'gamipress_gamipress_points_types_shortcode_fields', 'gamipress_shortcodes_remove_multisite_fields' ); | |
| 247 | +add_filter( 'gamipress_gamipress_ranks_shortcode_fields', 'gamipress_shortcodes_remove_multisite_fields' ); | |
| 248 | + | |
| 249 | +/** | |
| 250 | + * Function to report form error just if logged in user has permissions to manage GamiPress | |
| 251 | + * | |
| 252 | + * @since 1.5.9 | |
| 253 | + * | |
| 254 | + * @param string $error_message | |
| 255 | + * @param string $shortcode | |
| 256 | + * | |
| 257 | + * @return string | |
| 258 | + */ | |
| 259 | +function gamipress_shortcode_error( $error_message, $shortcode ) { | |
| 260 | + | |
| 261 | + global $gamipress_current_widget; | |
| 262 | + | |
| 263 | + // Setup label and shortcode label to customize message for shortcodes, widgets or blocks | |
| 264 | + switch( gamipress_get_renderer() ) { | |
| 265 | + case 'widget': | |
| 266 | + $label = __( 'Widget:', 'gamipress' ); | |
| 267 | + $shortcode_label = $gamipress_current_widget->name; | |
| 268 | + break; | |
| 269 | + case 'block': | |
| 270 | + $label = __( 'Block:', 'gamipress' ); | |
| 271 | + $shortcode_label = GamiPress()->shortcodes[$shortcode]->name; | |
| 272 | + break; | |
| 273 | + case 'shortcode': | |
| 274 | + default: | |
| 275 | + $label = __( 'Shortcode:', 'gamipress' ); | |
| 276 | + $shortcode_label = '[' . $shortcode . ']'; | |
| 277 | + break; | |
| 278 | + } | |
| 279 | + | |
| 280 | + if( current_user_can( gamipress_get_manager_capability() ) ) { | |
| 281 | + // Notify to admins about the error | |
| 282 | + return '<div class="gamipress-shortcode-error">' | |
| 283 | + . '<div class="gamipress-shortcode-error-content">' | |
| 284 | + . $error_message | |
| 285 | + . '</div>' | |
| 286 | + . '<div class="gamipress-shortcode-error-shortcode">' | |
| 287 | + . $label . ' ' . $shortcode_label | |
| 288 | + . '</div>' | |
| 289 | + . '<div class="gamipress-shortcode-error-reminder">' | |
| 290 | + . __( 'Message visible only to administrators.', 'gamipress' ) | |
| 291 | + . '</div>' | |
| 292 | + . '</div>'; | |
| 293 | + } else { | |
| 294 | + // Do not output anything for non admins | |
| 295 | + return ''; | |
| 296 | + } | |
| 297 | + | |
| 298 | +} | |
| 299 | + | |
| 300 | +/** | |
| 301 | + * Function to get the actual renderer | |
| 302 | + * | |
| 303 | + * @since 2.0.0 | |
| 304 | + * | |
| 305 | + * @return string shortcode | block | widget | |
| 306 | + */ | |
| 307 | +function gamipress_get_renderer() { | |
| 308 | + | |
| 309 | + global $gamipress_renderer, $wp; | |
| 310 | + | |
| 311 | + // Support for block Rest API rendering | |
| 312 | + if( isset( $wp->query_vars[ 'rest_route' ] ) | |
| 313 | + && ! empty( $wp->query_vars[ 'rest_route' ] ) | |
| 314 | + && strpos( $wp->query_vars[ 'rest_route' ], 'block-renderer' ) !== false ) { | |
| 315 | + $gamipress_renderer = 'block'; | |
| 316 | + } | |
| 317 | + | |
| 318 | + if( ! $gamipress_renderer ) { | |
| 319 | + $gamipress_renderer = 'shortcode'; | |
| 320 | + } | |
| 321 | + | |
| 322 | + return $gamipress_renderer; | |
| 323 | + | |
| 324 | +} | |
| 325 | + | |
| 326 | +/** | |
| 327 | + * Function to set the current renderer | |
| 328 | + * | |
| 329 | + * @since 1.5.9 | |
| 330 | + * | |
| 331 | + * @param string $renderer | |
| 332 | + */ | |
| 333 | +function gamipress_set_renderer( $renderer ) { | |
| 334 | + | |
| 335 | + global $gamipress_renderer; | |
| 336 | + | |
| 337 | + $gamipress_renderer = $renderer; | |
| 338 | + | |
| 339 | +} | |
| 340 | + | |
| 341 | +/** | |
| 342 | + * Helper function to build an array of shortcode attributes from the values given | |
| 343 | + * | |
| 344 | + * @since 1.7.0 | |
| 345 | + * | |
| 346 | + * @param string $shortcode | |
| 347 | + * @param array $values | |
| 348 | + * | |
| 349 | + * @return array | |
| 350 | + */ | |
| 351 | +function gamipress_build_shortcode_atts( $shortcode, $values ) { | |
| 352 | + | |
| 353 | + $atts = array(); | |
| 354 | + | |
| 355 | + // Bail if shortcode is not registered | |
| 356 | + if( ! isset( GamiPress()->shortcodes[$shortcode] ) ) return $atts; | |
| 357 | + | |
| 358 | + // Loop all shortcode fields to pass their value | |
| 359 | + foreach( GamiPress()->shortcodes[$shortcode]->fields as $field_id => $field ) { | |
| 360 | + | |
| 361 | + // If attribute exists on array of values given then process it | |
| 362 | + if( isset( $values[$field_id] ) ) { | |
| 363 | + $value = $values[$field_id]; | |
| 364 | + | |
| 365 | + // If is a checkbox field, then turn value into yes or no | |
| 366 | + if( $field['type'] === 'checkbox' ) { | |
| 367 | + | |
| 368 | + if( is_bool( $values[$field_id] ) ) { | |
| 369 | + $value = ( $values[$field_id] === true ? 'yes' : 'no' ); | |
| 370 | + } else { | |
| 371 | + $value = ( $values[$field_id] === 'on' ? 'yes' : 'no' ); | |
| 372 | + } | |
| 373 | + | |
| 374 | + } | |
| 375 | + | |
| 376 | + // If value is an array, setup a comma separated list of it's values | |
| 377 | + if( is_array( $value ) ) { | |
| 378 | + $value = implode( ',', $value ); | |
| 379 | + } | |
| 380 | + | |
| 381 | + $atts[$field_id] = $value; | |
| 382 | + } | |
| 383 | + } | |
| 384 | + | |
| 385 | + return $atts; | |
| 386 | + | |
| 387 | + | |
| 388 | +} | |