| @@ -1,0 +1,176 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * GamiPress Inline Rank Shortcode | |
| 4 | + * | |
| 5 | + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Inline_Rank | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 2.3.1 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register the [gamipress_inline_rank] shortcode | |
| 14 | + * | |
| 15 | + * @since 2.3.1 | |
| 16 | + */ | |
| 17 | +function gamipress_register_inline_rank_shortcode() { | |
| 18 | + | |
| 19 | + gamipress_register_shortcode( 'gamipress_inline_rank', array( | |
| 20 | + 'name' => __( 'Inline Rank', 'gamipress' ), | |
| 21 | + 'description' => __( 'Display a single rank inline.', 'gamipress' ), | |
| 22 | + 'icon' => 'rank', | |
| 23 | + 'group' => 'gamipress', | |
| 24 | + 'output_callback' => 'gamipress_inline_rank_shortcode', | |
| 25 | + 'fields' => array( | |
| 26 | + 'id' => array( | |
| 27 | + 'name' => __( 'Rank', 'gamipress' ), | |
| 28 | + 'tooltip' => __( 'Rank to render.', 'gamipress' ), | |
| 29 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 30 | + 'shortcode_desc' => __( 'The ID of the rank to render.', 'gamipress' ), | |
| 31 | + 'type' => 'select', | |
| 32 | + 'classes' => 'gamipress-post-selector', | |
| 33 | + 'attributes' => array( | |
| 34 | + 'data-post-type' => implode( ',', gamipress_get_rank_types_slugs() ), | |
| 35 | + 'data-placeholder' => __( 'Select a rank', 'gamipress' ), | |
| 36 | + ), | |
| 37 | + 'default' => '', | |
| 38 | + 'options_cb' => 'gamipress_options_cb_posts' | |
| 39 | + ), | |
| 40 | + 'link' => array( | |
| 41 | + 'name' => __( 'Show Link', 'gamipress' ), | |
| 42 | + 'tooltip' => __( 'Add a link on rank title to the rank page.', 'gamipress' ), | |
| 43 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 44 | + 'type' => 'checkbox', | |
| 45 | + 'classes' => 'gamipress-switch', | |
| 46 | + 'default' => 'yes' | |
| 47 | + ), | |
| 48 | + 'thumbnail' => array( | |
| 49 | + 'name' => __( 'Show Thumbnail', 'gamipress' ), | |
| 50 | + 'tooltip' => __( 'Display the rank featured image.', 'gamipress' ), | |
| 51 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 52 | + 'type' => 'checkbox', | |
| 53 | + 'classes' => 'gamipress-switch', | |
| 54 | + 'default' => 'yes' | |
| 55 | + ), | |
| 56 | + 'thumbnail_size' => array( | |
| 57 | + 'name' => __( 'Thumbnail Size (in pixels)', 'gamipress' ), | |
| 58 | + 'tooltip' => __( 'The rank featured image size in pixels. Leave empty to use the image size from settings.', 'gamipress' ), | |
| 59 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 60 | + 'type' => 'text', | |
| 61 | + 'attributes' => array( | |
| 62 | + 'type' => 'number', | |
| 63 | + ) | |
| 64 | + ), | |
| 65 | + ), | |
| 66 | + ) ); | |
| 67 | + | |
| 68 | +} | |
| 69 | +add_action( 'init', 'gamipress_register_inline_rank_shortcode' ); | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Single Rank Shortcode | |
| 73 | + * | |
| 74 | + * @since 2.3.1 | |
| 75 | + * | |
| 76 | + * @param array $atts Shortcode attributes | |
| 77 | + * @param string $content Shortcode content | |
| 78 | + * | |
| 79 | + * @return string HTML markup | |
| 80 | + */ | |
| 81 | +function gamipress_inline_rank_shortcode( $atts = array(), $content = '' ) { | |
| 82 | + | |
| 83 | + global $post, $gamipress_template_args; | |
| 84 | + | |
| 85 | + $shortcode = 'gamipress_inline_rank'; | |
| 86 | + | |
| 87 | + $original_atts = $atts; | |
| 88 | + | |
| 89 | + $atts = shortcode_atts( gamipress_inline_rank_shortcode_defaults(), $atts, $shortcode ); | |
| 90 | + | |
| 91 | + // --------------------------- | |
| 92 | + // Shortcode Errors | |
| 93 | + // --------------------------- | |
| 94 | + | |
| 95 | + // Get the rank post | |
| 96 | + $rank = gamipress_get_post( $atts['id'] ); | |
| 97 | + $is_rank = gamipress_is_rank( $rank ); | |
| 98 | + | |
| 99 | + // Return if rank id not specified | |
| 100 | + if ( empty( $original_atts['id'] ) && ! $is_rank ) | |
| 101 | + return gamipress_shortcode_error( __( 'Please, provide the rank ID.', 'gamipress' ), $shortcode ); | |
| 102 | + | |
| 103 | + // Check if we're dealing with a rank post | |
| 104 | + if ( ! $is_rank ) | |
| 105 | + return gamipress_shortcode_error( __( 'The id provided doesn\'t belong to a valid rank.', 'gamipress' ), $shortcode ); | |
| 106 | + | |
| 107 | + // --------------------------- | |
| 108 | + // Shortcode Processing | |
| 109 | + // --------------------------- | |
| 110 | + | |
| 111 | + // Enqueue assets | |
| 112 | + gamipress_enqueue_scripts(); | |
| 113 | + | |
| 114 | + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails | |
| 115 | + $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); | |
| 116 | + | |
| 117 | + // Initialize user ID to avoid undefined index errors | |
| 118 | + if( ! isset( $atts['user_id'] ) ) { | |
| 119 | + $atts['user_id'] = get_current_user_id(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + // Get the current user if none wasn't specified | |
| 123 | + if( absint( $atts['user_id'] ) === 0 ) { | |
| 124 | + $atts['user_id'] = get_current_user_id(); | |
| 125 | + } | |
| 126 | + | |
| 127 | + $gamipress_template_args = $atts; | |
| 128 | + | |
| 129 | + // Set up the post | |
| 130 | + $post = $rank; | |
| 131 | + setup_postdata( $post ); | |
| 132 | + | |
| 133 | + // Template rendering | |
| 134 | + ob_start(); | |
| 135 | + gamipress_get_template_part( 'inline-rank', $rank->post_type ); | |
| 136 | + $output = ob_get_clean(); | |
| 137 | + | |
| 138 | + $output = gamipress_parse_inline_output( $output ); | |
| 139 | + | |
| 140 | + // Reset the post set up | |
| 141 | + wp_reset_postdata(); | |
| 142 | + | |
| 143 | + // If switched to blog, return back to que current blog | |
| 144 | + if( $blog_id !== get_current_blog_id() && is_multisite() ) { | |
| 145 | + restore_current_blog(); | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * Filter to override shortcode output | |
| 150 | + * | |
| 151 | + * @since 2.3.1 | |
| 152 | + * | |
| 153 | + * @param string $output Final output | |
| 154 | + * @param array $atts Shortcode attributes | |
| 155 | + * @param string $content Shortcode content | |
| 156 | + */ | |
| 157 | + return apply_filters( 'gamipress_inline_rank_shortcode_output', $output, $atts, $content ); | |
| 158 | +} | |
| 159 | + | |
| 160 | +/** | |
| 161 | + * Single rank shortcode defaults attributes values | |
| 162 | + * | |
| 163 | + * @since 2.3.1 | |
| 164 | + * | |
| 165 | + * @return array | |
| 166 | + */ | |
| 167 | +function gamipress_inline_rank_shortcode_defaults() { | |
| 168 | + | |
| 169 | + return apply_filters( 'gamipress_inline_rank_shortcode_defaults', array( | |
| 170 | + 'id' => get_the_ID(), | |
| 171 | + 'link' => 'yes', | |
| 172 | + 'thumbnail' => 'yes', | |
| 173 | + 'thumbnail_size' => '', | |
| 174 | + ) ); | |
| 175 | + | |
| 176 | +} | |