| @@ -1,0 +1,358 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * GamiPress Points Shortcode | |
| 4 | + * | |
| 5 | + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Points | |
| 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 | +/** | |
| 13 | + * Register [gamipress_points] shortcode | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_register_points_shortcode() { | |
| 18 | + | |
| 19 | + gamipress_register_shortcode( 'gamipress_points', array( | |
| 20 | + 'name' => __( 'Points (Old)', 'gamipress' ), | |
| 21 | + 'description' => __( 'Display a user or site points balance.', 'gamipress' ), | |
| 22 | + 'icon' => 'star-filled', | |
| 23 | + 'group' => 'gamipress', | |
| 24 | + 'output_callback' => 'gamipress_points_shortcode', | |
| 25 | + 'fields' => array( | |
| 26 | + 'type' => array( | |
| 27 | + 'name' => __( 'Points Type(s)', 'gamipress' ), | |
| 28 | + 'tooltip' => __( 'Points type(s) to display.', 'gamipress' ), | |
| 29 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 30 | + 'shortcode_desc' => __( 'Single or comma-separated list of points type(s) to display.', 'gamipress' ), | |
| 31 | + 'type' => 'advanced_select', | |
| 32 | + 'multiple' => true, | |
| 33 | + 'classes' => 'gamipress-selector', | |
| 34 | + 'attributes' => array( | |
| 35 | + 'data-placeholder' => __( 'Default: All', 'gamipress' ), | |
| 36 | + ), | |
| 37 | + 'options_cb' => 'gamipress_options_cb_points_types', | |
| 38 | + 'default' => 'all', | |
| 39 | + ), | |
| 40 | + 'thumbnail' => array( | |
| 41 | + 'name' => __( 'Show Thumbnail', 'gamipress' ), | |
| 42 | + 'tooltip' => __( 'Display the points type featured image.', 'gamipress' ), | |
| 43 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 44 | + 'type' => 'checkbox', | |
| 45 | + 'classes' => 'gamipress-switch', | |
| 46 | + 'default' => 'yes' | |
| 47 | + ), | |
| 48 | + 'thumbnail_size' => array( | |
| 49 | + 'name' => __( 'Thumbnail Size (in pixels)', 'gamipress' ), | |
| 50 | + 'tooltip' => __( 'The points type featured image size in pixels. Leave empty to use the image size from settings.', 'gamipress' ), | |
| 51 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 52 | + 'type' => 'text', | |
| 53 | + 'attributes' => array( | |
| 54 | + 'type' => 'number', | |
| 55 | + ) | |
| 56 | + ), | |
| 57 | + 'label' => array( | |
| 58 | + 'name' => __( 'Show Points Type Label', 'gamipress' ), | |
| 59 | + 'tooltip' => __( 'Display the points type label (singular or plural name, based on the amount of points).', 'gamipress' ), | |
| 60 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 61 | + 'type' => 'checkbox', | |
| 62 | + 'classes' => 'gamipress-switch', | |
| 63 | + 'default' => 'yes' | |
| 64 | + ), | |
| 65 | + 'current_user' => array( | |
| 66 | + 'name' => __( 'Current User', 'gamipress' ), | |
| 67 | + 'tooltip' => __( 'Show only points earned by the current logged in user.', 'gamipress' ), | |
| 68 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 69 | + 'type' => 'checkbox', | |
| 70 | + 'classes' => 'gamipress-switch', | |
| 71 | + ), | |
| 72 | + 'user_id' => array( | |
| 73 | + 'name' => __( 'User', 'gamipress' ), | |
| 74 | + 'tooltip' => __( 'Show only points earned by a specific user. Leave blank to show the site points (points sum of all users).', 'gamipress' ), | |
| 75 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 76 | + 'type' => 'select', | |
| 77 | + 'default' => '', | |
| 78 | + 'options_cb' => 'gamipress_options_cb_users' | |
| 79 | + ), | |
| 80 | + 'period' => array( | |
| 81 | + 'name' => __( 'Period', 'gamipress' ), | |
| 82 | + 'tooltip' => __( 'Filter points balance based on a specific period selected. By default "None" that will display user or site current points balance.', 'gamipress' ), | |
| 83 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 84 | + 'type' => 'select', | |
| 85 | + 'options_cb' => 'gamipress_get_time_periods', | |
| 86 | + ), | |
| 87 | + 'period_start' => array( | |
| 88 | + 'name' => __( 'Start Date', 'gamipress' ), | |
| 89 | + 'tooltip' => __( 'Period start date. Leave blank to no filter by a start date (points balance will be filtered only to the end date).', 'gamipress' ) | |
| 90 | + . '<br>' . __( 'Accepts any valid PHP date format.', 'gamipress' ) . ' (<a href="https://gamipress.com/docs/advanced/date-fields" target="_blank">' . __( 'More information', 'gamipress' ) . '</a>)', | |
| 91 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 92 | + 'type' => 'text', | |
| 93 | + ), | |
| 94 | + 'period_end' => array( | |
| 95 | + 'name' => __( 'End Date', 'gamipress' ), | |
| 96 | + 'tooltip' => __( 'Period end date. Leave blank to no filter by an end date (points balance will be filtered from the start date to today).', 'gamipress' ) | |
| 97 | + . '<br>' . __( 'Accepts any valid PHP date format.', 'gamipress' ) . ' (<a href="https://gamipress.com/docs/advanced/date-fields" target="_blank">' . __( 'More information', 'gamipress' ) . '</a>)', | |
| 98 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 99 | + 'type' => 'text', | |
| 100 | + ), | |
| 101 | + 'inline' => array( | |
| 102 | + 'name' => __( 'Inline', 'gamipress' ), | |
| 103 | + 'tooltip' => __( 'Show points balance inline (as text).', 'gamipress' ), | |
| 104 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 105 | + 'type' => 'checkbox', | |
| 106 | + 'classes' => 'gamipress-switch', | |
| 107 | + ), | |
| 108 | + 'columns' => array( | |
| 109 | + 'name' => __( 'Columns', 'gamipress' ), | |
| 110 | + 'tooltip' => __( 'Columns to divide each points balance.', 'gamipress' ), | |
| 111 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 112 | + 'type' => 'select', | |
| 113 | + 'options' => array( | |
| 114 | + '1' => __( '1 Column', 'gamipress' ), | |
| 115 | + '2' => __( '2 Columns', 'gamipress' ), | |
| 116 | + '3' => __( '3 Columns', 'gamipress' ), | |
| 117 | + '4' => __( '4 Columns', 'gamipress' ), | |
| 118 | + '5' => __( '5 Columns', 'gamipress' ), | |
| 119 | + '6' => __( '6 Columns', 'gamipress' ), | |
| 120 | + ), | |
| 121 | + 'default' => '1' | |
| 122 | + ), | |
| 123 | + 'columns_small' => array( | |
| 124 | + 'name' => __( 'Columns in small screens', 'gamipress' ), | |
| 125 | + 'tooltip' => __( 'Columns to divide each points balance in small screens.', 'gamipress' ), | |
| 126 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 127 | + 'type' => 'select', | |
| 128 | + 'options' => array( | |
| 129 | + '1' => __( '1 Column', 'gamipress' ), | |
| 130 | + '2' => __( '2 Columns', 'gamipress' ), | |
| 131 | + '3' => __( '3 Columns', 'gamipress' ), | |
| 132 | + '4' => __( '4 Columns', 'gamipress' ), | |
| 133 | + '5' => __( '5 Columns', 'gamipress' ), | |
| 134 | + '6' => __( '6 Columns', 'gamipress' ), | |
| 135 | + ), | |
| 136 | + 'default' => '1' | |
| 137 | + ), | |
| 138 | + 'layout' => array( | |
| 139 | + 'name' => __( 'Layout', 'gamipress' ), | |
| 140 | + 'tooltip' => __( 'Layout to show the points.', 'gamipress' ), | |
| 141 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 142 | + 'type' => 'radio', | |
| 143 | + 'options' => gamipress_get_layout_options(), | |
| 144 | + 'default' => 'left', | |
| 145 | + 'inline' => true, | |
| 146 | + 'classes' => 'gamipress-image-options' | |
| 147 | + ), | |
| 148 | + 'align' => array( | |
| 149 | + 'name' => __( 'Alignment', 'gamipress' ), | |
| 150 | + 'tooltip' => __( 'Alignment to show the points.', 'gamipress' ), | |
| 151 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 152 | + 'type' => 'radio', | |
| 153 | + 'options' => gamipress_get_alignment_options(), | |
| 154 | + 'default' => 'none', | |
| 155 | + 'inline' => true, | |
| 156 | + 'classes' => 'gamipress-image-options' | |
| 157 | + ), | |
| 158 | + 'wpms' => array( | |
| 159 | + 'name' => __( 'Include Multisite Points', 'gamipress' ), | |
| 160 | + 'tooltip' => __( 'Show points from all network sites.', 'gamipress' ), | |
| 161 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 162 | + 'type' => 'checkbox', | |
| 163 | + 'classes' => 'gamipress-switch', | |
| 164 | + ), | |
| 165 | + ), | |
| 166 | + ) ); | |
| 167 | + | |
| 168 | +} | |
| 169 | +add_action( 'init', 'gamipress_register_points_shortcode' ); | |
| 170 | + | |
| 171 | +/** | |
| 172 | + * Points Shortcode. | |
| 173 | + * | |
| 174 | + * @since 1.0.0 | |
| 175 | + * | |
| 176 | + * @param array $atts Shortcode attributes | |
| 177 | + * @param string $content Shortcode content | |
| 178 | + * | |
| 179 | + * @return string HTML markup | |
| 180 | + */ | |
| 181 | +function gamipress_points_shortcode( $atts = array(), $content = '' ) { | |
| 182 | + | |
| 183 | + global $gamipress_template_args; | |
| 184 | + | |
| 185 | + // Initialize GamiPress template args global | |
| 186 | + $gamipress_template_args = array(); | |
| 187 | + | |
| 188 | + $shortcode = 'gamipress_points'; | |
| 189 | + | |
| 190 | + $atts = shortcode_atts( array( | |
| 191 | + // Points atts | |
| 192 | + 'type' => 'all', | |
| 193 | + 'thumbnail' => 'yes', | |
| 194 | + 'thumbnail_size' => '', | |
| 195 | + 'label' => 'yes', | |
| 196 | + 'current_user' => 'no', | |
| 197 | + 'user_id' => '0', | |
| 198 | + 'period' => '', | |
| 199 | + 'period_start' => '', | |
| 200 | + 'period_end' => '', | |
| 201 | + 'inline' => 'no', | |
| 202 | + 'columns' => '1', | |
| 203 | + 'columns_small' => '1', | |
| 204 | + 'layout' => 'left', | |
| 205 | + 'align' => 'none', | |
| 206 | + 'wpms' => 'no', | |
| 207 | + ), $atts, $shortcode ); | |
| 208 | + | |
| 209 | + // Single type check to use dynamic template | |
| 210 | + $is_single_type = false; | |
| 211 | + $types = explode( ',', $atts['type'] ); | |
| 212 | + | |
| 213 | + if ( empty( $atts['type'] ) || $atts['type'] === 'all' || in_array( 'all', $types ) ) { | |
| 214 | + $types = gamipress_get_points_types_slugs(); | |
| 215 | + } else if ( count( $types ) === 1 ) { | |
| 216 | + $is_single_type = true; | |
| 217 | + } | |
| 218 | + | |
| 219 | + // Date range (based on period given) | |
| 220 | + if( $atts['period'] === 'custom' ) { | |
| 221 | + | |
| 222 | + $date_range = array( | |
| 223 | + 'start' => gamipress_date( 'Y-m-d', $atts['period_start'] ), | |
| 224 | + 'end' => gamipress_date( 'Y-m-d', $atts['period_end'] ), | |
| 225 | + ); | |
| 226 | + | |
| 227 | + } else { | |
| 228 | + $date_range = gamipress_get_period_range( $atts['period'] ); | |
| 229 | + } | |
| 230 | + | |
| 231 | + // --------------------------- | |
| 232 | + // Shortcode Errors | |
| 233 | + // --------------------------- | |
| 234 | + | |
| 235 | + if( $is_single_type ) { | |
| 236 | + | |
| 237 | + // Check if points type is valid | |
| 238 | + if ( ! in_array( $atts['type'], gamipress_get_points_types_slugs() ) ) { | |
| 239 | + return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered points type.', 'gamipress' ), $shortcode ); | |
| 240 | + } | |
| 241 | + | |
| 242 | + } else if ( $atts['type'] !== 'all' ) { | |
| 243 | + | |
| 244 | + // Let's check if all types provided are wrong | |
| 245 | + $all_types_wrong = true; | |
| 246 | + | |
| 247 | + foreach ( $types as $type ) { | |
| 248 | + if ( in_array( $type, gamipress_get_points_types_slugs() ) ) { | |
| 249 | + $all_types_wrong = false; | |
| 250 | + } | |
| 251 | + } | |
| 252 | + | |
| 253 | + // just notify error if all types are wrong | |
| 254 | + if ( $all_types_wrong ) { | |
| 255 | + return gamipress_shortcode_error( __( 'All types provided aren\'t valid registered points types.', 'gamipress' ), $shortcode ); | |
| 256 | + } | |
| 257 | + | |
| 258 | + } | |
| 259 | + | |
| 260 | + // --------------------------- | |
| 261 | + // Shortcode Processing | |
| 262 | + // --------------------------- | |
| 263 | + | |
| 264 | + // Enqueue assets | |
| 265 | + gamipress_enqueue_scripts(); | |
| 266 | + | |
| 267 | + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails | |
| 268 | + $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); | |
| 269 | + | |
| 270 | + // Force to set current user as user ID | |
| 271 | + if( $atts['current_user'] === 'yes' ) { | |
| 272 | + $atts['user_id'] = get_current_user_id(); | |
| 273 | + } | |
| 274 | + | |
| 275 | + if( $atts['wpms'] === 'yes' && ! gamipress_is_network_wide_active() ) { | |
| 276 | + // If we're polling all sites, grab an array of site IDs | |
| 277 | + $sites = gamipress_get_network_site_ids(); | |
| 278 | + } else { | |
| 279 | + // Otherwise, use only the current site | |
| 280 | + $sites = array( $blog_id ); | |
| 281 | + } | |
| 282 | + | |
| 283 | + // GamiPress template args global | |
| 284 | + $gamipress_template_args = $atts; | |
| 285 | + | |
| 286 | + // Get the points count of all registered network sites | |
| 287 | + $gamipress_template_args['points'] = array(); | |
| 288 | + | |
| 289 | + // Loop through each site (default is current site only) | |
| 290 | + foreach( $sites as $site_blog_id ) { | |
| 291 | + | |
| 292 | + // If we're not polling the current site, switch to the site we're polling | |
| 293 | + $current_site_blog_id = get_current_blog_id(); | |
| 294 | + | |
| 295 | + if ( $current_site_blog_id != $site_blog_id ) { | |
| 296 | + switch_to_blog( $site_blog_id ); | |
| 297 | + } | |
| 298 | + | |
| 299 | + foreach( $types as $points_type ) { | |
| 300 | + | |
| 301 | + // Initialize points type var | |
| 302 | + if( ! isset( $gamipress_template_args['points'][$points_type] ) ) { | |
| 303 | + $gamipress_template_args['points'][$points_type] = 0; | |
| 304 | + } | |
| 305 | + | |
| 306 | + // Setup extra arguments to pass to the get points functions | |
| 307 | + $args = array( | |
| 308 | + 'date_query' => array( | |
| 309 | + 'after' => $date_range['start'], // After start date | |
| 310 | + 'before' => $date_range['end'], // Before end date | |
| 311 | + ) | |
| 312 | + ); | |
| 313 | + | |
| 314 | + if( $atts['current_user'] === 'no' && absint( $atts['user_id'] ) === 0 ) { | |
| 315 | + // Site points | |
| 316 | + $points = gamipress_get_site_points( $points_type, $args ); | |
| 317 | + } else { | |
| 318 | + // User points | |
| 319 | + $points = gamipress_get_user_points( $atts['user_id'], $points_type, $args ); | |
| 320 | + } | |
| 321 | + | |
| 322 | + $gamipress_template_args['points'][$points_type] += $points; | |
| 323 | + | |
| 324 | + } | |
| 325 | + | |
| 326 | + if ( $current_site_blog_id != $site_blog_id && is_multisite() ) { | |
| 327 | + // Come back to current blog | |
| 328 | + restore_current_blog(); | |
| 329 | + } | |
| 330 | + | |
| 331 | + } | |
| 332 | + | |
| 333 | + // Template rendering | |
| 334 | + ob_start(); | |
| 335 | + gamipress_get_template_part( ( $atts['inline'] === 'yes' ? 'inline-points' : 'points' ), ( $is_single_type ? $atts['type'] : null ) ); | |
| 336 | + $output = ob_get_clean(); | |
| 337 | + | |
| 338 | + if( $atts['inline'] === 'yes' ) { | |
| 339 | + $output = gamipress_parse_inline_output( $output ); | |
| 340 | + } | |
| 341 | + | |
| 342 | + // If switched to blog, return back to que current blog | |
| 343 | + if( $blog_id !== get_current_blog_id() && is_multisite() ) { | |
| 344 | + restore_current_blog(); | |
| 345 | + } | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Filter to override shortcode output | |
| 349 | + * | |
| 350 | + * @since 1.6.5 | |
| 351 | + * | |
| 352 | + * @param string $output Final output | |
| 353 | + * @param array $atts Shortcode attributes | |
| 354 | + * @param string $content Shortcode content | |
| 355 | + */ | |
| 356 | + return apply_filters( 'gamipress_points_shortcode_output', $output, $atts, $content ); | |
| 357 | + | |
| 358 | +} | |