| @@ -1,0 +1,322 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Plugin Name: GamiPress - BuddyPress integration | |
| 4 | + * Plugin URI: https://wordpress.org/plugins/gamipress-buddypress-integration/ | |
| 5 | + * Description: Connect GamiPress with BuddyPress. | |
| 6 | + * Version: 1.6.1 | |
| 7 | + * Author: GamiPress | |
| 8 | + * Author URI: https://gamipress.com/ | |
| 9 | + * Text Domain: gamipress-buddypress-integration | |
| 10 | + * Domain Path: /languages/ | |
| 11 | + * Requires at least: 4.4 | |
| 12 | + * Tested up to: 6.4 | |
| 13 | + * License: GNU AGPL v3.0 (http://www.gnu.org/licenses/agpl.txt) | |
| 14 | + * | |
| 15 | + * @package GamiPress\BuddyPress | |
| 16 | + * @author GamiPress | |
| 17 | + * @copyright Copyright (c) GamiPress | |
| 18 | + */ | |
| 19 | + | |
| 20 | +final class GamiPress_Integration_BuddyPress { | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * @var GamiPress_Integration_BuddyPress $instance The one true GamiPress_Integration_BuddyPress | |
| 24 | + * @since 1.0.0 | |
| 25 | + */ | |
| 26 | + private static $instance; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Get active instance | |
| 30 | + * | |
| 31 | + * @access public | |
| 32 | + * @since 1.0.0 | |
| 33 | + * @return GamiPress_Integration_BuddyPress self::$instance The one true GamiPress_Integration_BuddyPress | |
| 34 | + */ | |
| 35 | + public static function instance() { | |
| 36 | + if( !self::$instance ) { | |
| 37 | + self::$instance = new GamiPress_Integration_BuddyPress(); | |
| 38 | + self::$instance->constants(); | |
| 39 | + self::$instance->includes(); | |
| 40 | + self::$instance->bp_includes(); | |
| 41 | + self::$instance->hooks(); | |
| 42 | + | |
| 43 | + } | |
| 44 | + | |
| 45 | + return self::$instance; | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Setup plugin constants | |
| 50 | + * | |
| 51 | + * @access private | |
| 52 | + * @since 1.0.0 | |
| 53 | + * @return void | |
| 54 | + */ | |
| 55 | + private function constants() { | |
| 56 | + | |
| 57 | + if ( defined( 'BP_PLATFORM_VERSION' ) ) return; | |
| 58 | + | |
| 59 | + // Plugin version | |
| 60 | + define( 'GAMIPRESS_BP_VER', '1.6.1' ); | |
| 61 | + | |
| 62 | + // Plugin file | |
| 63 | + define( 'GAMIPRESS_BP_FILE', __FILE__ ); | |
| 64 | + | |
| 65 | + // Plugin path | |
| 66 | + define( 'GAMIPRESS_BP_DIR', plugin_dir_path( __FILE__ ) ); | |
| 67 | + | |
| 68 | + // Plugin URL | |
| 69 | + define( 'GAMIPRESS_BP_URL', plugin_dir_url( __FILE__ ) ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Include plugin files | |
| 74 | + * | |
| 75 | + * @access private | |
| 76 | + * @since 1.0.0 | |
| 77 | + * @return void | |
| 78 | + */ | |
| 79 | + private function includes() { | |
| 80 | + | |
| 81 | + if( $this->meets_requirements() ) { | |
| 82 | + | |
| 83 | + require_once GAMIPRESS_BP_DIR . 'includes/admin.php'; | |
| 84 | + require_once GAMIPRESS_BP_DIR . 'includes/functions.php'; | |
| 85 | + require_once GAMIPRESS_BP_DIR . 'includes/listeners.php'; | |
| 86 | + require_once GAMIPRESS_BP_DIR . 'includes/requirements.php'; | |
| 87 | + require_once GAMIPRESS_BP_DIR . 'includes/rules-engine.php'; | |
| 88 | + require_once GAMIPRESS_BP_DIR . 'includes/scripts.php'; | |
| 89 | + require_once GAMIPRESS_BP_DIR . 'includes/triggers.php'; | |
| 90 | + | |
| 91 | + } | |
| 92 | + | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Include integration specific files | |
| 97 | + * | |
| 98 | + * @since 1.0.1 | |
| 99 | + */ | |
| 100 | + private function bp_includes() { | |
| 101 | + | |
| 102 | + // Since the multisite feature we need an extra check here to meet if BuddyPress is active on current site | |
| 103 | + if ( $this->meets_requirements() && class_exists( 'BuddyPress' ) ) { | |
| 104 | + | |
| 105 | + require_once GAMIPRESS_BP_DIR . 'includes/components/gamipress-achievements-bp-component.php'; | |
| 106 | + require_once GAMIPRESS_BP_DIR . 'includes/components/gamipress-points-bp-component.php'; | |
| 107 | + require_once GAMIPRESS_BP_DIR . 'includes/components/gamipress-ranks-bp-component.php'; | |
| 108 | + | |
| 109 | + // Profile | |
| 110 | + require_once GAMIPRESS_BP_DIR . 'includes/bp-members.php'; | |
| 111 | + | |
| 112 | + // Activity | |
| 113 | + require_once GAMIPRESS_BP_DIR . 'includes/bp-activity.php'; | |
| 114 | + | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Setup plugin hooks | |
| 120 | + * | |
| 121 | + * @access private | |
| 122 | + * @since 1.0.0 | |
| 123 | + * @return void | |
| 124 | + */ | |
| 125 | + private function hooks() { | |
| 126 | + | |
| 127 | + } | |
| 128 | + | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * Activation hook for the plugin. | |
| 132 | + * | |
| 133 | + * @since 1.0.0 | |
| 134 | + */ | |
| 135 | + public static function activate() { | |
| 136 | + | |
| 137 | + if ( ! class_exists( 'GamiPress' ) ) | |
| 138 | + return; | |
| 139 | + | |
| 140 | + GamiPress_Integration_BuddyPress::instance(); | |
| 141 | + | |
| 142 | + global $wpdb; | |
| 143 | + | |
| 144 | + // Get stored version | |
| 145 | + $stored_version = get_option( 'gamipress_buddypress_integration_version', '1.0.0' ); | |
| 146 | + | |
| 147 | + if( function_exists('gamipress_is_network_wide_active') && gamipress_is_network_wide_active() ) { | |
| 148 | + $gamipress_settings = get_site_option( 'gamipress_settings', array() ); | |
| 149 | + } else { | |
| 150 | + $gamipress_settings = get_option( 'gamipress_settings', array() ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + // GamiPress BuddyPress 1.0.5 upgrade | |
| 154 | + if ( version_compare( $stored_version, '1.0.5', '<' ) ) { | |
| 155 | + | |
| 156 | + // Setup new setting | |
| 157 | + $gamipress_settings['bp_members_achievements_types'] = array(); | |
| 158 | + | |
| 159 | + $achievement_types = $wpdb->get_results( "SELECT p.ID, p.post_name FROM {$wpdb->posts} AS p WHERE p.post_type = 'achievement-type'" ); | |
| 160 | + | |
| 161 | + foreach( $achievement_types as $achievement_type ) { | |
| 162 | + $show = (bool) get_post_meta( $achievement_type->ID, '_gamipress_bp_show_bp_member_menu', true ); | |
| 163 | + | |
| 164 | + if( $show ) { | |
| 165 | + $gamipress_settings['bp_members_achievements_types'][] = $achievement_type->post_name; | |
| 166 | + } | |
| 167 | + } | |
| 168 | + | |
| 169 | + } | |
| 170 | + | |
| 171 | + // GamiPress BuddyPress 1.1.8 upgrade | |
| 172 | + if ( version_compare( $stored_version, '1.1.8', '<' ) ) { | |
| 173 | + | |
| 174 | + // Initialize default settings to keep backward compatibility | |
| 175 | + | |
| 176 | + // Label on points types | |
| 177 | + $gamipress_settings['bp_members_points_types_top_label'] = 'on'; | |
| 178 | + | |
| 179 | + // Thumbnail, thumbnail size and link on achievements | |
| 180 | + $gamipress_settings['bp_members_achievements_top_thumbnail'] = 'on'; | |
| 181 | + $gamipress_settings['bp_members_achievements_top_thumbnail_size'] = '25'; | |
| 182 | + $gamipress_settings['bp_members_achievements_top_link'] = 'on'; | |
| 183 | + | |
| 184 | + // Title on ranks | |
| 185 | + $gamipress_settings['bp_members_ranks_top_title'] = 'on'; | |
| 186 | + | |
| 187 | + } | |
| 188 | + | |
| 189 | + // GamiPress BuddyPress 1.2.6 upgrade | |
| 190 | + if ( version_compare( $stored_version, '1.2.6', '<' ) ) { | |
| 191 | + | |
| 192 | + // Update post metas with key '_gamipress_bp_create_bp_activity' to '_gamipress_bp_create_achievement_activity' | |
| 193 | + $wpdb->query( $wpdb->prepare( | |
| 194 | + "UPDATE {$wpdb->postmeta} SET meta_key = %s WHERE meta_key = %s", | |
| 195 | + '_gamipress_bp_create_achievement_activity', | |
| 196 | + '_gamipress_bp_create_bp_activity' | |
| 197 | + ) ); | |
| 198 | + | |
| 199 | + } | |
| 200 | + | |
| 201 | + // GamiPress BuddyPress 1.4.2 upgrade | |
| 202 | + if ( version_compare( $stored_version, '1.4.2', '<' ) ) { | |
| 203 | + | |
| 204 | + // Points tab setting | |
| 205 | + $points_placement = ( isset( $gamipress_settings['bp_points_placement'] ) ? $gamipress_settings['bp_points_placement'] : '' ); | |
| 206 | + | |
| 207 | + if( in_array( $points_placement, array( 'tab', 'both' ) ) && ! isset( $gamipress_settings['bp_points_tab'] ) ) { | |
| 208 | + $gamipress_settings['bp_points_tab'] = 'on'; | |
| 209 | + } | |
| 210 | + | |
| 211 | + // Achievements tab setting | |
| 212 | + $achievements_placement = ( isset( $gamipress_settings['bp_achievements_placement'] ) ? $gamipress_settings['bp_achievements_placement'] : '' ); | |
| 213 | + | |
| 214 | + if( in_array( $achievements_placement, array( 'tab', 'both' ) ) && ! isset( $gamipress_settings['bp_achievements_tab'] ) ) { | |
| 215 | + $gamipress_settings['bp_achievements_tab'] = 'on'; | |
| 216 | + } | |
| 217 | + | |
| 218 | + | |
| 219 | + // Ranks tab setting | |
| 220 | + $ranks_placement = ( isset( $gamipress_settings['bp_ranks_placement'] ) ? $gamipress_settings['bp_ranks_placement'] : '' ); | |
| 221 | + | |
| 222 | + if( in_array( $ranks_placement, array( 'tab', 'both' ) ) && ! isset( $gamipress_settings['bp_ranks_tab'] ) ) { | |
| 223 | + $gamipress_settings['bp_ranks_tab'] = 'on'; | |
| 224 | + } | |
| 225 | + | |
| 226 | + // Clone types and order settings | |
| 227 | + foreach( array( 'points', 'achievements', 'ranks' ) as $key ) { | |
| 228 | + if( ! isset( $gamipress_settings["bp_tab_{$key}_types"] ) ) { | |
| 229 | + $gamipress_settings["bp_tab_{$key}_types"] = $gamipress_settings["bp_members_{$key}_types"]; | |
| 230 | + $gamipress_settings["bp_tab_{$key}_types_order"] = $gamipress_settings["bp_members_{$key}_types_order"]; | |
| 231 | + } | |
| 232 | + } | |
| 233 | + | |
| 234 | + // Finally, update placement to the new options | |
| 235 | + if( ! is_array( $gamipress_settings['bp_points_placement'] ) && in_array( $gamipress_settings['bp_points_placement'], array( 'top', 'both' ) ) ) { | |
| 236 | + $gamipress_settings['bp_points_placement'] = array( 'top' ); | |
| 237 | + } else { | |
| 238 | + $gamipress_settings['bp_points_placement'] = array(); | |
| 239 | + } | |
| 240 | + | |
| 241 | + if( ! is_array( $gamipress_settings['bp_achievements_placement'] ) && in_array( $gamipress_settings['bp_achievements_placement'], array( 'top', 'both' ) ) ) { | |
| 242 | + $gamipress_settings['bp_achievements_placement'] = array( 'top' ); | |
| 243 | + } else { | |
| 244 | + $gamipress_settings['bp_achievements_placement'] = array(); | |
| 245 | + } | |
| 246 | + | |
| 247 | + if( ! is_array( $gamipress_settings['bp_ranks_placement'] ) && in_array( $gamipress_settings['bp_ranks_placement'], array( 'top', 'both' ) ) ) { | |
| 248 | + $gamipress_settings['bp_ranks_placement'] = array( 'top' ); | |
| 249 | + } else { | |
| 250 | + $gamipress_settings['bp_ranks_placement'] = array(); | |
| 251 | + } | |
| 252 | + | |
| 253 | + } | |
| 254 | + | |
| 255 | + // Update GamiPress options | |
| 256 | + if( function_exists('gamipress_is_network_wide_active') && gamipress_is_network_wide_active() ) { | |
| 257 | + update_site_option( 'gamipress_settings', $gamipress_settings ); | |
| 258 | + } else { | |
| 259 | + update_option( 'gamipress_settings', $gamipress_settings ); | |
| 260 | + } | |
| 261 | + | |
| 262 | + // Updated stored version | |
| 263 | + update_option( 'gamipress_buddypress_integration_version', GAMIPRESS_BP_VER ); | |
| 264 | + | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Deactivation hook for the plugin. | |
| 269 | + * | |
| 270 | + * @since 1.0.0 | |
| 271 | + */ | |
| 272 | + public static function deactivate() { | |
| 273 | + | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * Check if there are all plugin requirements | |
| 278 | + * | |
| 279 | + * @since 1.0.0 | |
| 280 | + * | |
| 281 | + * @return bool True if installation meets all requirements | |
| 282 | + */ | |
| 283 | + private function meets_requirements() { | |
| 284 | + | |
| 285 | + if ( defined( 'BP_PLATFORM_VERSION' ) ) return false; | |
| 286 | + | |
| 287 | + | |
| 288 | + if ( ! class_exists( 'GamiPress' ) ) { | |
| 289 | + return false; | |
| 290 | + } | |
| 291 | + | |
| 292 | + // Requirements on multisite install | |
| 293 | + if( is_multisite() && is_main_site() && function_exists('gamipress_is_network_wide_active') && gamipress_is_network_wide_active() ) { | |
| 294 | + // On main site, need to check if integrated plugin is installed on any sub site to load all configuration files | |
| 295 | + if( gamipress_is_plugin_active_on_network( 'buddypress/bp-loader.php' ) ) | |
| 296 | + return true; | |
| 297 | + } | |
| 298 | + | |
| 299 | + if ( ! class_exists( 'BuddyPress' ) ) { | |
| 300 | + return false; | |
| 301 | + } | |
| 302 | + | |
| 303 | + return true; | |
| 304 | + | |
| 305 | + } | |
| 306 | + | |
| 307 | +} | |
| 308 | + | |
| 309 | +/** | |
| 310 | + * The main function responsible for returning the one true GamiPress_Integration_BuddyPress instance to functions everywhere | |
| 311 | + * | |
| 312 | + * @since 1.0.0 | |
| 313 | + * @return \GamiPress_Integration_BuddyPress The one true GamiPress_Integration_BuddyPress | |
| 314 | + */ | |
| 315 | +function GamiPress_BP() { | |
| 316 | + return GamiPress_Integration_BuddyPress::instance(); | |
| 317 | +} | |
| 318 | +add_action( 'gamipress_pre_init', 'GamiPress_BP' ); | |
| 319 | + | |
| 320 | +// Setup our activation and deactivation hooks | |
| 321 | +register_activation_hook( __FILE__, array( 'GamiPress_Integration_BuddyPress', 'activate' ) ); | |
| 322 | +register_deactivation_hook( __FILE__, array( 'GamiPress_Integration_BuddyPress', 'deactivate' ) ); | |