RTMediaAlbum::add PHP Method

add() public method

Adds a new album
public add ( string $title = '', boolean | integer $author_id = false, boolean | type $new = true, boolean | integer $post_id = false, boolean $context = false, boolean $context_id = false ) : type
$title string
$author_id boolean | integer
$new boolean | type
$post_id boolean | integer
$context boolean
$context_id boolean
return type
    function add($title = '', $author_id = false, $new = true, $post_id = false, $context = false, $context_id = false)
    {
        global $rtmedia_interaction;
        /* action to perform any task before adding the album */
        do_action('rtmedia_before_add_album');
        $author_id = $author_id ? $author_id : $this->get_current_author();
        /* Album Details which will be passed to Database query to add the album */
        $post_vars = array('post_title' => empty($title) ? esc_html__('Untitled Album', 'buddypress-media') : $title, 'post_type' => 'rtmedia_album', 'post_author' => $author_id, 'post_status' => 'hidden');
        /* Check whether to create a new album in wp_post table
         * This is the case when a user creates a album of his own. We need to
         * create a separte post in wp_post which will work as parent for
         * all the media uploaded to that album
         *
         *  */
        if ($new) {
            $album_id = wp_insert_post($post_vars);
        } else {
            $album_id = $post_id;
        }
        $current_album = get_post($album_id, ARRAY_A);
        if (false === $context) {
            $context = isset($rtmedia_interaction->context->type) ? $rtmedia_interaction->context->type : null;
        }
        if (false === $context_id) {
            $context_id = isset($rtmedia_interaction->context->id) ? $rtmedia_interaction->context->id : null;
        }
        // add in the media since album is also a media
        //defaults
        $attributes = array('blog_id' => get_current_blog_id(), 'media_id' => $album_id, 'album_id' => null, 'media_title' => $current_album['post_title'], 'media_author' => $current_album['post_author'], 'media_type' => 'album', 'context' => $context, 'context_id' => $context_id, 'activity_id' => null, 'privacy' => null);
        $attributes = apply_filters('rtmedia_before_save_album_attributes', $attributes, $_POST);
        // @codingStandardsIgnoreLine
        $rtmedia_id = $this->media->insert_album($attributes);
        $rtmedia_nav = new RTMediaNav();
        $media_count = $rtmedia_nav->refresh_counts($context_id, array('context' => $context, 'media_author' => $context_id));
        /* action to perform any task after adding the album */
        global $rtmedia_points_media_id;
        $rtmedia_points_media_id = $rtmedia_id;
        do_action('rtmedia_after_add_album', $this);
        return $rtmedia_id;
    }

Usage Example

Exemplo n.º 1
0
 function create_album()
 {
     $nonce = filter_input(INPUT_POST, 'create_album_nonce', FILTER_SANITIZE_STRING);
     $_name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
     $return['error'] = false;
     if (wp_verify_nonce($nonce, 'rtmedia_create_album_nonce') && isset($_name) && $_name && is_rtmedia_album_enable()) {
         $_context = filter_input(INPUT_POST, 'context', FILTER_SANITIZE_STRING);
         $_context_id = filter_input(INPUT_POST, 'context_id', FILTER_SANITIZE_NUMBER_INT);
         if (!empty($_context) && 'group' === $_context) {
             $group_id = !empty($_context_id) ? $_context_id : '';
             if (false === can_user_create_album_in_group($group_id)) {
                 $return['error'] = esc_html__('You can not create album in this group.', 'buddypress-media');
             }
         }
         $create_album = apply_filters('rtm_is_album_create_enable', true);
         if (!$create_album) {
             $return['error'] = esc_html__('You can not create album.', 'buddypress-media');
         }
         $create_album = apply_filters('rtm_display_create_album_button', true, $_context_id);
         if (!$create_album) {
             $return['error'] = esc_html__('You can not create more albums, you exceed your album limit.', 'buddypress-media');
         }
         if (false !== $return['error']) {
             wp_send_json($return);
         }
         $album = new RTMediaAlbum();
         // setup context values
         $context = $_context;
         if ('profile' === $context) {
             $context_id = get_current_user_id();
         } else {
             $context_id = !empty($_context_id) ? $_context_id : 0;
         }
         // setup new album data
         $album_data = apply_filters('rtmedia_create_album_data', array('title' => $_name, 'author' => get_current_user_id(), 'new' => true, 'post_id' => false, 'context' => $context, 'context_id' => $context_id));
         $rtmedia_id = $album->add($album_data['title'], $album_data['author'], $album_data['new'], $album_data['post_id'], $album_data['context'], $album_data['context_id']);
         $rtmedia_nav = new RTMediaNav();
         if ('group' === $_context) {
             $rtmedia_nav->refresh_counts($_context_id, array('context' => $_context, 'context_id' => $_context_id));
         } else {
             $rtmedia_nav->refresh_counts(get_current_user_id(), array('context' => 'profile', 'media_author' => get_current_user_id()));
         }
         if ($rtmedia_id) {
             $return['album'] = apply_filters('rtmedia_create_album_response', $rtmedia_id);
             wp_send_json($return);
         } else {
             echo false;
         }
     } else {
         $return['error'] = esc_html__('Data mismatch, Please insert data properly.', 'buddypress-media');
         wp_send_json($return);
     }
     wp_die();
 }
All Usage Examples Of RTMediaAlbum::add