RTMediaGalleryShortcode::render PHP Method

render() static public method

Render a shortcode according to the attributes passed with it
static public render ( boolean $attr ) : boolean | string
$attr boolean
return boolean | string
    static function render($attr)
    {
        if (self::display_allowed()) {
            self::$add_script = true;
            ob_start();
            $authorized_member = true;
            //by default, viewer is authorized
            if (!isset($attr) || empty($attr)) {
                $attr = true;
            }
            $attr = array('name' => 'gallery', 'attr' => $attr);
            global $post;
            if (isset($attr) && isset($attr['attr'])) {
                if (!is_array($attr['attr'])) {
                    $attr['attr'] = array();
                }
                if (!isset($attr['attr']['context_id']) && isset($attr['attr']['context']) && 'profile' === $attr['attr']['context']) {
                    $attr['attr']['context_id'] = get_current_user_id();
                } elseif (!isset($attr['attr']['context_id']) && isset($post->ID)) {
                    $attr['attr']['context_id'] = $post->ID;
                }
                //check if context is group, then the gallery should only be visible to users according to the group privacy
                if (isset($attr['attr']['context']) && 'group' === $attr['attr']['context']) {
                    if (function_exists('groups_get_group')) {
                        //if buddypress group is enabled
                        $group = groups_get_group(array('group_id' => $attr['attr']['context_id']));
                        if (isset($group->status) && 'public' !== $group->status) {
                            if (is_user_logged_in()) {
                                $is_member = groups_is_user_member(get_current_user_id(), $attr['attr']['context_id']);
                                if (!$is_member) {
                                    $authorized_member = false;
                                    //if user doesnot have access to the specified group
                                }
                            } else {
                                $authorized_member = false;
                                //if user is  groupnot logged in and visits group media gallery
                            }
                        }
                    }
                }
                if (!isset($attr['attr']['context']) && isset($post->post_type)) {
                    $attr['attr']['context'] = $post->post_type;
                }
            }
            if ($authorized_member) {
                // if current user has access to view the gallery (when context is 'group')
                global $rtmedia_query;
                if (!$rtmedia_query) {
                    $rtmedia_query = new RTMediaQuery($attr['attr']);
                }
                $rtmedia_query->is_gallery_shortcode = true;
                // to check if gallery shortcode is executed to display the gallery.
                $template = new RTMediaTemplate();
                $gallery_template = false;
                if (isset($attr['attr']['global']) && true === $attr['attr']['global']) {
                    add_filter('rtmedia-model-where-query', array('RTMediaGalleryShortcode', 'rtmedia_query_where_filter'), 10, 3);
                }
                $template->set_template($gallery_template, $attr);
                if (isset($attr['attr']['global']) && true === $attr['attr']['global']) {
                    remove_filter('rtmedia-model-where-query', array('RTMediaGalleryShortcode', 'rtmedia_query_where_filter'), 10, 3);
                }
            } else {
                //if user cannot view the media gallery (when context is 'group'), show message
                esc_html_e('You do not have sufficient privileges to view this gallery', 'buddypress-media');
                return false;
            }
            return ob_get_clean();
        }
    }

Usage Example

function rtmedia_gallery($attr = '')
{
    echo RTMediaGalleryShortcode::render($attr);
}
All Usage Examples Of RTMediaGalleryShortcode::render