| @@ -1,0 +1,755 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Admin Add-ons Pages | |
| 4 | + * | |
| 5 | + * @package GamiPress\Admin\Add_ons | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.1.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Add-ons page | |
| 14 | + * | |
| 15 | + * @since 1.1.0 | |
| 16 | + * | |
| 17 | + * @return void | |
| 18 | + */ | |
| 19 | +function gamipress_add_ons_page() { | |
| 20 | + | |
| 21 | + if( ! function_exists( 'plugins_api' ) ) { | |
| 22 | + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 23 | + } | |
| 24 | + | |
| 25 | + wp_enqueue_script( 'plugin-install' ); | |
| 26 | + add_thickbox(); | |
| 27 | + wp_enqueue_script( 'updates' ); | |
| 28 | + | |
| 29 | + ?> | |
| 30 | + <div class="wrap"> | |
| 31 | + <div id="icon-options-general" class="icon32"></div> | |
| 32 | + <h1 class="wp-heading-inline"><?php _e( 'GamiPress Add-ons', 'gamipress' ); ?></h1> | |
| 33 | + <hr class="wp-header-end"> | |
| 34 | + | |
| 35 | + <div class="wp-filter"> | |
| 36 | + <ul class="filter-links"> | |
| 37 | + <li class="plugins-premium"><a href="#" data-target="gamipress-premium-add-on" class="current"><?php echo gamipress_dashicon( 'gamipress', 'span' ); ?> <?php _e( 'Premium', 'gamipress' ); ?></a></li> | |
| 38 | + <li class="plugins-assets"><a href="#" data-target="gamipress-asset-add-on"><?php echo gamipress_dashicon( 'awards', 'span' ); ?> <?php _e( 'Assets', 'gamipress' ); ?></a></li> | |
| 39 | + <li class="plugins-free"><a href="#" data-target="gamipress-free-add-on"><?php echo gamipress_dashicon( 'heart', 'span' ); ?> <?php _e( 'Free', 'gamipress' ); ?></a></li> | |
| 40 | + <li class="plugins-integrations"><a href="#" data-target="gamipress-integration-add-on"><?php echo gamipress_dashicon( 'admin-plugins', 'span' ); ?> <?php _e( 'Integrations', 'gamipress' ); ?></a></li> | |
| 41 | + <li class="plugins-third-party"><a href="#" data-target="gamipress-third-party-add-on"><?php echo gamipress_dashicon( 'star-filled', 'span' ); ?> <?php _e( '3rd Party', 'gamipress' ); ?></a></li> | |
| 42 | + <li class="plugins-tools"><a href="#" data-target="gamipress-tool-add-on"><?php echo gamipress_dashicon( 'admin-tools', 'span' ); ?> <?php _e( 'Tools', 'gamipress' ); ?></a></li> | |
| 43 | + </ul> | |
| 44 | + </div> | |
| 45 | + | |
| 46 | + <p><?php _e( 'Add-ons to extend and expand the functionality of GamiPress.', 'gamipress' ); ?></p> | |
| 47 | + | |
| 48 | + <form id="plugin-filter" method="post"> | |
| 49 | + <div class="wp-list-table widefat gamipress-add-ons"> | |
| 50 | + | |
| 51 | + <?php | |
| 52 | + | |
| 53 | + $plugins = gamipress_plugins_api(); | |
| 54 | + | |
| 55 | + if ( is_wp_error( $plugins ) ) { | |
| 56 | + echo $plugins->get_error_message(); | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + foreach ( $plugins as $plugin ) { | |
| 61 | + | |
| 62 | + // Skip if is an asset or pass | |
| 63 | + if( gamipress_is_plugin_pass( $plugin ) ) | |
| 64 | + continue; | |
| 65 | + | |
| 66 | + gamipress_render_plugin_card( $plugin ); | |
| 67 | + | |
| 68 | + } | |
| 69 | + | |
| 70 | + ?> | |
| 71 | + </div> | |
| 72 | + </form> | |
| 73 | + </div> | |
| 74 | + <?php | |
| 75 | +} | |
| 76 | + | |
| 77 | +/** | |
| 78 | + * Return an array of all installed plugins slugs | |
| 79 | + * | |
| 80 | + * @since 1.1.0 | |
| 81 | + * | |
| 82 | + * @return array | |
| 83 | + */ | |
| 84 | +function gamipress_get_installed_plugins_slugs() { | |
| 85 | + | |
| 86 | + $slugs = array(); | |
| 87 | + | |
| 88 | + $plugin_info = get_site_transient( 'update_plugins' ); | |
| 89 | + | |
| 90 | + if ( isset( $plugin_info->no_update ) ) { | |
| 91 | + foreach ( $plugin_info->no_update as $plugin ) { | |
| 92 | + $slugs[] = $plugin->slug; | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + if ( isset( $plugin_info->response ) ) { | |
| 97 | + foreach ( $plugin_info->response as $plugin ) { | |
| 98 | + $slugs[] = $plugin->slug; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + return $slugs; | |
| 103 | + | |
| 104 | +} | |
| 105 | + | |
| 106 | +/** | |
| 107 | + * Helper function to render a plugin card from the add-ons page | |
| 108 | + * | |
| 109 | + * @since 1.1.0 | |
| 110 | + * | |
| 111 | + * @param stdClass $plugin | |
| 112 | + * | |
| 113 | + * @return void | |
| 114 | + */ | |
| 115 | +function gamipress_render_plugin_card( $plugin ) { | |
| 116 | + | |
| 117 | + // Plugin title | |
| 118 | + $name = $plugin->info->title; | |
| 119 | + | |
| 120 | + // Plugin slug | |
| 121 | + $slug = $plugin->wp_info ? $plugin->wp_info->slug : $plugin->info->slug; | |
| 122 | + | |
| 123 | + // Available actions for this plugin | |
| 124 | + $action_links = array(); | |
| 125 | + | |
| 126 | + //$details_link = esc_url( network_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $slug . '&TB_iframe=true&width=600&height=550' ) ); | |
| 127 | + $details_link = esc_url( 'https://gamipress.com/add-ons/' . $plugin->info->slug ); | |
| 128 | + | |
| 129 | + if( gamipress_plugin_has_category( $plugin, '3rd-party' ) ) { | |
| 130 | + | |
| 131 | + $class = 'gamipress-third-party-add-on'; | |
| 132 | + | |
| 133 | + // "More Information" action | |
| 134 | + $action_links[] = '<a href="https://gamipress.com/add-ons/' . $plugin->info->slug . '" class="button button-primary" target="_blank">' . __( 'More Information', 'gamipress' ) . '</a>'; | |
| 135 | + | |
| 136 | + } else if( gamipress_plugin_has_category( $plugin, 'integrations' ) ) { | |
| 137 | + $class = 'gamipress-integration-add-on'; | |
| 138 | + | |
| 139 | + // "More Information" action | |
| 140 | + $action_links[] = '<a href="https://gamipress.com/add-ons/' . $plugin->info->slug . '" class="button button-primary" target="_blank">' . __( 'More Information', 'gamipress' ) . '</a>'; | |
| 141 | + } else if( $plugin->wp_info ) { | |
| 142 | + // Free add-ons | |
| 143 | + | |
| 144 | + if( gamipress_plugin_has_category( $plugin, 'integrations' ) ) { | |
| 145 | + $class = 'gamipress-integration-add-on'; | |
| 146 | + } else if( gamipress_plugin_has_category( $plugin, 'tools' ) ) { | |
| 147 | + $class = 'gamipress-tool-add-on'; | |
| 148 | + } else { | |
| 149 | + $class = 'gamipress-free-add-on'; | |
| 150 | + } | |
| 151 | + | |
| 152 | + // Check plugin status | |
| 153 | + if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) { | |
| 154 | + $status = install_plugin_install_status( $plugin->wp_info ); | |
| 155 | + | |
| 156 | + switch ( $status['status'] ) { | |
| 157 | + case 'install': | |
| 158 | + if ( $status['url'] ) { | |
| 159 | + $action_links[] = '<a class="install-now button button-primary" data-slug="' . esc_attr( $slug ) . '" href="' . esc_url( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Install Now' ) . '</a>'; | |
| 160 | + } | |
| 161 | + break; | |
| 162 | + | |
| 163 | + case 'update_available': | |
| 164 | + if ( $status['url'] ) { | |
| 165 | + $action_links[] = '<a class="update-now button button-primary aria-button-if-js" data-plugin="' . esc_attr( $status['file'] ) . '" data-slug="' . esc_attr( $slug ) . '" href="' . esc_url( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Update Now' ) . '</a>'; | |
| 166 | + } | |
| 167 | + break; | |
| 168 | + | |
| 169 | + case 'latest_installed': | |
| 170 | + case 'newer_installed': | |
| 171 | + if ( is_plugin_active( $status['file'] ) ) { | |
| 172 | + $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Active', 'plugin' ) . '</button>'; | |
| 173 | + } elseif ( current_user_can( 'activate_plugins' ) ) { | |
| 174 | + $button_text = __( 'Activate' ); | |
| 175 | + $button_label = _x( 'Activate %s', 'plugin' ); | |
| 176 | + $activate_url = add_query_arg( array( | |
| 177 | + '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ), | |
| 178 | + 'action' => 'activate', | |
| 179 | + 'plugin' => $status['file'], | |
| 180 | + ), network_admin_url( 'plugins.php' ) ); | |
| 181 | + | |
| 182 | + if ( is_network_admin() ) { | |
| 183 | + $button_text = __( 'Network Activate' ); | |
| 184 | + $button_label = _x( 'Network Activate %s', 'plugin' ); | |
| 185 | + $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + $action_links[] = sprintf( | |
| 189 | + '<a href="%1$s" class="button button-primary activate-now" aria-label="%2$s">%3$s</a>', | |
| 190 | + esc_url( $activate_url ), | |
| 191 | + esc_attr( sprintf( $button_label, $name ) ), | |
| 192 | + $button_text | |
| 193 | + ); | |
| 194 | + } else { | |
| 195 | + $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Installed', 'plugin' ) . '</button>'; | |
| 196 | + } | |
| 197 | + break; | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } else { | |
| 201 | + // Premium add-ons | |
| 202 | + | |
| 203 | + $class = 'gamipress-premium-add-on'; | |
| 204 | + | |
| 205 | + $plugin_file = $slug . '/' . $slug . '.php'; | |
| 206 | + | |
| 207 | + // If is installed | |
| 208 | + if ( is_dir( WP_PLUGIN_DIR . '/' . $slug ) ) { | |
| 209 | + | |
| 210 | + // If is active | |
| 211 | + if ( is_plugin_active( $slug . '/' . $slug . '.php' ) ) { | |
| 212 | + | |
| 213 | + // If has licensing enabled | |
| 214 | + if( $plugin->licensing->enabled ) { | |
| 215 | + | |
| 216 | + // Plugin installed and active, so field should be registered | |
| 217 | + $field = cmb2_get_field( $slug . '-license', str_replace( '-', '_', $slug ) . '_license', 'gamipress_settings', 'options-page' ); | |
| 218 | + | |
| 219 | + if( $field ) { | |
| 220 | + $license_key = $field->escaped_value(); | |
| 221 | + | |
| 222 | + $license = rgc_cmb2_edd_license_data( $license_key ); | |
| 223 | + $license_status = ( $license !== false ) ? $license->license : false; | |
| 224 | + | |
| 225 | + if( $license_status !== 'valid' ) { | |
| 226 | + // "Activate License" action | |
| 227 | + $action_links[] = '<a href="' . admin_url( 'admin.php?page=gamipress_licenses' ) . '" class="button">' . __( 'Activate License', 'gamipress' ) . '</a>'; | |
| 228 | + } else { | |
| 229 | + // "Active and License Registered" action | |
| 230 | + $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . __( 'Active and License Registered', 'gamipress' ) . '</button>'; | |
| 231 | + } | |
| 232 | + } | |
| 233 | + | |
| 234 | + } else { | |
| 235 | + // "Active" action | |
| 236 | + $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . __( 'Active', 'gamipress' ) . '</button>'; | |
| 237 | + } | |
| 238 | + | |
| 239 | + } else if ( current_user_can( 'activate_plugins' ) ) { | |
| 240 | + // If not active and current user can activate plugins, then add the "Activate" action | |
| 241 | + | |
| 242 | + $button_text = __( 'Activate' ); | |
| 243 | + $button_label = _x( 'Activate %s', 'plugin' ); | |
| 244 | + $activate_url = add_query_arg( array( | |
| 245 | + '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_file ), | |
| 246 | + 'action' => 'activate', | |
| 247 | + 'plugin' => $plugin_file, | |
| 248 | + ), network_admin_url( 'plugins.php' ) ); | |
| 249 | + | |
| 250 | + if ( is_network_admin() ) { | |
| 251 | + $button_text = __( 'Network Activate' ); | |
| 252 | + $button_label = _x( 'Network Activate %s', 'plugin' ); | |
| 253 | + $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url ); | |
| 254 | + } | |
| 255 | + | |
| 256 | + // "Activate" action | |
| 257 | + $action_links[] = sprintf( | |
| 258 | + '<a href="%1$s" class="button button-primary activate-now" aria-label="%2$s">%3$s</a>', | |
| 259 | + esc_url( $activate_url ), | |
| 260 | + esc_attr( sprintf( $button_label, $name ) ), | |
| 261 | + $button_text | |
| 262 | + ); | |
| 263 | + } | |
| 264 | + } else if( gamipress_is_plugin_pass( $plugin ) ) { | |
| 265 | + | |
| 266 | + // "Get this pass" action | |
| 267 | + $action_links[] = '<a href="https://gamipress.com/add-ons/' . $plugin->info->slug . '" class="button button-primary" target="_blank">' . __( 'Get this pass', 'gamipress' ) . '</a>'; | |
| 268 | + | |
| 269 | + } else if( gamipress_is_plugin_asset( $plugin ) ) { | |
| 270 | + | |
| 271 | + $class = 'gamipress-asset-add-on'; | |
| 272 | + | |
| 273 | + // "Get this asset" action | |
| 274 | + $action_links[] = '<a href="https://gamipress.com/add-ons/' . $plugin->info->slug . '" class="button button-primary" target="_blank">' . __( 'Get this asset', 'gamipress' ) . '</a>'; | |
| 275 | + | |
| 276 | + } else { | |
| 277 | + | |
| 278 | + // "Get this add-on" action | |
| 279 | + $action_links[] = '<a href="https://gamipress.com/add-ons/' . $plugin->info->slug . '" class="button button-primary" target="_blank">' . __( 'Get this add-on', 'gamipress' ) . '</a>'; | |
| 280 | + | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Plugin card actions | |
| 286 | + * | |
| 287 | + * @since 1.0.0 | |
| 288 | + * | |
| 289 | + * @param array $actions | |
| 290 | + * @param string $slug | |
| 291 | + * | |
| 292 | + * @return array | |
| 293 | + */ | |
| 294 | + $action_links = apply_filters( 'gamipress_plugin_card_actions', $action_links, $slug ); | |
| 295 | + | |
| 296 | + // Status | |
| 297 | + $status = ''; | |
| 298 | + | |
| 299 | + if( is_plugin_active( $slug . '/' . $slug . '.php' ) ) { | |
| 300 | + $status = 'active'; | |
| 301 | + } else if( is_dir( WP_PLUGIN_DIR . '/' . $slug ) ) { | |
| 302 | + $status = 'installed'; | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 306 | + * Plugin card status | |
| 307 | + * | |
| 308 | + * @since 1.0.0 | |
| 309 | + * | |
| 310 | + * @param string $status | |
| 311 | + * @param string $slug | |
| 312 | + * | |
| 313 | + * @return string | |
| 314 | + */ | |
| 315 | + $status = apply_filters( 'gamipress_plugin_card_status', $status, $slug ); | |
| 316 | + $status_label = ''; | |
| 317 | + | |
| 318 | + switch( $status ) { | |
| 319 | + case 'active': $status_label = __( 'Active', 'gamipress' ); break; | |
| 320 | + case 'inactive': $status_label = __( 'Inactive', 'gamipress' ); break; | |
| 321 | + case 'installed': $status_label = __( 'Installed', 'gamipress' ); break; | |
| 322 | + } | |
| 323 | + | |
| 324 | + ?> | |
| 325 | + | |
| 326 | + <div class="gamipress-plugin-card plugin-card plugin-card-<?php echo sanitize_html_class( $slug ); ?> <?php echo $class; ?> gamipress-plugin-card-status-<?php echo sanitize_html_class( $status ); ?>"> | |
| 327 | + | |
| 328 | + <div class="plugin-card-top cmb-tooltip"> | |
| 329 | + | |
| 330 | + <?php | |
| 331 | + | |
| 332 | + ?> | |
| 333 | + | |
| 334 | + <?php if( $status !== '' ) : ?> | |
| 335 | + <div class="<?php echo esc_attr( $status ); ?> column-status"> | |
| 336 | + <span><?php echo esc_html( $status_label ) ?></span> | |
| 337 | + </div> | |
| 338 | + <?php endif; ?> | |
| 339 | + | |
| 340 | + <div class="thumbnail column-thumbnail"> | |
| 341 | + <a href="<?php echo esc_url( $details_link ); ?>" class="open-plugin-details-modal" target="_blank"> | |
| 342 | + <img src="<?php echo esc_attr( $plugin->info->thumbnail ) ?>" class="plugin-thumbnail" alt=""> | |
| 343 | + </a> | |
| 344 | + </div> | |
| 345 | + | |
| 346 | + <div class="name column-name"> | |
| 347 | + <h3> | |
| 348 | + <a href="<?php echo esc_url( $details_link ); ?>" class="open-plugin-details-modal" target="_blank"> | |
| 349 | + <?php echo $name; ?> | |
| 350 | + </a> | |
| 351 | + </h3> | |
| 352 | + </div> | |
| 353 | + | |
| 354 | + <div class="desc column-description cmb-tooltip-desc cmb-tooltip-top"> | |
| 355 | + <p><?php echo gamipress_esc_plugin_excerpt( $plugin->info->excerpt ); ?></p> | |
| 356 | + </div> | |
| 357 | + | |
| 358 | + </div> | |
| 359 | + | |
| 360 | + <div class="plugin-card-bottom"> | |
| 361 | + <div class="action-links"> | |
| 362 | + <?php if ( $action_links ) { | |
| 363 | + echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>'; | |
| 364 | + } ?> | |
| 365 | + </div> | |
| 366 | + </div> | |
| 367 | + | |
| 368 | + </div> | |
| 369 | + | |
| 370 | + <?php | |
| 371 | +} | |
| 372 | + | |
| 373 | +/** | |
| 374 | + * Function to contact with the GamiPress plugins API | |
| 375 | + * | |
| 376 | + * @since 1.1.0 | |
| 377 | + * | |
| 378 | + * @return object|WP_Error Object with GamiPress plugins | |
| 379 | + */ | |
| 380 | +function gamipress_plugins_api() { | |
| 381 | + | |
| 382 | + $cache = gamipress_get_cache( 'gamipress_plugins_api', false, false ); | |
| 383 | + | |
| 384 | + // If result already cached, return it | |
| 385 | + if( $cache !== false ) { | |
| 386 | + return $cache; | |
| 387 | + } | |
| 388 | + | |
| 389 | + // If a plugins api request has been cached already, then use cached plugins | |
| 390 | + if ( false !== ( $res = get_transient( 'gamipress_plugins_api' ) ) ) { | |
| 391 | + return apply_filters( 'gamipress_plugins_api', $res ); | |
| 392 | + } | |
| 393 | + | |
| 394 | + $url = $http_url = 'https://gamipress.com/wp-json/api/add-ons/'; | |
| 395 | + | |
| 396 | + if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { | |
| 397 | + $url = set_url_scheme( $url, 'https' ); | |
| 398 | + } | |
| 399 | + | |
| 400 | + $http_args = array( | |
| 401 | + 'timeout' => 15, | |
| 402 | + ); | |
| 403 | + | |
| 404 | + $request = wp_remote_get( $url, $http_args ); | |
| 405 | + | |
| 406 | + if ( $ssl && is_wp_error( $request ) ) { | |
| 407 | + trigger_error( | |
| 408 | + sprintf( | |
| 409 | + __( 'An unexpected error occurred. Something may be wrong with gamipress.com or this server’s configuration. If you continue to have problems, please try to <a href="%s">contact us</a>.', 'gamipress' ), | |
| 410 | + 'https://gamipress.com/contact-us/' | |
| 411 | + ) . ' ' . __( '(WordPress could not establish a secure connection to gamipress.com. Please contact your server administrator.)' ), | |
| 412 | + headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE | |
| 413 | + ); | |
| 414 | + | |
| 415 | + $request = wp_remote_get( $http_url, $http_args ); | |
| 416 | + } | |
| 417 | + | |
| 418 | + if ( is_wp_error( $request ) ) { | |
| 419 | + $res = new WP_Error( 'gamipress_plugins_api_failed', | |
| 420 | + sprintf( | |
| 421 | + __( 'An unexpected error occurred. Something may be wrong with gamipress.com or this server’s configuration. If you continue to have problems, please try to <a href="%s">contact us</a>.', 'gamipress' ), | |
| 422 | + 'https://gamipress.com/contact-us/' | |
| 423 | + ), | |
| 424 | + $request->get_error_message() | |
| 425 | + ); | |
| 426 | + } else { | |
| 427 | + $res = json_decode( $request['body'] ); | |
| 428 | + | |
| 429 | + $res = (array) $res->products; | |
| 430 | + | |
| 431 | + // Set a transient for 1 week with api plugins | |
| 432 | + set_transient( 'gamipress_plugins_api', $res, ( 24 * 7 ) * HOUR_IN_SECONDS ); | |
| 433 | + | |
| 434 | + gamipress_set_cache( 'gamipress_plugins_api', $res ); | |
| 435 | + } | |
| 436 | + | |
| 437 | + return apply_filters( 'gamipress_plugins_api', $res ); | |
| 438 | + | |
| 439 | +} | |
| 440 | + | |
| 441 | +/** | |
| 442 | + * Function to contact with the GamiPress website API | |
| 443 | + * | |
| 444 | + * @since 1.1.4 | |
| 445 | + * | |
| 446 | + * @param string $action | |
| 447 | + * @param array $data | |
| 448 | + * | |
| 449 | + * @return object|WP_Error Object with GamiPress website API response | |
| 450 | + */ | |
| 451 | +function gamipress_api_request( $action, $data ) { | |
| 452 | + | |
| 453 | + // Slug is required to meet the plugin to work | |
| 454 | + if( ! isset( $data['slug'] ) ) { | |
| 455 | + return false; | |
| 456 | + } | |
| 457 | + | |
| 458 | + $slug = $data['slug']; | |
| 459 | + | |
| 460 | + $cache_key = "gamipress_api_request_{$action}_{$slug}"; | |
| 461 | + | |
| 462 | + // If a GamiPress api request has been cached already, then use cached response | |
| 463 | + if ( false !== ( $response = get_transient( $cache_key ) ) ) { | |
| 464 | + return $response; | |
| 465 | + } | |
| 466 | + | |
| 467 | + $api_params = array( | |
| 468 | + 'edd_action' => 'get_version', | |
| 469 | + 'license' => ! empty( $data['license'] ) ? $data['license'] : '', | |
| 470 | + 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false, | |
| 471 | + 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false, | |
| 472 | + 'version' => isset( $data['version'] ) ? $data['version'] : false, | |
| 473 | + 'slug' => $data['slug'], | |
| 474 | + 'author' => isset( $data['author'] ) ? $data['author'] : '', | |
| 475 | + 'url' => home_url(), | |
| 476 | + 'beta' => ! empty( $data['beta'] ), | |
| 477 | + ); | |
| 478 | + | |
| 479 | + $request = wp_remote_post( 'http://gamipress.com/edd-sl-api/', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) ); | |
| 480 | + | |
| 481 | + if ( is_wp_error( $request ) ) { | |
| 482 | + // Return the error | |
| 483 | + return $request; | |
| 484 | + } | |
| 485 | + | |
| 486 | + $request = json_decode( wp_remote_retrieve_body( $request ) ); | |
| 487 | + | |
| 488 | + // Unserialize sections | |
| 489 | + if ( $request && isset( $request->sections ) ) { | |
| 490 | + $request->sections = maybe_unserialize( $request->sections ); | |
| 491 | + } else { | |
| 492 | + $request = false; | |
| 493 | + } | |
| 494 | + | |
| 495 | + // Unserialize banners | |
| 496 | + if ( $request && isset( $request->banners ) ) { | |
| 497 | + $request->banners = maybe_unserialize( $request->banners ); | |
| 498 | + } | |
| 499 | + | |
| 500 | + // Turn each section into array | |
| 501 | + if( ! empty( $request->sections ) ) { | |
| 502 | + foreach( $request->sections as $key => $section ) { | |
| 503 | + $request->$key = (array) $section; | |
| 504 | + } | |
| 505 | + } | |
| 506 | + | |
| 507 | + // Set a transient of 24 hours with GamiPress api response | |
| 508 | + set_transient( $cache_key, $request, 24 * HOUR_IN_SECONDS ); | |
| 509 | + | |
| 510 | + return $request; | |
| 511 | +} | |
| 512 | + | |
| 513 | +/** | |
| 514 | + * Overrides the plugin information when plugin is not stored on WordPress.org just on GamiPress.com | |
| 515 | + * | |
| 516 | + * @since 1.1.4 | |
| 517 | + * | |
| 518 | + * @param mixed $data | |
| 519 | + * @param string $action | |
| 520 | + * @param object $args | |
| 521 | + * | |
| 522 | + * @return object $data | |
| 523 | + */ | |
| 524 | +function gamipress_override_plugin_information( $data, $action = '', $args = null ) { | |
| 525 | + | |
| 526 | + if ( $action != 'plugin_information' ) { | |
| 527 | + return $data; | |
| 528 | + } | |
| 529 | + | |
| 530 | + if ( ! isset( $args->slug ) ) { | |
| 531 | + return $data; | |
| 532 | + } | |
| 533 | + | |
| 534 | + $plugins = gamipress_plugins_api(); | |
| 535 | + | |
| 536 | + // Bail if API is not returning any plugin | |
| 537 | + if( ! is_array( $plugins ) ) { | |
| 538 | + return $data; | |
| 539 | + } | |
| 540 | + | |
| 541 | + $override_plugin = false; | |
| 542 | + | |
| 543 | + foreach( $plugins as $plugin ) { | |
| 544 | + // Just override if plugin has not assigned to a WordPress.org plugin | |
| 545 | + if ( $args->slug === $plugin->info->slug && ! $plugin->wp_info ) { | |
| 546 | + | |
| 547 | + $override_plugin = $plugin; | |
| 548 | + | |
| 549 | + // Plugin to override found, so exit loop | |
| 550 | + break; | |
| 551 | + | |
| 552 | + } | |
| 553 | + } | |
| 554 | + | |
| 555 | + // if not plugin to override, return | |
| 556 | + if ( $override_plugin === false ) { | |
| 557 | + return $data; | |
| 558 | + } | |
| 559 | + | |
| 560 | + $to_send = array( | |
| 561 | + 'slug' => $override_plugin->info->slug, | |
| 562 | + 'item_name' => $override_plugin->info->title, | |
| 563 | + 'is_ssl' => is_ssl(), | |
| 564 | + 'fields' => array( | |
| 565 | + 'banners' => array(), | |
| 566 | + 'reviews' => false | |
| 567 | + ) | |
| 568 | + ); | |
| 569 | + | |
| 570 | + $api_response = gamipress_api_request( 'plugin_information', $to_send ); | |
| 571 | + | |
| 572 | + if ( $api_response !== false) { | |
| 573 | + $data = $api_response; | |
| 574 | + } | |
| 575 | + | |
| 576 | + // Convert sections into an associative array, since we're getting an object, but Core expects an array. | |
| 577 | + if ( isset( $data->sections ) && is_array( $data->sections ) ) { | |
| 578 | + $new_sections = array(); | |
| 579 | + foreach ( $data->sections as $key => $value ) { | |
| 580 | + $new_sections[ $key ] = $value; | |
| 581 | + } | |
| 582 | + | |
| 583 | + $data->sections = $new_sections; | |
| 584 | + } | |
| 585 | + | |
| 586 | + // Convert banners into an associative array, since we're getting an object, but Core expects an array. | |
| 587 | + if ( isset( $data->banners ) && ! is_array( $data->banners ) ) { | |
| 588 | + $new_banners = array(); | |
| 589 | + foreach ( $data->banners as $key => $value ) { | |
| 590 | + $new_banners[ $key ] = $value; | |
| 591 | + } | |
| 592 | + | |
| 593 | + $data->banners = $new_banners; | |
| 594 | + } | |
| 595 | + | |
| 596 | + return $data; | |
| 597 | + | |
| 598 | +} | |
| 599 | +add_filter( 'plugins_api', 'gamipress_override_plugin_information', 10, 3 ); | |
| 600 | + | |
| 601 | +/** | |
| 602 | + * Escape plugin description | |
| 603 | + * | |
| 604 | + * @since 1.3.9.7 | |
| 605 | + * | |
| 606 | + * @param string $excerpt | |
| 607 | + * | |
| 608 | + * @return string | |
| 609 | + */ | |
| 610 | +function gamipress_esc_plugin_excerpt( $excerpt ) { | |
| 611 | + | |
| 612 | + // To prevent execute shortcodes on website, the are double capsuled on [] | |
| 613 | + $excerpt = str_replace( '[[', '[', $excerpt ); | |
| 614 | + $excerpt = str_replace( ']]', ']', $excerpt ); | |
| 615 | + | |
| 616 | + return $excerpt; | |
| 617 | + | |
| 618 | +} | |
| 619 | + | |
| 620 | +/** | |
| 621 | + * Append custom body classes to plugin information iframe | |
| 622 | + * | |
| 623 | + * @since 1.6.0 | |
| 624 | + * | |
| 625 | + * @param string $classes | |
| 626 | + * | |
| 627 | + * @return string | |
| 628 | + */ | |
| 629 | +function gamipress_plugin_information_admin_body_class( $classes ) { | |
| 630 | + | |
| 631 | + $body_id = ( isset( $GLOBALS['body_id'] ) ? $GLOBALS['body_id'] : '' ); | |
| 632 | + | |
| 633 | + if( $body_id === 'plugin-information' && isset( $_REQUEST['plugin'] ) ) { | |
| 634 | + | |
| 635 | + // Get the plugin queried | |
| 636 | + $plugin_slug = wp_unslash( $_REQUEST['plugin'] ); | |
| 637 | + | |
| 638 | + // Check if is a gamipress plugin | |
| 639 | + $plugins = gamipress_plugins_api(); | |
| 640 | + | |
| 641 | + $override_plugin = false; | |
| 642 | + | |
| 643 | + foreach( $plugins as $plugin ) { | |
| 644 | + | |
| 645 | + // Just override if is a plugin we own | |
| 646 | + if ( $plugin_slug === $plugin->info->slug ) { | |
| 647 | + | |
| 648 | + $override_plugin = $plugin; | |
| 649 | + | |
| 650 | + // Plugin to override found, so exit loop | |
| 651 | + break; | |
| 652 | + | |
| 653 | + } | |
| 654 | + } | |
| 655 | + | |
| 656 | + // if not plugin to override, return | |
| 657 | + if ( $override_plugin === false ) { | |
| 658 | + return $classes; | |
| 659 | + } | |
| 660 | + | |
| 661 | + $classes .= ' is-gamipress-plugin'; | |
| 662 | + | |
| 663 | + if( gamipress_is_plugin_asset( $override_plugin ) ) { | |
| 664 | + $classes .= ' is-gamipress-asset'; | |
| 665 | + } | |
| 666 | + } | |
| 667 | + | |
| 668 | + return $classes; | |
| 669 | +} | |
| 670 | +add_filter( 'admin_body_class', 'gamipress_plugin_information_admin_body_class' ); | |
| 671 | + | |
| 672 | +/** | |
| 673 | + * Append custom body classes to plugin information iframe | |
| 674 | + * | |
| 675 | + * @since 1.6.3 | |
| 676 | + * | |
| 677 | + * @param stdClass $plugin Plugin API object | |
| 678 | + * @param string $category Category slug | |
| 679 | + * | |
| 680 | + * @return string | |
| 681 | + */ | |
| 682 | +function gamipress_plugin_has_category( $plugin, $category ) { | |
| 683 | + | |
| 684 | + if( is_array( $plugin->info->category ) ) { | |
| 685 | + | |
| 686 | + // Loop plugin categories objects | |
| 687 | + foreach( $plugin->info->category as $plugin_cat ) { | |
| 688 | + | |
| 689 | + // Check if category slug is equal that category given | |
| 690 | + if( $plugin_cat->slug === $category ) { | |
| 691 | + return true; | |
| 692 | + } | |
| 693 | + } | |
| 694 | + | |
| 695 | + } | |
| 696 | + | |
| 697 | + return false; | |
| 698 | + | |
| 699 | +} | |
| 700 | + | |
| 701 | +/** | |
| 702 | + * Helper function to determine if give plugin has the passes category | |
| 703 | + * | |
| 704 | + * @since 1.6.9.2 | |
| 705 | + * | |
| 706 | + * @param stdClass $plugin | |
| 707 | + * | |
| 708 | + * @return bool | |
| 709 | + */ | |
| 710 | +function gamipress_is_plugin_pass( $plugin ) { | |
| 711 | + | |
| 712 | + // Check if plugin has categories | |
| 713 | + if( is_array( $plugin->info->category ) && count( $plugin->info->category ) ) { | |
| 714 | + | |
| 715 | + // Loop plugin categories | |
| 716 | + foreach( $plugin->info->category as $category ) { | |
| 717 | + | |
| 718 | + // Passes category found | |
| 719 | + if( $category->slug === 'passes' ) { | |
| 720 | + return true; | |
| 721 | + } | |
| 722 | + } | |
| 723 | + | |
| 724 | + } | |
| 725 | + | |
| 726 | + return false; | |
| 727 | +} | |
| 728 | + | |
| 729 | +/** | |
| 730 | + * Helper function to determine if give plugin has the asset tag | |
| 731 | + * | |
| 732 | + * @since 1.6.0 | |
| 733 | + * | |
| 734 | + * @param stdClass $plugin | |
| 735 | + * | |
| 736 | + * @return bool | |
| 737 | + */ | |
| 738 | +function gamipress_is_plugin_asset( $plugin ) { | |
| 739 | + | |
| 740 | + // Check if plugin has tags | |
| 741 | + if( is_array( $plugin->info->tags ) && count( $plugin->info->tags ) ) { | |
| 742 | + | |
| 743 | + // Loop plugin tags | |
| 744 | + foreach( $plugin->info->tags as $tag ) { | |
| 745 | + | |
| 746 | + // asset tag found | |
| 747 | + if( $tag->slug === 'asset' ) { | |
| 748 | + return true; | |
| 749 | + } | |
| 750 | + } | |
| 751 | + | |
| 752 | + } | |
| 753 | + | |
| 754 | + return false; | |
| 755 | +} | |