Beispiel #1
0
/**
 * @param      $action
 * @param      $data
 * @param bool $filter
 *
 * @return array
 */
function gmedia_ios_app_processor($action, $data, $filter = true)
{
    global $gmCore, $gmDB, $gmGallery, $user_ID, $gmapp_version;
    $out = array();
    if (version_compare('3', $gmapp_version, '<=')) {
        $logic = 2;
        if (version_compare('3.1', $gmapp_version, '<')) {
            $logic = 3;
        }
    } else {
        $logic = 1;
    }
    $error = array();
    $error_info = array();
    $alert = array();
    $alert_info = array();
    $data = (array) $data;
    switch ($action) {
        case 'do_library':
            if (!isset($data['action'])) {
                return $out;
            }
            $filter = array();
            switch ($data['action']) {
                case 'add_media':
                    if (!current_user_can('gmedia_upload')) {
                        $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'grand-media'), 'message' => __('You have no permission to do this operation', 'grand-media'));
                        return $out;
                    }
                    usleep(10);
                    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
                        $file_name = $_FILES['userfile']['name'];
                        $file_tmp = $_FILES['userfile']['tmp_name'];
                    } else {
                        $error[] = __("Failed to move uploaded file.", 'grand-media');
                        break;
                    }
                    $fileinfo = $gmCore->fileinfo($file_name);
                    if (false === $fileinfo) {
                        break;
                    }
                    $gmedia = (array) $data['item'];
                    if (!current_user_can('gmedia_terms')) {
                        unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                    } else {
                        if (empty($gmedia['albums'])) {
                            $gmedia['terms']['gmedia_album'] = '';
                        } else {
                            $alb = isset($gmedia['albums'][0]->term_id) ? $gmedia['albums'][0]->term_id : $gmedia['albums'][0]->name;
                            $gmedia['terms']['gmedia_album'] = $alb;
                        }
                        if (empty($gmedia['categories'])) {
                            $gmedia['terms']['gmedia_category'] = '';
                        } else {
                            $categories = array();
                            foreach ($gmedia['categories'] as $category) {
                                $categories[] = isset($category->term_id) ? $category->term_id : $category->name;
                            }
                            $gmedia['terms']['gmedia_category'] = implode(',', $categories);
                        }
                        if (empty($gmedia['tags'])) {
                            $gmedia['terms']['gmedia_tag'] = '';
                        } else {
                            $tags = array();
                            foreach ($gmedia['tags'] as $tag) {
                                $tags[] = isset($tag->term_id) ? $tag->term_id : $tag->name;
                            }
                            $gmedia['terms']['gmedia_tag'] = implode(',', $tags);
                        }
                        unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                    }
                    if (isset($gmedia['status']) && 'public' == $gmedia['status']) {
                        $gmedia['status'] = 'publish';
                    }
                    $return = $gmCore->gmedia_upload_handler($file_tmp, $fileinfo, 'multipart', $gmedia);
                    if (isset($return['error'])) {
                        $error[] = $return['error']['message'];
                    } else {
                        $alert[] = $return['success']['message'];
                    }
                    break;
                case 'update_media':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    $gmedia = (array) $data['item'];
                    if (!empty($gmedia['ID'])) {
                        $item = $gmDB->get_gmedia($gmedia['ID']);
                        if (!$item || $user_ID != $item->author && !current_user_can('gmedia_edit_others_media')) {
                            $error[] = __('You are not allowed to edit others media', 'grand-media');
                            break;
                        }
                        unset($gmedia['date'], $gmedia['mime_type'], $gmedia['gmuid'], $gmedia['modified']);
                        //$gmedia['modified']  = current_time('mysql');
                        if (!current_user_can('gmedia_delete_others_media')) {
                            $gmedia['author'] = $item->author;
                        }
                        if (isset($gmedia['status']) && 'public' == $gmedia['status']) {
                            $gmedia['status'] = 'publish';
                        }
                        if (!current_user_can('gmedia_terms')) {
                            unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                        } else {
                            if (empty($gmedia['albums'])) {
                                $gmedia['terms']['gmedia_album'] = '';
                            } else {
                                if (isset($gmedia['albums'][0]->term_id)) {
                                    $gmedia['terms']['gmedia_album'] = $gmedia['albums'][0]->term_id;
                                } elseif (current_user_can('gmedia_album_manage')) {
                                    $gmedia['terms']['gmedia_album'] = $gmedia['albums'][0]->name;
                                }
                            }
                            if (empty($gmedia['categories'])) {
                                $gmedia['terms']['gmedia_category'] = '';
                            } else {
                                $categories = array();
                                foreach ($gmedia['categories'] as $category) {
                                    if (isset($category->term_id)) {
                                        $categories[] = $category->term_id;
                                    } elseif (current_user_can('gmedia_category_manage')) {
                                        $categories[] = $category->name;
                                    }
                                }
                                $gmedia['terms']['gmedia_category'] = $categories;
                            }
                            if (empty($gmedia['tags'])) {
                                $gmedia['terms']['gmedia_tag'] = '';
                            } else {
                                $tags = array();
                                foreach ($gmedia['tags'] as $tag) {
                                    if (isset($tag->term_id)) {
                                        $tags[] = $tag->term_id;
                                    } elseif (current_user_can('gmedia_tag_manage')) {
                                        $tags[] = $tag->name;
                                    }
                                }
                                $gmedia['terms']['gmedia_tag'] = $tags;
                            }
                            unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                        }
                        $gmDB->insert_gmedia($gmedia);
                    }
                    break;
                case 'assign_album':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    if (!current_user_can('gmedia_terms')) {
                        $error[] = __('You are not allowed to manage albums', 'grand-media');
                    }
                    $term = $data['assign_album'][0];
                    $count = count($data['selected']);
                    if ('0' == $term) {
                        foreach ($data['selected'] as $item) {
                            $gmDB->delete_gmedia_term_relationships($item, 'gmedia_album');
                        }
                        $alert[] = sprintf(__('%d item(s) updated with "No Album"', 'grand-media'), $count);
                    } else {
                        foreach ($data['selected'] as $item) {
                            $gm_item = $gmDB->get_gmedia($item);
                            if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                                continue;
                            }
                            $result = $gmDB->set_gmedia_terms($item, $term, 'gmedia_album', $append = 0);
                            if (is_wp_error($result)) {
                                $error[] = $result->get_error_message();
                                $count--;
                            } elseif (!$result) {
                                $count--;
                            }
                        }
                        if ($gmCore->is_digit($term)) {
                            $alb_name = $gmDB->get_term_name($term);
                        } else {
                            $alb_name = $term;
                        }
                        $alert[] = sprintf(__('Album `%s` assigned to %d item(s)', 'grand-media'), esc_html($alb_name), $count);
                    }
                    break;
                case 'assign_category':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    if (!current_user_can('gmedia_terms')) {
                        $error[] = __('You are not allowed to manage categories', 'grand-media');
                        break;
                    }
                    if (empty($data['assign_category'])) {
                        $error[] = __('No categories provided', 'grand-media');
                        break;
                    }
                    $terms = $data['assign_category'];
                    $count = count($data['selected']);
                    if (1 === count($terms) && '0' == $terms[0]) {
                        foreach ($data['selected'] as $item) {
                            $gm_item = $gmDB->get_gmedia($item);
                            if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                                continue;
                            }
                            $gmDB->delete_gmedia_term_relationships($item, 'gmedia_category');
                        }
                        $alert[] = sprintf(__('%d item(s) updated with "Uncategorized"', 'grand-media'), $count);
                    } else {
                        foreach ($data['selected'] as $item) {
                            $gm_item = $gmDB->get_gmedia($item);
                            if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                                continue;
                            }
                            $result = $gmDB->set_gmedia_terms($item, $terms, 'gmedia_category', $append = 0);
                            if (is_wp_error($result)) {
                                $error[] = $result->get_error_message();
                                $count--;
                            } elseif (!$result) {
                                $count--;
                            }
                        }
                        $alert[] = sprintf(__('%d category(ies) added to %d item(s)', 'grand-media'), count($terms), $count);
                    }
                    break;
                case 'unassign_category':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    if (empty($data['unassign_category'])) {
                        $error[] = __('No categories provided', 'grand-media');
                        break;
                    }
                    $terms = array_map('intval', $data['unassign_category']);
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $gm_item = $gmDB->get_gmedia($item);
                        if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                            continue;
                        }
                        $result = $gmDB->set_gmedia_terms($item, $terms, 'gmedia_category', $append = -1);
                        if (is_wp_error($result)) {
                            $error[] = $result->get_error_message();
                            $count--;
                        } elseif (!$result) {
                            $count--;
                        }
                    }
                    $alert[] = sprintf(__('%d category(ies) deleted from %d item(s)', 'grand-media'), count($terms), $count);
                    break;
                case 'add_tags':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    if (!current_user_can('gmedia_terms')) {
                        $error[] = __('You are not allowed manage tags', 'grand-media');
                        break;
                    }
                    if (empty($data['add_tags'])) {
                        $error[] = __('No tags provided', 'grand-media');
                        break;
                    }
                    $terms = $data['add_tags'];
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $gm_item = $gmDB->get_gmedia($item);
                        if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                            continue;
                        }
                        $result = $gmDB->set_gmedia_terms($item, $terms, 'gmedia_tag', $append = 1);
                        if (is_wp_error($result)) {
                            $error[] = $result->get_error_message();
                            $count--;
                        } elseif (!$result) {
                            $count--;
                        }
                    }
                    $alert[] = sprintf(__('%d tag(s) added to %d item(s)', 'grand-media'), count($terms), $count);
                    break;
                case 'add_cover':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    $cover = (int) $data['add_cover'];
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $gm_item = $gmDB->get_gmedia($item);
                        if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                            $count--;
                            continue;
                        }
                        if ('image' == substr($gm_item->mime_type, 0, 5)) {
                            $count--;
                            continue;
                        }
                        if ($cover) {
                            $gmDB->update_metadata('gmedia', $gm_item->ID, '_cover', $cover);
                        } else {
                            $gmDB->delete_metadata('gmedia', $gm_item->ID, '_cover');
                        }
                    }
                    $alert[] = sprintf(__('%d item(s) updated', 'grand-media'), $count);
                    break;
                case 'delete_tags':
                    if (!current_user_can('gmedia_edit_media')) {
                        $error[] = __('You are not allowed to edit media', 'grand-media');
                        break;
                    }
                    if (empty($data['delete_tags'])) {
                        $error[] = __('No tags provided', 'grand-media');
                        break;
                    }
                    $terms = array_map('intval', $data['delete_tags']);
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $gm_item = $gmDB->get_gmedia($item);
                        if (!$gm_item || $user_ID != $gm_item->author && !current_user_can('gmedia_edit_others_media')) {
                            continue;
                        }
                        $result = $gmDB->set_gmedia_terms($item, $terms, 'gmedia_tag', $append = -1);
                        if (is_wp_error($result)) {
                            $error[] = $result->get_error_message();
                            $count--;
                        } elseif (!$result) {
                            $count--;
                        }
                    }
                    $alert[] = sprintf(__('%d tag(s) deleted from %d item(s)', 'grand-media'), count($terms), $count);
                    break;
                case 'delete':
                    if (!current_user_can('gmedia_delete_media')) {
                        $error[] = __('You are not allowed to delete this post.');
                        break;
                    }
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $gm_item = $gmDB->get_gmedia($item);
                        if ((int) $gm_item->author != $user_ID && !current_user_can('gmedia_delete_others_media')) {
                            $error[] = "#{$item}: " . __('You are not allowed to delete media others media', 'grand-media');
                            continue;
                        }
                        if (!$gmDB->delete_gmedia((int) $item)) {
                            $error[] = "#{$item}: " . __('Error in deleting...', 'grand-media');
                            $count--;
                        }
                    }
                    if ($count) {
                        $alert[] = sprintf(__('%d items deleted successfuly', 'grand-media'), $count);
                    }
                    break;
            }
            if (1 === $logic) {
                $filter = gmedia_ios_app_library_data(array('filter', 'gmedia_category', 'gmedia_album', 'gmedia_tag'));
            }
            $out = array_merge($out, $filter);
            break;
        case 'library':
            $ep = $gmGallery->options['endpoint'];
            if (get_option('permalink_structure')) {
                $share_link_base = home_url(urlencode($ep) . '/$2/$1');
            } else {
                $share_link_base = add_query_arg(array("{$ep}" => '$1', 't' => '$2'), home_url('index.php'));
            }
            $filter = $filter ? gmedia_ios_app_library_data(array('filter')) : array();
            $mime_type = $logic > 2 ? array('image', 'audio') : 'image';
            $args = array('mime_type' => $mime_type, 'orderby' => 'ID', 'order' => 'DESC', 'per_page' => 100, 'page' => 1, 'tag__in' => null, 'category__in' => null, 'album__in' => null, 'gmedia__in' => null, 'author' => 0, 'status' => null);
            $terms_ids_query = array();
            if (!empty($data['tag__in'])) {
                if (empty($data['category__in']) && empty($data['album__in'])) {
                    $args['orderby'] = $gmGallery->options['in_tag_orderby'];
                    $args['order'] = $gmGallery->options['in_tag_order'];
                }
                $terms_ids_query = array_merge($terms_ids_query, $data['tag__in']);
            }
            if (!empty($data['category__in'])) {
                $cat_ids = wp_parse_id_list($data['category__in']);
                if (1 === count($cat_ids)) {
                    $cat_meta = $gmDB->get_metadata('gmedia_term', $cat_ids[0]);
                    $args['orderby'] = !empty($cat_meta['_orderby'][0]) ? $cat_meta['_orderby'][0] : $gmGallery->options['in_category_orderby'];
                    $args['order'] = !empty($cat_meta['_order'][0]) ? $cat_meta['_order'][0] : $gmGallery->options['in_category_order'];
                }
                $terms_ids_query = array_merge($terms_ids_query, $cat_ids);
            }
            if (!empty($data['album__in'])) {
                $alb_ids = wp_parse_id_list($data['album__in']);
                if (1 === count($alb_ids)) {
                    $album_meta = $gmDB->get_metadata('gmedia_term', $alb_ids[0]);
                    $args['orderby'] = !empty($album_meta['_orderby'][0]) ? $album_meta['_orderby'][0] : $gmGallery->options['in_album_orderby'];
                    $args['order'] = !empty($album_meta['_order'][0]) ? $album_meta['_order'][0] : $gmGallery->options['in_album_order'];
                }
                $terms_ids_query = array_merge($terms_ids_query, $alb_ids);
            }
            $data = wp_parse_args($data, $args);
            $false_out = array_merge($filter, array('properties' => array('request' => isset($data['request']) ? $data['request'] : null), 'data' => array()));
            $terms_ids = array();
            if (!empty($terms_ids_query)) {
                $terms_ids = $gmDB->get_terms(array('gmedia_album', 'gmedia_category', 'gmedia_tag'), array('include' => $terms_ids_query));
                if (!empty($terms_ids) && !is_wp_error($terms_ids)) {
                    foreach ($terms_ids as $i => $term) {
                        gmedia_ios_app_term_data_extend($terms_ids[$i], $share_link_base);
                    }
                    $terms_ids = array_filter($terms_ids);
                    if (empty($terms_ids)) {
                        $out = $false_out;
                        break;
                    }
                } else {
                    $terms_ids = array();
                }
            }
            $req_terms = array();
            if (!empty($terms_ids)) {
                foreach ($terms_ids as $term) {
                    $taxterm = str_replace('gmedia_', '', $term->taxonomy);
                    $req_terms["{$taxterm}__in"]["{$term->term_id}"] = $term;
                }
            }
            $is_admin = isset($data['admin']) ? intval($data['admin']) : 0;
            if (!is_user_logged_in()) {
                $logged_in = false;
                $data['status'] = array('publish');
                if (!empty($req_terms['album__in'])) {
                    $break = false;
                    foreach ($req_terms['album__in'] as $alb) {
                        if (!(isset($alb->status) && 'publish' == $alb->status)) {
                            $break = true;
                        }
                    }
                    if ($break) {
                        $out = $false_out;
                        break;
                    }
                }
            } else {
                $logged_in = true;
                if ($is_admin && !current_user_can('gmedia_library')) {
                    $out = $false_out;
                    break;
                }
                if ($is_admin && !current_user_can('gmedia_show_others_media')) {
                    $data['author'] = $user_ID;
                }
            }
            $_data = $data;
            $_data['per_page'] = -1;
            $_data['status'] = null;
            $_data['fields'] = 'ids';
            $all_gmedias_ids = $gmDB->get_gmedias($_data);
            $gmedias = $gmDB->get_gmedias($data);
            $properties = array_merge($req_terms, array('request' => isset($data['request']) ? $data['request'] : null, 'total_pages' => $gmDB->pages, 'current_page' => $gmDB->openPage, 'items_count' => $gmDB->resultPerPage, 'total_count' => $gmDB->totalResult, 'count' => count($all_gmedias_ids)));
            foreach ($gmedias as $i => $item) {
                //if((!$logged_in && 'publish' != $item->status) || (!$is_admin && ('draft' == $item->status) && ((int)$user_ID != (int)$item->author))) {
                if ((!$is_admin || $is_admin && !current_user_can('gmedia_edit_others_media')) && ('draft' == $item->status && (int) $user_ID != (int) $item->author)) {
                    unset($gmedias[$i]);
                    $properties['total_count']--;
                    $properties['items_count']--;
                    continue;
                }
                $author_id = $item->author;
                $authordata = get_userdata($author_id);
                if ($authordata) {
                    $display_name = $authordata->display_name;
                    $first_name = $authordata->first_name;
                    $last_name = $authordata->last_name;
                } else {
                    $display_name = __('Deleted User', 'grand-media');
                    $first_name = '';
                    $last_name = '';
                }
                $gmedias[$i]->user = array('id' => $author_id, 'displayname' => $display_name, 'firstname' => $first_name, 'last_name' => $last_name);
                $gmedias[$i]->date = strtotime($item->date);
                $meta = $gmDB->get_metadata('gmedia', $item->ID);
                //$_metadata        = maybe_unserialize( $meta['_metadata'][0] );
                $_metadata = $meta['_metadata'][0];
                unset($meta['_metadata']);
                $type = explode('/', $item->mime_type);
                $item_url = $gmCore->upload['url'] . '/' . $gmGallery->options['folder'][$type[0]] . '/' . $item->gmuid;
                $gmedias[$i]->url = $item_url;
                $terms = $gmDB->get_the_gmedia_terms($item->ID, 'gmedia_tag');
                $tags = array();
                if ($terms) {
                    $terms = array_values((array) $terms);
                    foreach ($terms as $term) {
                        $tags[] = array('term_id' => $term->term_id, 'name' => $term->name);
                    }
                }
                $gmedias[$i]->tags = $tags;
                $terms = $gmDB->get_the_gmedia_terms($item->ID, 'gmedia_album');
                $albums = array();
                if ($terms) {
                    $terms = array_values((array) $terms);
                    foreach ($terms as $term) {
                        $albums[] = array('term_id' => $term->term_id, 'name' => $term->name, 'status' => 1 === $logic && 'publish' == $term->status ? 'public' : $term->status);
                    }
                }
                $gmedias[$i]->albums = $albums;
                if ('image' == $type[0]) {
                    $terms = $gmDB->get_the_gmedia_terms($item->ID, 'gmedia_category');
                    $categories = array();
                    if ($terms) {
                        $terms = array_values((array) $terms);
                        foreach ($terms as $term) {
                            $categories[] = array('term_id' => $term->term_id, 'name' => $term->term_id, 'title' => $term->name);
                        }
                    }
                    $gmedias[$i]->categories = $categories;
                    $gmedias[$i]->meta = array('thumb' => $_metadata['thumb'], 'web' => $_metadata['web'], 'original' => $_metadata['original']);
                    $gmedias[$i]->meta['thumb']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image_thumb']}/{$item->gmuid}";
                    $gmedias[$i]->meta['web']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image']}/{$item->gmuid}";
                    if (is_file("{$gmCore->upload['path']}/{$gmGallery->options['folder']['image_original']}/{$item->gmuid}")) {
                        $gmedias[$i]->meta['original']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image_original']}/{$item->gmuid}";
                    } else {
                        $gmedias[$i]->meta['original']['link'] = '';
                    }
                    if (isset($_metadata['image_meta'])) {
                        $gmedias[$i]->meta['data'] = $_metadata['image_meta'];
                    }
                    unset($meta['image_meta']);
                } else {
                    $cover_gmedia = false;
                    if (!empty($meta['_cover'][0])) {
                        $cover_gmedia = $gmDB->get_gmedia($meta['_cover'][0]);
                        if ($cover_gmedia) {
                            $cover_metadata = $gmDB->get_metadata('gmedia', $cover_gmedia->ID, '_metadata', true);
                            $gmedias[$i]->meta = array('thumb' => $cover_metadata['thumb'], 'web' => $cover_metadata['web'], 'original' => $cover_metadata['original']);
                            $gmedias[$i]->meta['thumb']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image_thumb']}/{$cover_gmedia->gmuid}";
                            $gmedias[$i]->meta['web']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image']}/{$cover_gmedia->gmuid}";
                            if (is_file("{$gmCore->upload['path']}/{$gmGallery->options['folder']['image_original']}/{$cover_gmedia->gmuid}")) {
                                $gmedias[$i]->meta['original']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image_original']}/{$cover_gmedia->gmuid}";
                            } else {
                                $gmedias[$i]->meta['original']['link'] = '';
                            }
                        }
                    }
                    unset($meta['_cover']);
                    if (!$cover_gmedia) {
                        $gmedias[$i]->meta = array('thumb' => array('link' => $gmCore->gm_get_media_image($item, 'thumb', false), 'width' => 300, 'height' => 300));
                    }
                    if (!empty($_metadata)) {
                        $gmedias[$i]->meta['data'] = $_metadata;
                    }
                }
                $gmedias[$i]->meta['views'] = 0;
                $gmedias[$i]->meta['likes'] = 0;
                if (isset($meta['views'][0])) {
                    $gmedias[$i]->meta['views'] = $meta['views'][0];
                }
                unset($meta['views']);
                if (isset($meta['likes'][0])) {
                    $gmedias[$i]->meta['likes'] = $meta['likes'][0];
                }
                unset($meta['likes']);
                if (!empty($meta['_gps'][0])) {
                    $gmedias[$i]->meta['data']['GPS'] = $meta['_gps'][0];
                }
                unset($meta['_gps']);
                if (isset($meta['_rating'][0])) {
                    $gmedias[$i]->meta['rating'] = maybe_unserialize($meta['_rating'][0]);
                }
                unset($meta['_rating']);
                if (!empty($data['meta']) && !empty($meta)) {
                    foreach ($meta as $key => $val) {
                        if ('_peaks' == $key) {
                            $gmedias[$i]->meta[$key] = json_decode($val[0]);
                        } else {
                            $gmedias[$i]->meta[$key] = maybe_unserialize($val);
                        }
                    }
                }
                $item_name = $item->title ? $item->title : pathinfo($item->gmuid, PATHINFO_FILENAME);
                $gmedia_hashid = gmedia_hash_id_encode($item->ID, 'single');
                $gmedias[$i]->sharelink = str_replace(array('$1', '$2'), array(urlencode($gmedia_hashid), 's'), $share_link_base);
                if (1 === $logic && 'publish' == $item->status) {
                    $gmedias[$i]->status = 'public';
                }
            }
            $out = array_merge($filter, array('properties' => $properties, 'data' => array_values($gmedias)));
            break;
        case 'delete_term':
            $taxonomy = $data['taxonomy'];
            if (!empty($data['items'])) {
                if (!current_user_can('gmedia_terms_delete')) {
                    $error[] = __('You have no permission to do this operation', 'grand-media');
                    break;
                }
                $count = count($data['items']);
                foreach ($data['items'] as $item) {
                    if (!current_user_can('gmedia_edit_others_media')) {
                        if ('gmedia_album' == $taxonomy) {
                            $term = $gmDB->get_term($item, $taxonomy);
                            if ((int) $term->global != (int) $user_ID) {
                                $error['delete_album'] = __('You are not allowed to edit others media', 'grand-media');
                                $count--;
                                continue;
                            }
                        } else {
                            $error[] = __('You are not allowed to edit others media', 'grand-media');
                            $count--;
                            continue;
                        }
                    }
                    $delete = $gmDB->delete_term($item);
                    if (is_wp_error($delete)) {
                        $error[] = $delete->get_error_message();
                        $count--;
                    }
                }
                if ($count) {
                    $alert[] = sprintf(__('%d items deleted successfuly', 'grand-media'), $count);
                }
            }
            $out = gmedia_ios_app_library_data(array('filter', $taxonomy));
            break;
        case 'add_term':
            $taxonomy = $data['taxonomy'];
            $edit_term = isset($data['term_id']) ? (int) $data['term_id'] : 0;
            $term = $data;
            $term_id = 0;
            if ('gmedia_album' == $taxonomy) {
                if (!current_user_can('gmedia_album_manage')) {
                    $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'grand-media'), 'message' => __('You have no permission to do this operation', 'grand-media'));
                    return $out;
                }
                $args = array();
                do {
                    $term['name'] = trim($term['name']);
                    if (empty($term['name'])) {
                        $error[] = __('Term Name is not specified', 'grand-media');
                        break;
                    }
                    if ($gmCore->is_digit($term['name'])) {
                        $error[] = __("Term Name can't be only digits", 'grand-media');
                        break;
                    }
                    if (1 === $logic && isset($term['status']) && 'public' == $term['status']) {
                        $term['status'] = 'publish';
                    }
                    if ($edit_term && !$gmDB->term_exists($edit_term, $taxonomy)) {
                        $error[] = __('A term with the id provided does not exists', 'grand-media');
                        $edit_term = false;
                    }
                    $term_author = isset($term['global']) ? $term['global'] : false;
                    if ($term_id = $gmDB->term_exists($term['name'], $taxonomy, $term_author)) {
                        if ($term_id != $edit_term) {
                            $error[] = __('A term with the name provided already exists', 'grand-media');
                            $error_info['terms'][$term_id] = $term['name'];
                            break;
                        }
                    }
                    if ($edit_term) {
                        $_term = $gmDB->get_term($edit_term, $taxonomy);
                        if ((int) $_term->global != (int) $user_ID && !current_user_can('gmedia_edit_others_media')) {
                            $error[] = __('You are not allowed to edit others media', 'grand-media');
                            break;
                        }
                        $term_id = $gmDB->update_term($edit_term, $term);
                    } else {
                        if (!current_user_can('gmedia_edit_others_media')) {
                            $term['global'] = intval($user_ID);
                        }
                        $term_id = $gmDB->insert_term($term['name'], $taxonomy, $term);
                    }
                    if (is_wp_error($term_id)) {
                        $error[] = $term_id->get_error_message();
                        $term_id = 0;
                        break;
                    }
                    $alert_info['terms'][$term_id] = $term['name'];
                    $term_meta = array();
                    if (isset($term['_orderby'])) {
                        $term_meta['_orderby'] = $term['_orderby'];
                    } elseif (isset($term['orderby'])) {
                        $term_meta['_orderby'] = $term['orderby'];
                    }
                    if (isset($term['_order'])) {
                        $term_meta['_order'] = $term['_order'];
                    } elseif (isset($term['order'])) {
                        $term_meta['_order'] = $term['order'];
                    }
                    if (isset($term['cover_id'])) {
                        $term_meta['_cover'] = (int) $term['cover_id'] ? $term['cover_id'] : '';
                    }
                    foreach ($term_meta as $key => $value) {
                        $gmDB->update_metadata('gmedia_term', $term_id, $key, $value);
                    }
                    $alert[] = sprintf(__('Album `%s` successfuly saved', 'grand-media'), $term['name']);
                } while (0);
                if (1 < $logic && $edit_term && $term_id) {
                    $lib_data = array('album__in' => array($term_id), 'admin' => 1);
                    if (!empty($data['per_page'])) {
                        $lib_data['per_page'] = $data['per_page'];
                    }
                    $out = gmedia_ios_app_processor('library', $lib_data);
                } else {
                    $out = gmedia_ios_app_library_data(array('filter', $taxonomy), $args);
                }
            } elseif ('gmedia_category' == $taxonomy) {
                if (!current_user_can('gmedia_category_manage')) {
                    $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'grand-media'), 'message' => __('You have no permission to do this operation', 'grand-media'));
                    return $out;
                }
                $args = array();
                if ($edit_term) {
                    if (!current_user_can('gmedia_edit_others_media')) {
                        $error[] = __('You are not allowed to edit others media', 'grand-media');
                        break;
                    }
                    $term['name'] = trim($term['name']);
                    $term['term_id'] = intval($term['term_id']);
                    if ($term['name'] && !$gmCore->is_digit($term['name'])) {
                        if ($term_id = $gmDB->term_exists($term['term_id'], $taxonomy)) {
                            $db_term = $gmDB->get_term($term_id);
                            if ($db_term->name == $term['name'] || $db_term->name != $term['name'] && !$gmDB->term_exists($term['name'], $taxonomy)) {
                                $term_id = $gmDB->update_term($term['term_id'], $term);
                                if (is_wp_error($term_id)) {
                                    $error[] = $term_id->get_error_message();
                                    $term_id = 0;
                                } else {
                                    $alert[] = sprintf(__("Category %d successfuly updated", 'grand-media'), $term_id);
                                    $alert_info['terms'][$term_id] = $term['name'];
                                    if (1 < $logic) {
                                        $args['include'][] = $term_id;
                                    }
                                    $term_meta = array();
                                    if (isset($term['_orderby'])) {
                                        $term_meta['_orderby'] = $term['_orderby'];
                                    } elseif (isset($term['orderby'])) {
                                        $term_meta['_orderby'] = $term['orderby'];
                                    }
                                    if (isset($term['_order'])) {
                                        $term_meta['_order'] = $term['_order'];
                                    } elseif (isset($term['order'])) {
                                        $term_meta['_order'] = $term['order'];
                                    }
                                    foreach ($term_meta as $key => $value) {
                                        $gmDB->update_metadata('gmedia_term', $term_id, $key, $value);
                                    }
                                }
                            } else {
                                $error[] = __('A term with the name provided already exists', 'grand-media');
                                $error_info['terms'][$term_id] = $term['name'];
                            }
                        } else {
                            $error[] = __("A term with the id provided does not exists", 'grand-media');
                        }
                    } else {
                        $error[] = __("Term name can't be only digits or empty", 'grand-media');
                    }
                } else {
                    $terms = array_filter(array_map('trim', explode(',', $term['name'])));
                    $terms_added = 0;
                    $terms_qty = count($terms);
                    foreach ($terms as $term_name) {
                        if ($gmCore->is_digit($term_name)) {
                            continue;
                        }
                        if (!($term_id = $gmDB->term_exists($term_name, $taxonomy))) {
                            $term_id = $gmDB->insert_term($term_name, $taxonomy);
                            if (is_wp_error($term_id)) {
                                $error[] = $term_id->get_error_message();
                                $term_id = 0;
                            } else {
                                $alert['category_add'] = sprintf(__('%d of %d categories successfuly added', 'grand-media'), ++$terms_added, $terms_qty);
                                $alert_info['terms'][$term_id] = $term_name;
                            }
                        } else {
                            $alert['category_add'] = __('Some of provided categories are already exists', 'grand-media');
                            $alert_info['terms'][$term_id] = $term_name;
                        }
                    }
                }
                if (1 < $logic && $edit_term && $term_id) {
                    $lib_data = array('category__in' => array($term_id), 'admin' => 1);
                    if (!empty($data['per_page'])) {
                        $lib_data['per_page'] = $data['per_page'];
                    }
                    $out = gmedia_ios_app_processor('library', $lib_data);
                } else {
                    $out = gmedia_ios_app_library_data(array('filter', $taxonomy), $args);
                }
            } elseif ('gmedia_tag' == $taxonomy) {
                if (!current_user_can('gmedia_tag_manage')) {
                    $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'grand-media'), 'message' => __('You have no permission to do this operation', 'grand-media'));
                    return $out;
                }
                $args = array();
                if ($edit_term) {
                    if (!current_user_can('gmedia_edit_others_media')) {
                        $error[] = __('You are not allowed to edit others media', 'grand-media');
                        break;
                    }
                    $term['name'] = trim($term['name']);
                    $term['term_id'] = intval($term['term_id']);
                    if ($term['name'] && !$gmCore->is_digit($term['name'])) {
                        if ($term_id = $gmDB->term_exists($term['term_id'], $taxonomy)) {
                            if (!$gmDB->term_exists($term['name'], $taxonomy)) {
                                $term_id = $gmDB->update_term($term['term_id'], $term);
                                if (is_wp_error($term_id)) {
                                    $error[] = $term_id->get_error_message();
                                    $term_id = 0;
                                } else {
                                    $alert[] = sprintf(__("Tag %d successfuly updated", 'grand-media'), $term_id);
                                    $alert_info['terms'][$term_id] = $term['name'];
                                }
                            } else {
                                $error[] = __('A term with the name provided already exists', 'grand-media');
                                $error_info['terms'][$term_id] = $term['name'];
                            }
                        } else {
                            $error[] = __("A term with the id provided does not exists", 'grand-media');
                        }
                    } else {
                        $error[] = __("Term name can't be only digits or empty", 'grand-media');
                    }
                } else {
                    $terms = array_filter(array_map('trim', explode(',', $term['name'])));
                    $terms_added = 0;
                    $terms_qty = count($terms);
                    foreach ($terms as $term_name) {
                        if ($gmCore->is_digit($term_name)) {
                            continue;
                        }
                        if (!($term_id = $gmDB->term_exists($term_name, $taxonomy))) {
                            $term_id = $gmDB->insert_term($term_name, $taxonomy);
                            if (is_wp_error($term_id)) {
                                $error[] = $term_id->get_error_message();
                                $term_id = 0;
                            } else {
                                $alert['tag_add'] = sprintf(__('%d of %d tags successfuly added', 'grand-media'), ++$terms_added, $terms_qty);
                                $alert_info['terms'][$term_id] = $term_name;
                            }
                        } else {
                            $alert['tag_add'] = __('Some of provided tags are already exists', 'grand-media');
                            $alert_info['terms'][$term_id] = $term_name;
                        }
                    }
                }
                if (1 < $logic && $edit_term && $term_id) {
                    $lib_data = array('tag__in' => array($term_id), 'admin' => 1);
                    if (!empty($data['per_page'])) {
                        $lib_data['per_page'] = $data['per_page'];
                    }
                    $out = gmedia_ios_app_processor('library', $lib_data);
                } else {
                    $out = gmedia_ios_app_library_data(array('filter', $taxonomy), $args);
                }
            }
            break;
        default:
            break;
    }
    if (!empty($error)) {
        $out['error'] = array('code' => $action, 'title' => 'ERROR', 'message' => implode("\n", $error));
        if (!empty($error_info)) {
            $out['error'] = array_merge($out['error'], $error_info);
        }
    }
    if (!empty($alert)) {
        $out['alert'] = array('title' => 'Success', 'message' => implode("\n", $alert));
        if (!empty($alert_info)) {
            $out['alert'] = array_merge($out['alert'], $alert_info);
        }
    }
    return $out;
}
Beispiel #2
0
/**
 * @param      $action
 * @param      $data
 *
 * @param bool $filter
 *
 * @return array
 */
