Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/29/2015 10:42:56 PM (9 years ago)
Author:
imath
Message:

Introduce functions to register and enqueue the Cover Images Javascript and Css

  • Add the cover-image.js Javascript and register it inside Core Common Scripts
  • Make sure to enqueue Cover Images script only when needed ("change-cover-image" screens)
  • Create a new function to attach an the inline css specified inside the Cover Image feature settings
  • Create the inline css for BP Legacy.
  • Adjust our main css file (buddypress.css) and the companion stylesheets
  • Add the Cover Images Backbone template into bp-legacy/buddypress/assets/_attachments

Props r-a-y

See #6570

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-cssjs.php

    r10108 r10155  
    4343        'bp-avatar'   => array( 'file' => "{$url}avatar{$min}.js", 'dependencies' => array( 'jcrop' ), 'footer' => true ),
    4444        'bp-webcam'   => array( 'file' => "{$url}webcam{$min}.js", 'dependencies' => array( 'bp-avatar' ), 'footer' => true ),
     45
     46        // 2.4
     47        'bp-cover-image' => array( 'file' => "{$url}cover-image{$min}.js", 'dependencies' => array(), 'footer' => true ),
    4548
    4649    ) );
     
    141144
    142145/**
     146 * Enqueues the css and js required by the Cover Image UI.
     147 *
     148 * @since  2.4.0
     149 */
     150function bp_core_cover_image_scripts() {
     151    if ( ! bp_attachments_cover_image_is_edit() ) {
     152        return false;
     153    }
     154
     155    // Enqueue the Attachments scripts for the Cover Image UI
     156    bp_attachments_enqueue_scripts( 'BP_Attachment_Cover_Image' );
     157}
     158add_action( 'bp_enqueue_scripts', 'bp_core_cover_image_scripts' );
     159
     160/**
    143161 * Enqueues jCrop library and hooks BP's custom cropper JS.
    144162 */
     
    338356    ) );
    339357}
     358
     359/**
     360 * Add inline css to display the component's single item cover image
     361 *
     362 * @since 2.4.0
     363 *
     364 * @param  bool $return true to get the inline css
     365 * @return string|array the inline css or an associative array containing
     366 *                      the css rules and the style handle
     367 */
     368function bp_add_cover_image_inline_css( $return = false ) {
     369    $bp = buddypress();
     370
     371    // Find the component of the current item
     372    if ( bp_is_user() ) {
     373
     374        // User is not allowed to upload cover images
     375        // no need to carry on
     376        if ( bp_disable_cover_image_uploads() ) {
     377            return;
     378        }
     379
     380        $cover_image_object = array(
     381            'component' => 'xprofile',
     382            'object' => $bp->displayed_user
     383        );
     384    } elseif ( bp_is_group() ) {
     385
     386        // Users are not allowed to upload cover images for their groups
     387        // no need to carry on
     388        if ( bp_disable_group_cover_image_uploads() ) {
     389            return;
     390        }
     391
     392        $cover_image_object = array(
     393            'component' =>'groups',
     394            'object' => $bp->groups->current_group
     395        );
     396    } else {
     397        $cover_image_object = apply_filters( 'bp_current_cover_image_object_inline_css', array() );
     398    }
     399
     400    // Bail if no component were found.
     401    if ( empty( $cover_image_object['component'] ) || empty( $cover_image_object['object'] ) || ! bp_is_active( $cover_image_object['component'], 'cover_image' ) ) {
     402        return;
     403    }
     404
     405    // Get the settings of the cover image feature for the current component
     406    $params = bp_attachments_get_cover_image_settings( $cover_image_object['component'] );
     407
     408    // Bail if no params.
     409    if ( empty( $params ) ) {
     410        return;
     411    }
     412
     413    // Try to call the callback
     414    if ( is_callable( $params['callback'] ) ) {
     415
     416        $object_dir = $cover_image_object['component'];
     417
     418        if ( 'xprofile' === $object_dir ) {
     419            $object_dir = 'members';
     420        }
     421
     422        $cover_image = bp_attachments_get_attachment( 'url', array(
     423            'object_dir' => $object_dir,
     424            'item_id'    => $cover_image_object['object']->id,
     425        ) );
     426
     427        if ( empty( $cover_image ) ) {
     428            if ( ! empty( $params['default_cover'] ) ) {
     429                $cover_image = $params['default_cover'];
     430            }
     431        }
     432
     433        $inline_css = call_user_func_array( $params['callback'], array( array(
     434            'cover_image' => esc_url( $cover_image ),
     435            'component'   => sanitize_key( $cover_image_object['component'] ),
     436            'object_id'   => (int) $cover_image_object['object']->id,
     437            'width'       => (int) $params['width'],
     438            'height'      => (int) $params['height'],
     439        ) ) );
     440
     441        // Finally add the inline css to the handle
     442        if ( ! empty( $inline_css ) ) {
     443
     444            // Used to get the css when Ajax setting the cover image
     445            if ( true === $return ) {
     446                return array(
     447                    'css_rules' => '<style type="text/css">' . "\n" . $inline_css . "\n" . '</style>',
     448                    'handle'    => $params['theme_handle'],
     449                );
     450            }
     451
     452            wp_add_inline_style( $params['theme_handle'], $inline_css );
     453        } else {
     454            return false;
     455        }
     456    }
     457}
     458add_action( 'bp_enqueue_scripts', 'bp_add_cover_image_inline_css', 11 );
Note: See TracChangeset for help on using the changeset viewer.