| 1 | <?php |
| 2 | /** |
| 3 | * BuddyPress Theme Packages. |
| 4 | * |
| 5 | * @package BuddyPress |
| 6 | * @subpackage Core |
| 7 | * @since 2.7.0 |
| 8 | */ |
| 9 | |
| 10 | // Exit if accessed directly. |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Displays the Template Packs Table's header & footer. |
| 15 | * |
| 16 | * @since 2.7.0 |
| 17 | * |
| 18 | * @return string HTML Output. |
| 19 | */ |
| 20 | function bp_core_admin_theme_packages_get_head() { |
| 21 | $table_head = array_fill_keys( array( 'head', 'foot' ), array( |
| 22 | 'cb' => ' ', |
| 23 | 'name' => __( 'Name', 'buddypress' ), |
| 24 | 'description' => __( 'Description', 'buddypress' ), |
| 25 | 'actions' => __( 'Actions', 'buddypress' ), |
| 26 | ) ); |
| 27 | |
| 28 | foreach ( $table_head as $kh => $columns ) { |
| 29 | printf( '<t%1$s>%2$s<tr>%3$s', $kh, "\n\t", "\n" ); |
| 30 | |
| 31 | foreach ( $columns as $kc => $column ) { |
| 32 | $class = ''; |
| 33 | if ( 'cb' === $kc ) { |
| 34 | $class = ' check_column'; |
| 35 | } |
| 36 | |
| 37 | printf( |
| 38 | '<th id="%2$s-%3$s" class="manage-column column-%2$s%4$s">%5$s</th>%6$s', |
| 39 | "\t\t", |
| 40 | $kc, |
| 41 | $kh, |
| 42 | $class, |
| 43 | esc_html( $column ), |
| 44 | "\n" |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | printf( '%1$s</tr>%2$s<t%3$s>', "\t", "\n", $kh ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Format Template Pack's metas for display. |
| 54 | * |
| 55 | * @since 2.7.0 |
| 56 | * |
| 57 | * @param object $theme_package The Theme Package we need to get metas for. |
| 58 | * @return string HTML Output. |
| 59 | */ |
| 60 | function bp_core_admin_theme_package_metas( $theme_package_id = '' ) { |
| 61 | $theme_packages = buddypress()->theme_compat->packages; |
| 62 | |
| 63 | if ( ! isset( $theme_packages[ $theme_package_id ] ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $metas = array(); |
| 68 | $theme_package = $theme_packages[ $theme_package_id ]; |
| 69 | |
| 70 | if ( ! empty( $theme_package->__get( 'version' ) ) ) { |
| 71 | $metas[] = sprintf( _x( 'Version %s', 'Version of the Template Pack', 'buddypress' ), esc_html( $theme_package->__get( 'version' ) ) ); |
| 72 | } |
| 73 | |
| 74 | if ( ! empty( $theme_package->__get( 'author' ) ) ) { |
| 75 | $metas[] = sprintf( _x( 'By %s', 'Name of the author of the Template Pack.', 'buddypress' ), esc_html( $theme_package->__get( 'author' ) ) ); |
| 76 | } |
| 77 | |
| 78 | if ( ! empty( $theme_package->__get( 'link' ) ) ) { |
| 79 | $metas[] = sprintf( '<a href="%1$s">%2$s</a>', |
| 80 | esc_url( $theme_package->__get( 'link' ) ), |
| 81 | __( 'Visit Template Pack site', 'buddypress' ) |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | if ( empty( $metas ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | echo join( ' | ', $metas ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets Template Pack's warnings. |
| 94 | * |
| 95 | * @since 2.7.0 |
| 96 | * |
| 97 | * @param object $theme_package The Theme Package we need to set warnings for. |
| 98 | * @return string HTML Output. |
| 99 | */ |
| 100 | function bp_core_admin_theme_package_get_warnings() { |
| 101 | $theme_packages = buddypress()->theme_compat->packages; |
| 102 | $warnings = array(); |
| 103 | |
| 104 | $wp_version = bp_get_major_wp_version(); |
| 105 | $bp_version = bp_get_version(); |
| 106 | $active_components = array_intersect( array_keys( buddypress()->active_components ), bp_core_get_packaged_component_ids() ); |
| 107 | $retired_forums = array_search( 'forums', $active_components ); |
| 108 | |
| 109 | |
| 110 | if ( $retired_forums ) { |
| 111 | $active_components[ $retired_forums ] = 'retired-forums'; |
| 112 | } |
| 113 | |
| 114 | foreach ( $theme_packages as $theme_package ) { |
| 115 | if ( ! empty( $theme_package->__get( 'bp_required_version' ) ) && version_compare( $bp_version, $theme_package->__get( 'bp_required_version' ), '<' ) ) { |
| 116 | $warnings[ $theme_package->id ][] = sprintf( __( 'BuddyPress required version is %s.', 'buddypress' ), $theme_package->__get( 'bp_required_version' ) ); |
| 117 | } |
| 118 | |
| 119 | if ( ! empty( $theme_package->__get( 'wp_required_version' ) ) && version_compare( $wp_version, $theme_package->__get( 'wp_required_version' ), '<' ) ) { |
| 120 | $warnings[ $theme_package->id ][] = sprintf( __( 'WordPress required version is %s.', 'buddypress' ), $theme_package->__get( 'wp_required_version' ) ); |
| 121 | } |
| 122 | |
| 123 | if ( ! empty( $theme_package->__get( 'supports' ) ) ) { |
| 124 | $supported_components = (array) $theme_package->__get( 'supports' ); |
| 125 | |
| 126 | // This component is often forgotten. |
| 127 | if ( ! array_search( 'members', $supported_components ) ) { |
| 128 | $supported_components[] = 'members'; |
| 129 | } |
| 130 | |
| 131 | $no_support = array_diff( $active_components, $supported_components ); |
| 132 | |
| 133 | if ( ! empty( $no_support ) ) { |
| 134 | $warnings[ $theme_package->id ][] = sprintf( __( 'No support is provided for the following component(s): %s.', 'buddypress' ), join( ', ', $no_support ) ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return $warnings; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Displays the Template Packs Table's body. |
| 144 | * |
| 145 | * @since 2.7.0 |
| 146 | * |
| 147 | * @return string HTML Output. |
| 148 | */ |
| 149 | function bp_core_admin_theme_packages_get_body() { |
| 150 | $theme_packages = buddypress()->theme_compat->packages; |
| 151 | $current = bp_get_theme_package_id(); |
| 152 | $warnings = bp_core_admin_theme_package_get_warnings(); |
| 153 | |
| 154 | $customizer_args = array( |
| 155 | 'url' => rawurlencode( bp_loggedin_user_domain() ), |
| 156 | 'return' => esc_url( add_query_arg( 'page', 'bp-theme-packages', bp_get_admin_url( 'admin.php' ) ) ), |
| 157 | ); |
| 158 | ?> |
| 159 | |
| 160 | <tbody id="the-list"> |
| 161 | |
| 162 | <?php if ( empty( $theme_packages ) ) : ?> |
| 163 | |
| 164 | <tr class="no-items"> |
| 165 | <td class="colspanchange" colspan="4"><?php esc_html_e( 'No template packs found.', 'buddypress' ); ?></td> |
| 166 | </tr> |
| 167 | |
| 168 | <?php else : foreach( $theme_packages as $theme_package ) : |
| 169 | $class = 'active'; |
| 170 | $activation_link = ''; |
| 171 | |
| 172 | if ( $theme_package->id !== $current ) { |
| 173 | $class = 'inactive'; |
| 174 | $activation_link = wp_nonce_url( add_query_arg( array( |
| 175 | 'page' => 'bp-theme-packages', |
| 176 | 'action' => 'activate', |
| 177 | 'package' => $theme_package->id, |
| 178 | ), bp_get_admin_url( 'admin.php' ) ), 'bp-theme-packages-activate' ); |
| 179 | |
| 180 | $customizer_args['bp_template_pack'] = $theme_package->id; |
| 181 | } else { |
| 182 | $customizer_args = array_intersect_key( $customizer_args, array( 'url' => true, 'return' => true ) ); |
| 183 | } |
| 184 | |
| 185 | $customizer_link = add_query_arg( $customizer_args, admin_url( 'customize.php' ) ); |
| 186 | ?> |
| 187 | |
| 188 | <tr id="<?php echo esc_attr( $theme_package->id ); ?>" class="<?php echo esc_attr( $theme_package->id ) . ' ' . esc_attr( $class ); ?>"> |
| 189 | <th scope="row" class="check-column"> |
| 190 | |
| 191 | </th> |
| 192 | |
| 193 | <td class="plugin-title" style="width: 190px;"> |
| 194 | <span></span> |
| 195 | |
| 196 | <label for="bp_theme_package-<?php echo esc_attr( $theme_package->id ); ?>"> |
| 197 | <strong><?php echo esc_html( $theme_package->name ); ?></strong> |
| 198 | </label> |
| 199 | |
| 200 | <div class="row-actions-visible"></div> |
| 201 | </td> |
| 202 | |
| 203 | <td class="column-description desc"> |
| 204 | <div class="plugin-description"> |
| 205 | <p><?php echo esc_html( $theme_package->description ); ?></p> |
| 206 | </div> |
| 207 | |
| 208 | <div class="active second plugin-version-author-uri"> |
| 209 | <?php bp_core_admin_theme_package_metas( $theme_package->id ); ?> |
| 210 | </div> |
| 211 | |
| 212 | <?php if ( ! empty( $warnings[ $theme_package->id ] ) ) : ?> |
| 213 | |
| 214 | <div class="attention"> |
| 215 | |
| 216 | <?php echo join( '<br/>', array_map( 'esc_html', $warnings[ $theme_package->id ] ) ); ?> |
| 217 | |
| 218 | </div> |
| 219 | |
| 220 | <?php endif; ?> |
| 221 | </td> |
| 222 | |
| 223 | <td class="column-actions desc"> |
| 224 | <?php if ( ! empty( $warnings[ $theme_package->id ] ) ) : ?> |
| 225 | <?php esc_html_e( 'Not compatible', 'buddypress' ); ?> |
| 226 | <?php elseif ( ! empty( $activation_link ) ) : ?> |
| 227 | <a href="<?php echo esc_url( $activation_link ); ?>" class="button button-primary"><?php esc_html_e( 'Activate', 'buddypress' ); ?></a> |
| 228 | <a href="<?php echo esc_url( $customizer_link ); ?>" class="button button-secondary"><?php esc_html_e( 'Preview', 'buddypress' ); ?></a> |
| 229 | <?php else : ?> |
| 230 | <a href="<?php echo esc_url( $customizer_link ); ?>" class="button button-secondary"><?php esc_html_e( 'Customize', 'buddypress' ); ?></a> |
| 231 | <?php endif; ?> |
| 232 | |
| 233 | </td> |
| 234 | </tr> |
| 235 | |
| 236 | |
| 237 | <?php endforeach; endif; ?> |
| 238 | |
| 239 | </tbody> |
| 240 | <?php |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns feedback messages to inform the site owner. |
| 245 | * |
| 246 | * @since 2.7.0 |
| 247 | * |
| 248 | * @return array The list of feedback messages. |
| 249 | */ |
| 250 | function bp_core_admin_theme_packages_feedbacks() { |
| 251 | /** |
| 252 | * Filter here to add your own feedback messages. |
| 253 | * |
| 254 | * @since 2.7.0 |
| 255 | * |
| 256 | * @param array $value The list of feedback messages. |
| 257 | */ |
| 258 | return apply_filters( 'bp_core_admin_theme_packages_feedbacks', array( |
| 259 | 'activated' => array( |
| 260 | 'message' => __( 'Template pack successfully activated.', 'buddypress' ), |
| 261 | 'type' => 'updated', |
| 262 | ), |
| 263 | 'activation_error' => array( |
| 264 | 'message' => __( 'The Template Pack could not be activated.', 'buddypress' ), |
| 265 | 'type' => 'error', |
| 266 | ), |
| 267 | ) ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Manage the actions the site owner requested. |
| 272 | * |
| 273 | * @since 2.7.0 |
| 274 | */ |
| 275 | function bp_core_admin_theme_packages_load() { |
| 276 | |
| 277 | // Bail if it's not a BP Theme Package form. |
| 278 | if ( empty( $_GET['action'] ) ) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | $action = $_GET['action']; |
| 283 | |
| 284 | // Build the redirect args. |
| 285 | $query_arg = array( 'page' => 'bp-theme-packages' ); |
| 286 | |
| 287 | if ( 'activate' === $action && ! empty( $_GET['package'] ) ) { |
| 288 | // Check the nonce. |
| 289 | check_admin_referer( 'bp-theme-packages-activate' ); |
| 290 | |
| 291 | $theme_packages = buddypress()->theme_compat->packages; |
| 292 | $theme_package_id = sanitize_key( $_GET['package'] ); |
| 293 | |
| 294 | // Bail if we can't activate the Template Pack due to some missing informations. |
| 295 | if ( ! isset( $theme_packages[ $theme_package_id ] ) || ! is_dir( $theme_packages[ $theme_package_id ]->dir ) || ! file_exists( $theme_packages[ $theme_package_id ]->dir . '/buddypress-functions.php' ) ) { |
| 296 | $query_arg['status'] = 'activation_error'; |
| 297 | |
| 298 | // Process to the activation. |
| 299 | } else { |
| 300 | |
| 301 | // Update the Theme Package ID |
| 302 | bp_update_option( '_bp_theme_package_id', $theme_package_id ); |
| 303 | $query_arg['status'] = 'activated'; |
| 304 | } |
| 305 | |
| 306 | } else { |
| 307 | /** |
| 308 | * Hook here to run your custom actions |
| 309 | * |
| 310 | * @since 2.7.0 |
| 311 | * |
| 312 | * @param $action the current Template Pack action. |
| 313 | */ |
| 314 | do_action( 'bp_core_admin_theme_packages_load', $action ); |
| 315 | } |
| 316 | |
| 317 | wp_safe_redirect( add_query_arg( $query_arg, bp_get_admin_url( 'admin.php' ) ) ); |
| 318 | exit(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Main UI to Manage the Template Packs. |
| 323 | * |
| 324 | * @since 2.7.0 |
| 325 | * |
| 326 | * @return string HTML Output. |
| 327 | */ |
| 328 | function bp_core_admin_theme_packages() { |
| 329 | $bp = buddypress(); |
| 330 | |
| 331 | ?> |
| 332 | <div class="wrap"> |
| 333 | |
| 334 | <h1><?php _e( 'BuddyPress Settings', 'buddypress' ); ?> </h1> |
| 335 | |
| 336 | <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Template Pack', 'buddypress' ) ); ?></h2> |
| 337 | |
| 338 | <?php |
| 339 | // User feedbacks |
| 340 | if ( isset( $_GET['status'] ) ) { |
| 341 | // Defaults to an error. |
| 342 | $type = 'error'; |
| 343 | $how_to = ''; |
| 344 | $feedbacks = bp_core_admin_theme_packages_feedbacks(); |
| 345 | |
| 346 | if ( isset( $feedbacks[ $_GET['status'] ] ) ) { |
| 347 | $feedback = $feedbacks[ $_GET['status'] ]; |
| 348 | |
| 349 | // Display the feedback message. |
| 350 | printf( |
| 351 | '<div id="message" class="%1$s notice is-dismissible"><p>%2$s</p></div>', |
| 352 | $feedback['type'], |
| 353 | esc_html( $feedback['message'] ) |
| 354 | ); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Fires before the form tag. |
| 360 | * |
| 361 | * @since 2.7.0 |
| 362 | */ |
| 363 | do_action( 'bp_core_admin_before_theme_packages_form' ); ?> |
| 364 | |
| 365 | <form action="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-theme-packages' ), bp_get_admin_url( 'admin.php' ) ) ); ?>" method="post" id="bp-core-admin-theme-packages-form" style="margin-top:1.5em"> |
| 366 | |
| 367 | <?php |
| 368 | /** |
| 369 | * Fires before the list table. |
| 370 | * |
| 371 | * @since 2.7.0 |
| 372 | */ |
| 373 | do_action( 'bp_core_admin_before_theme_packages_list' ); ?> |
| 374 | |
| 375 | <table class="widefat plugins" cellspacing="0"> |
| 376 | |
| 377 | <?php bp_core_admin_theme_packages_get_head() ;?> |
| 378 | |
| 379 | <?php bp_core_admin_theme_packages_get_body() ;?> |
| 380 | |
| 381 | </table> |
| 382 | |
| 383 | <?php |
| 384 | /** |
| 385 | * Fires after the list table. |
| 386 | * |
| 387 | * @since 2.7.0 |
| 388 | */ |
| 389 | do_action( 'bp_core_admin_after_theme_packages_list' ); |
| 390 | |
| 391 | wp_nonce_field( 'bp-theme-packages-manage' ); ?> |
| 392 | |
| 393 | </form> |
| 394 | </div> |
| 395 | <?php |
| 396 | } |