function gmedia_ios_app_processor($action, $data, $filter = true)
{
    global $gmCore, $gmDB, $gmGallery, $user_ID;
    $out = array();
    $error = array();
    $alert = array();
    $data = (array) $data;
    switch ($action) {
        case 'do_library':
            if (!isset($data['action'])) {
                return $out;
            }
            switch ($data['action']) {
                case 'add_media':
                    if (!current_user_can('gmedia_upload')) {
                        $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'gmLang'), 'message' => __('You have no permission to do this operation', 'gmLang'));
                        return $out;
                    }
                    usleep(10);
                    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
                        $file_name = $_FILES['userfile']['name'];
                        $file_tmp = $_FILES['userfile']['tmp_name'];
                    } else {
                        $error[] = __("Failed to move uploaded file.", 'gmLang');
                        break;
                    }
                    $fileinfo = $gmCore->fileinfo($file_name);
                    if (false === $fileinfo) {
                        break;
                    }
                    $gmedia = (array) $data['item'];
                    if (!current_user_can('gmedia_terms')) {
                        unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                    } else {
                        if (!empty($gmedia['categories'])) {
                            $cat = $gmedia['categories'][0]->name;
                            $gmedia['terms']['gmedia_category'] = $cat;
                        }
                        if (empty($gmedia['albums'])) {
                            $gmedia['terms']['gmedia_album'] = '';
                        } else {
                            $alb = isset($gmedia['albums'][0]->term_id) ? $gmedia['albums'][0]->term_id : $gmedia['albums'][0]->name;
                            $gmedia['terms']['gmedia_album'] = $alb;
                        }
                        if (empty($gmedia['tags'])) {
                            $gmedia['terms']['gmedia_tag'] = '';
                        } else {
                            $tags = array();
                            foreach ($gmedia['tags'] as $tag) {
                                $tags[] = isset($tag->term_id) ? $tag->term_id : $tag->name;
                            }
                            $gmedia['terms']['gmedia_tag'] = implode(',', $tags);
                        }
                        unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                    }
                    /*if(isset($gmedia['terms'])){
                    			$post_data = array('terms' => $gmedia['terms']);
                    		} else{
                    			$post_data = array();
                    		}*/
                    $return = $gmCore->gmedia_upload_handler($file_tmp, $fileinfo, 'multipart', $gmedia);
                    if (isset($return['error'])) {
                        $error[] = $return['error']['message'];
                    } else {
                        $alert[] = $return['success']['message'];
                    }
                    break;
                case 'update_media':
                    $gmedia = (array) $data['item'];
                    if (!empty($gmedia['ID'])) {
                        $item = $gmDB->get_gmedia($gmedia['ID']);
                        $gmedia['modified'] = current_time('mysql');
                        $gmedia['mime_type'] = $item->mime_type;
                        $gmedia['gmuid'] = $item->gmuid;
                        if (!current_user_can('gmedia_delete_others_media')) {
                            $gmedia['author'] = $item->author;
                        }
                        if (!current_user_can('gmedia_terms')) {
                            unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                        } else {
                            if (!empty($gmedia['categories'])) {
                                $cat = $gmedia['categories'][0]->name;
                                $gmedia['terms']['gmedia_category'] = $cat;
                            }
                            if (empty($gmedia['albums'])) {
                                $gmedia['terms']['gmedia_album'] = '';
                            } else {
                                if (isset($gmedia['albums'][0]->term_id)) {
                                    $gmedia['terms']['gmedia_album'] = $gmedia['albums'][0]->term_id;
                                } elseif (current_user_can('gmedia_album_manage')) {
                                    $gmedia['terms']['gmedia_album'] = $gmedia['albums'][0]->name;
                                }
                            }
                            if (empty($gmedia['tags'])) {
                                $gmedia['terms']['gmedia_tag'] = '';
                            } else {
                                $tags = array();
                                foreach ($gmedia['tags'] as $tag) {
                                    if (isset($tag->term_id)) {
                                        $tags[] = $tag->term_id;
                                    } elseif (current_user_can('gmedia_tag_manage')) {
                                        $tags[] = $tag->name;
                                    }
                                }
                                $gmedia['terms']['gmedia_tag'] = implode(',', $tags);
                            }
                            unset($gmedia['categories'], $gmedia['albums'], $gmedia['tags']);
                        }
                        if ((int) $item->author == get_current_user_id()) {
                            if (!current_user_can('gmedia_edit_media')) {
                                $alert[] = __('You are not allowed to edit media', 'gmLang');
                            }
                        } else {
                            if (!current_user_can('gmedia_edit_others_media')) {
                                $alert[] = __('You are not allowed to edit others media', 'gmLang');
                            }
                        }
                        if (empty($alert)) {
                            $gmDB->insert_gmedia($gmedia);
                        } else {
                            if (current_user_can('gmedia_terms') && count($gmedia['terms'])) {
                                foreach ($gmedia['terms'] as $tax => $terms) {
                                    $result = $gmDB->set_gmedia_terms($gmedia['ID'], $terms, $tax, $append = 0);
                                    if (is_wp_error($result)) {
                                        $error[] = $result->get_error_message();
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 'assign_category':
                    if (!current_user_can('gmedia_terms')) {
                        $error[] = __('You are not allowed to manage categories', 'gmLang');
                        break;
                    }
                    $term = $data['assign_category'][0];
                    if (false === $term) {
                        break;
                    }
                    $count = count($data['selected']);
                    if ('0' == $term) {
                        foreach ($data['selected'] as $item) {
                            $gmDB->delete_gmedia_term_relationships($item, 'gmedia_category');
                        }
                        $alert[] = sprintf(__('%d items updated with "Uncategorized"', 'gmLang'), $count);
                    } else {
                        foreach ($data['selected'] as $item) {
                            $result = $gmDB->set_gmedia_terms($item, $term, 'gmedia_category', $append = 0);
                            if (is_wp_error($result)) {
                                $error[] = $result->get_error_message();
                                $count--;
                            } elseif (!$result) {
                                $count--;
                            }
                        }
                        if (isset($gmGallery->options['taxonomies']['gmedia_category'][$term])) {
                            $cat_name = $gmGallery->options['taxonomies']['gmedia_category'][$term];
                            $alert[] = sprintf(__("Category `%s` assigned to %d images.", 'gmLang'), esc_html($cat_name), $count);
                        } else {
                            $error[] = sprintf(__("Category `%s` can't be assigned.", 'gmLang'), $term);
                        }
                    }
                    break;
                case 'assign_album':
                    if (!current_user_can('gmedia_terms')) {
                        $error[] = __('You are not allowed to manage albums', 'gmLang');
                    }
                    $term = $data['assign_album'][0];
                    $count = count($data['selected']);
                    if ('0' == $term) {
                        foreach ($data['selected'] as $item) {
                            $gmDB->delete_gmedia_term_relationships($item, 'gmedia_album');
                        }
                        $alert[] = sprintf(__('%d items updated with "No Album"', 'gmLang'), $count);
                    } else {
                        foreach ($data['selected'] as $item) {
                            $result = $gmDB->set_gmedia_terms($item, $term, 'gmedia_album', $append = 0);
                            if (is_wp_error($result)) {
                                $error[] = $result->get_error_message();
                                $count--;
                            } elseif (!$result) {
                                $count--;
                            }
                        }
                        if ($gmCore->is_digit($term)) {
                            $alb_name = $gmDB->get_alb_name($term);
                        } else {
                            $alb_name = $term;
                        }
                        $alert[] = sprintf(__('Album `%s` assigned to %d items', 'gmLang'), esc_html($alb_name), $count);
                    }
                    break;
                case 'add_tags':
                    if (!current_user_can('gmedia_terms')) {
                        $error[] = __('You are not allowed manage tags', 'gmLang');
                        break;
                    }
                    if (empty($data['add_tags'])) {
                        $error[] = __('No tags provided', 'gmLang');
                        break;
                    }
                    $term = $data['add_tags'];
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $result = $gmDB->set_gmedia_terms($item, $term, 'gmedia_tag', $append = 1);
                        if (is_wp_error($result)) {
                            $error[] = $result->get_error_message();
                            $count--;
                        } elseif (!$result) {
                            $count--;
                        }
                    }
                    $alert[] = sprintf(__('%d tags added to %d items', 'gmLang'), count($term), $count);
                    break;
                case 'delete_tags':
                    if (!current_user_can('gmedia_delete_others_media')) {
                        $error[] = __('You are not allowed to delete others media', 'gmLang');
                        break;
                    }
                    if (empty($data['delete_tags'])) {
                        $error[] = __('No tags provided', 'gmLang');
                        break;
                    }
                    $term = array_map('intval', $data['delete_tags']);
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $result = $gmDB->set_gmedia_terms($item, $term, 'gmedia_tag', $append = -1);
                        if (is_wp_error($result)) {
                            $error[] = $result->get_error_message();
                            $count--;
                        } elseif (!$result) {
                            $count--;
                        }
                    }
                    $alert[] = sprintf(__('%d tags deleted from %d items', 'gmLang'), count($term), $count);
                    break;
                case 'delete':
                    if (!current_user_can('gmedia_delete_media')) {
                        $error[] = __('You are not allowed to delete this post.');
                        break;
                    }
                    $count = count($data['selected']);
                    foreach ($data['selected'] as $item) {
                        $gm_item = $gmDB->get_gmedia($item);
                        if ((int) $gm_item->author != $user_ID && !current_user_can('gmedia_delete_others_media')) {
                            $error[] = "#{$item}: " . __('You are not allowed to delete media others media', 'gmLang');
                            continue;
                        }
                        if (!$gmDB->delete_gmedia((int) $item)) {
                            $error[] = "#{$item}: " . __('Error in deleting...', 'gmLang');
                            $count--;
                        }
                    }
                    if ($count) {
                        $alert[] = sprintf(__('%d items deleted successfuly', 'gmLang'), $count);
                    }
                    break;
            }
            $filter = gmedia_ios_app_library_data(array('filter', 'gmedia_category', 'gmedia_album', 'gmedia_tag'));
            $out = array_merge($out, $filter);
            break;
        case 'library':
            $ep = $gmGallery->options['endpoint'];
            if (get_option('permalink_structure')) {
                $share_link = home_url(urlencode($ep) . '/$2/$1');
            } else {
                $share_link = add_query_arg(array("{$ep}" => '$1', 't' => '$2'), home_url('index.php'));
            }
            $filter = $filter ? gmedia_ios_app_library_data(array('filter')) : array();
            $args = array('mime_type' => 'image/*', 'orderby' => 'ID', 'order' => 'DESC', 'per_page' => 100, 'page' => 1, 'tag__in' => null, 'category__in' => null, 'album__in' => null, 'author' => 0, 'status' => null);
            $data = wp_parse_args($data, $args);
            $false_out = array_merge($filter, array('properties' => array('request' => isset($data['request']) ? $data['request'] : null), 'data' => array()));
            $is_collection = !(empty($data['album__in']) && empty($data['tag__in']) && empty($data['category__in']));
            $is_admin = isset($data['admin']) ? intval($data['admin']) : 0;
            if (!is_user_logged_in()) {
                $data['status'] = array('public');
                if ($is_collection) {
                    if (!empty($data['album__in'])) {
                        $alb = $gmDB->get_term((int) $data['album__in'][0], 'gmedia_album');
                        if (!(isset($alb->status) && 'public' == $alb->status)) {
                            $out = $false_out;
                            break;
                        }
                    }
                } else {
                    $out = $false_out;
                    break;
                }
            } else {
                if ($is_admin && !current_user_can('gmedia_library')) {
                    $out = $false_out;
                    break;
                }
                if ($is_admin && !current_user_can('gmedia_show_others_media')) {
                    $data['author'] = $user_ID;
                }
            }
            $gmedias = $gmDB->get_gmedias($data);
            foreach ($gmedias as $i => $item) {
                if (!$is_admin && 'draft' == $item->status && (int) $user_ID != (int) $item->author) {
                    //if (!current_user_can('gmedia_edit_others_media')){
                    unset($gmedias[$i]);
                    continue;
                    //}
                }
                $author_id = $item->author;
                $authordata = get_userdata($author_id);
                if ($authordata) {
                    $display_name = $authordata->display_name;
                    $first_name = $authordata->first_name;
                    $last_name = $authordata->last_name;
                } else {
                    $display_name = __('Deleted User', 'gmLang');
                    $first_name = '';
                    $last_name = '';
                }
                $gmedias[$i]->user = array('id' => $author_id, 'displayname' => $display_name, 'firstname' => $first_name, 'last_name' => $last_name);
                $meta = $gmDB->get_metadata('gmedia', $item->ID);
                $_metadata = maybe_unserialize($meta['_metadata'][0]);
                $type = explode('/', $item->mime_type);
                $item_url = $gmCore->upload['url'] . '/' . $gmGallery->options['folder'][$type[0]] . '/' . $item->gmuid;
                $gmedias[$i]->url = $item_url;
                $terms = $gmDB->get_the_gmedia_terms($item->ID, 'gmedia_tag');
                $tags = array();
                if ($terms) {
                    $terms = array_values((array) $terms);
                    foreach ($terms as $term) {
                        $tags[] = array('term_id' => $term->term_id, 'name' => $term->name);
                    }
                }
                $gmedias[$i]->tags = $tags;
                $terms = $gmDB->get_the_gmedia_terms($item->ID, 'gmedia_album');
                $albums = array();
                if ($terms) {
                    $terms = array_values((array) $terms);
                    foreach ($terms as $term) {
                        $albums[] = array('term_id' => $term->term_id, 'name' => $term->name, 'status' => $term->status);
                    }
                }
                $gmedias[$i]->albums = $albums;
                if ('image' == $type[0]) {
                    $terms = $gmDB->get_the_gmedia_terms($item->ID, 'gmedia_category');
                    $categories = array();
                    if ($terms) {
                        $terms = array_values((array) $terms);
                        foreach ($terms as $term) {
                            $categories[] = array('term_id' => $term->term_id, 'name' => $term->name);
                        }
                    }
                    $gmedias[$i]->categories = $categories;
                    $gmedias[$i]->meta = array('thumb' => $_metadata['thumb'], 'web' => $_metadata['web'], 'original' => $_metadata['original']);
                    $gmedias[$i]->meta['thumb']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image_thumb']}/{$item->gmuid}";
                    $gmedias[$i]->meta['web']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image']}/{$item->gmuid}";
                    $gmedias[$i]->meta['original']['link'] = "{$gmCore->upload['url']}/{$gmGallery->options['folder']['image_original']}/{$item->gmuid}";
                    $gmedias[$i]->meta['views'] = 0;
                    $gmedias[$i]->meta['likes'] = 0;
                    if (isset($meta['views'][0])) {
                        $gmedias[$i]->meta['views'] = $meta['views'][0];
                    }
                    if (isset($meta['likes'][0])) {
                        $gmedias[$i]->meta['likes'] = $meta['likes'][0];
                    }
                    if (isset($_metadata['image_info'])) {
                        $gmedias[$i]->meta['data'] = $_metadata['image_info'];
                    }
                } else {
                    $gmedias[$i]->meta = array('thumb' => array('link' => $gmCore->gm_get_media_image($item, 'thumb'), 'width' => 300, 'height' => 300));
                    if (!empty($_metadata)) {
                        $gmedias[$i]->meta['data'] = $_metadata;
                    }
                }
                if (isset($meta['rating'][0])) {
                    $gmedias[$i]->meta['rating'] = maybe_unserialize($meta['rating'][0]);
                }
                $item_name = $item->title ? $item->title : pathinfo($item->gmuid, PATHINFO_FILENAME);
                $gmedia_hashid = gmedia_hash_id_encode($item->ID, 'single');
                $gmedias[$i]->sharelink = str_replace(array('$1', '$2'), array(urlencode($gmedia_hashid), 's'), $share_link);
            }
            $out = array_merge($filter, array('properties' => array('request' => isset($data['request']) ? $data['request'] : null, 'total_pages' => $gmDB->pages, 'current_page' => $gmDB->openPage, 'items_count' => $gmDB->gmediaCount), 'data' => array_values($gmedias)));
            break;
        case 'delete_term':
            $taxonomy = $data['taxonomy'];
            if (!empty($data['items'])) {
                if (!current_user_can('gmedia_terms_delete')) {
                    $error[] = __('You have no permission to do this operation', 'gmLang');
                    break;
                }
                $count = count($data['items']);
                foreach ($data['items'] as $item) {
                    if (!current_user_can('gmedia_edit_others_media')) {
                        if ('gmedia_album' == $taxonomy) {
                            $term = $gmDB->get_term($item, $taxonomy);
                            if ((int) $term->global != (int) $user_ID) {
                                $error['delete_album'] = __('You are not allowed to edit others media', 'gmLang');
                                $count--;
                                continue;
                            }
                        } else {
                            $error[] = __('You are not allowed to edit others media', 'gmLang');
                            $count--;
                            continue;
                        }
                    }
                    $delete = $gmDB->delete_term($item, $taxonomy);
                    if (is_wp_error($delete)) {
                        $error[] = $delete->get_error_message();
                        $count--;
                    }
                }
                if ($count) {
                    $alert[] = sprintf(__('%d items deleted successfuly', 'gmLang'), $count);
                }
            }
            $out = gmedia_ios_app_library_data(array('filter', $taxonomy));
            break;
        case 'add_term':
            $taxonomy = $data['taxonomy'];
            $edit_term = isset($data['term_id']) ? (int) $data['term_id'] : 0;
            $term = $data;
            if ('gmedia_album' == $taxonomy) {
                if (!current_user_can('gmedia_album_manage')) {
                    $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'gmLang'), 'message' => __('You have no permission to do this operation', 'gmLang'));
                    return $out;
                }
                do {
                    $term['name'] = trim($term['name']);
                    if (empty($term['name'])) {
                        $error[] = __('Term Name is not specified', 'gmLang');
                        break;
                    }
                    if ($gmCore->is_digit($term['name'])) {
                        $error[] = __("Term Name can't be only digits", 'gmLang');
                        break;
                    }
                    if ($edit_term && !$gmDB->term_exists($edit_term, $taxonomy)) {
                        $error[] = __('A term with the id provided do not exists', 'gmLang');
                        $edit_term = false;
                    }
                    if ($term_id = $gmDB->term_exists($term['name'], $taxonomy)) {
                        if ($term_id != $edit_term) {
                            $error[] = __('A term with the name provided already exists', 'gmLang');
                            break;
                        }
                    }
                    if ($edit_term) {
                        $_term = $gmDB->get_term($edit_term, $taxonomy);
                        if ((int) $_term->global != (int) $user_ID && !current_user_can('gmedia_edit_others_media')) {
                            $error[] = __('You are not allowed to edit others media', 'gmLang');
                            break;
                        }
                        $term_id = $gmDB->update_term($edit_term, $taxonomy, $term);
                    } else {
                        if (!current_user_can('gmedia_edit_others_media')) {
                            $term['global'] = intval($user_ID);
                        }
                        $term_id = $gmDB->insert_term($term['name'], $taxonomy, $term);
                    }
                    if (is_wp_error($term_id)) {
                        $error[] = $term_id->get_error_message();
                        break;
                    }
                    $term_meta = array('orderby' => $term['orderby'], 'order' => $term['order']);
                    foreach ($term_meta as $key => $value) {
                        if ($edit_term) {
                            $gmDB->update_metadata('gmedia_term', $term_id, $key, $value);
                        } else {
                            $gmDB->add_metadata('gmedia_term', $term_id, $key, $value);
                        }
                    }
                    $alert[] = sprintf(__('Album `%s` successfuly saved', 'gmLang'), $term['name']);
                } while (0);
                $out = gmedia_ios_app_library_data(array('filter', $taxonomy));
            } elseif ('gmedia_tag' == $taxonomy) {
                if (!current_user_can('gmedia_tag_manage')) {
                    $out['error'] = array('code' => 'nocapability', 'title' => __("You can't do this", 'gmLang'), 'message' => __('You have no permission to do this operation', 'gmLang'));
                    return $out;
                }
                if ($edit_term) {
                    if (!current_user_can('gmedia_edit_others_media')) {
                        $error[] = __('You are not allowed to edit others media', 'gmLang');
                        break;
                    }
                    $term['name'] = trim($term['name']);
                    $term['term_id'] = intval($term['term_id']);
                    if ($term['name'] && !$gmCore->is_digit($term['name'])) {
                        if ($term_id = $gmDB->term_exists($term['term_id'], $taxonomy)) {
                            if (!$gmDB->term_exists($term['name'], $taxonomy)) {
                                $term_id = $gmDB->update_term($term['term_id'], $taxonomy, $term);
                                if (is_wp_error($term_id)) {
                                    $error[] = $term_id->get_error_message();
                                } else {
                                    $alert[] = sprintf(__("Tag %d successfuly updated", 'gmLang'), $term_id);
                                }
                            } else {
                                $error[] = __('A term with the name provided already exists', 'gmLang');
                            }
                        } else {
                            $error[] = __("A term with the id provided do not exists", 'gmLang');
                        }
                    } else {
                        $error[] = __("Term name can't be only digits or empty", 'gmLang');
                    }
                } else {
                    $terms = array_filter(array_map('trim', explode(',', $term['name'])));
                    $terms_added = 0;
                    $terms_qty = count($terms);
                    foreach ($terms as $term_name) {
                        if ($gmCore->is_digit($term_name)) {
                            continue;
                        }
                        if (!$gmDB->term_exists($term_name, $taxonomy)) {
                            $term_id = $gmDB->insert_term($term_name, $taxonomy);
                            if (is_wp_error($term_id)) {
                                $error[] = $term_id->get_error_message();
                            } else {
                                $alert['tag_add'] = sprintf(__('%d of %d tags successfuly added', 'gmLang'), ++$terms_added, $terms_qty);
                            }
                        } else {
                            $alert['tag_add'] = __('Some of provided tags are already exists', 'gmLang');
                        }
                    }
                }
                $out = gmedia_ios_app_library_data(array('filter', $taxonomy));
            }
            break;
        default:
            break;
    }
    if (!empty($error)) {
        $out['error'] = array('code' => $action, 'title' => 'ERROR', 'message' => implode("\n", $error));
    }
    if (!empty($alert)) {
        $out['alert'] = array('title' => 'Success', 'message' => implode("\n", $alert));
    }
    return $out;
}