/**
 * Get all the valid User Access statuses
 * 
 * @param type $component_type
 * @param type $component_id
 * @return array of status like array( 'public', 'private', 'friends' ) etc
 */
function mpp_get_accessible_statuses($component_type, $component_id, $user_id = false)
{
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    $all_status = mpp_get_active_statuses();
    $allowed_status = array();
    foreach ($all_status as $status => $status_details) {
        if (!empty($status_details->callback) && is_callable($status_details->callback)) {
            //should I use cal_user_func instead? have seenh that as inefficient though!
            $func = $status_details->callback;
            if ($func($component_type, $component_id, $user_id)) {
                $allowed_status[] = $status;
            }
        }
    }
    //shoudl we check for empty and mark invalid?
    //return the filtered, allowed status for the current context
    return apply_filters("mpp_get_accessible_" . strtolower($component_type) . "_gallery_statuses", $allowed_status, $component_id, $user_id);
}
function mpp_get_editable_statuses($type = null, $component = null)
{
    if (!$type || $type == 'active') {
        $statuses = mpp_get_active_statuses();
    } else {
        $statuses = mpp_get_registered_statuses();
    }
    //if a component is given, filter the status
    if ($component) {
        $all_statuses = (array) $statuses;
        $statuses = array();
        foreach ($all_statuses as $status => $status_object) {
            if (mpp_component_supports_status($component, $status)) {
                $statuses[$status] = $status_object;
            }
        }
    }
    return apply_filters('mpp_get_editable_statuses', $statuses);
}
 public function setup_nav($main = array(), $sub = array())
 {
     $bp = buddypress();
     $component = 'members';
     $component_id = mpp_get_current_component_id();
     if (!mpp_is_enabled($component, $component_id)) {
         //allow to disable user galleries in case they don't want it
         return false;
     }
     $view_helper = MPP_Gallery_Screens::get_instance();
     // Add 'Gallery' to the user's main navigation
     $main_nav = array('name' => sprintf(__('Gallery <span>%d</span>', 'mediapress'), mpp_get_total_gallery_for_user()), 'slug' => $this->slug, 'position' => 86, 'screen_function' => array($view_helper, 'user_galleries'), 'default_subnav_slug' => 'my-galleries', 'item_css_id' => $this->id);
     if (bp_is_user()) {
         $user_domain = bp_displayed_user_domain();
     } else {
         $user_domain = bp_loggedin_user_domain();
     }
     $gallery_link = trailingslashit($user_domain . $this->slug);
     //with a trailing slash
     // Add the My Gallery nav item
     $sub_nav[] = array('name' => __('My Gallery', 'mediapress'), 'slug' => 'my-galleries', 'parent_url' => $gallery_link, 'parent_slug' => $this->slug, 'screen_function' => array($view_helper, 'my_galleries'), 'position' => 10, 'item_css_id' => 'gallery-my-gallery');
     if (mpp_user_can_create_gallery($component, get_current_user_id())) {
         // Add the Create gallery link to gallery nav
         $sub_nav[] = array('name' => __('Create a Gallery', 'mediapress'), 'slug' => 'create', 'parent_url' => $gallery_link, 'parent_slug' => $this->slug, 'screen_function' => array($view_helper, 'my_galleries'), 'user_has_access' => bp_is_my_profile(), 'position' => 20);
     }
     if (mpp_component_has_type_filters_enabled($component, $component_id)) {
         $i = 10;
         $supported_types = mpp_component_get_supported_types($component);
         foreach ($supported_types as $type) {
             if (!mpp_is_active_type($type)) {
                 continue;
             }
             $type_object = mpp_get_type_object($type);
             $sub_nav[] = array('name' => $type_object->label, 'slug' => 'type/' . $type, 'parent_url' => $gallery_link, 'parent_slug' => $this->slug, 'screen_function' => array($view_helper, 'my_galleries'), 'position' => 20 + $i);
             $i = $i + 10;
             //increment the position
         }
     }
     // Add the Upload link to gallery nav
     /*$sub_nav[] = array(
           'name'				=> __( 'Upload', 'mediapress'),
           'slug'				=> 'upload',
           'parent_url'		=> $gallery_link,
           'parent_slug'		=> $this->slug,
           'screen_function'	=> array( $view_helper, 'upload_media' ),
           'user_has_access'	=> bp_is_my_profile(),
           'position'			=> 30
       );*/
     parent::setup_nav($main_nav, $sub_nav);
     //disallow these names in various lists
     //we have yet to implement it
     $this->forbidden_names = apply_filters('mpp_forbidden_names', array('gallery', 'galleries', 'my-gallery', 'create', 'delete', 'upload', 'add', 'edit', 'admin', 'request', 'upload', 'tags', 'audio', 'video', 'photo'));
     //use this to extend the valid status
     $this->valid_status = apply_filters('mpp_valid_gallery_status', array_keys(mpp_get_active_statuses()));
     do_action('mpp_setup_nav');
     // $bp->gallery->current_gallery->user_has_access
 }
 /**
  * Map gallery parameters to wp_query native parameters
  * 
  * @param type $args
  * @return string
  */
 public function build_params($args)
 {
     $defaults = array('type' => array_keys(mpp_get_active_types()), 'id' => false, 'in' => false, 'exclude' => false, 'slug' => false, 'status' => array_keys(mpp_get_active_statuses()), 'component' => array_keys(mpp_get_active_components()), 'component_id' => false, 'per_page' => mpp_get_option('galleries_per_page'), 'offset' => false, 'page' => false, 'nopaging' => false, 'order' => 'DESC', 'orderby' => 'date', 'user_id' => false, 'include_users' => false, 'exclude_users' => false, 'user_name' => false, 'scope' => false, 'search_terms' => '', 'year' => false, 'month' => false, 'week' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'yearmonth' => false, 'fields' => false);
     //build params for WP_Query
     $r = wp_parse_args($args, $defaults);
     extract($r, EXTR_SKIP);
     //build the wp_query args
     $wp_query_args = array('post_type' => mpp_get_gallery_post_type(), 'post_status' => 'any', 'p' => $id, 'post__in' => $in, 'post__not_in' => $exclude, 'name' => $slug, 'posts_per_page' => $per_page, 'paged' => $page, 'offset' => $offset, 'nopaging' => $nopaging, 'author' => $user_id, 'author_name' => $user_name, 'author__in' => $include_users, 'author__not_in' => $exclude_users, 'year' => $year, 'monthnum' => $month, 'w' => $week, 'day' => $day, 'hour' => $hour, 'minute' => $minute, 'second' => $second, 'm' => $yearmonth, 'order' => $order, 'orderby' => $orderby, 's' => $search_terms, 'fields' => $fields, '_mpp_mapped_query' => true);
     $tax_query = array();
     $gmeta_query = array();
     //meta
     if (isset($meta_key) && $meta_key) {
         $wp_query_args['meta_key'] = $meta_key;
     }
     if (isset($meta_value)) {
         $wp_query_args['meta_value'] = $meta_value;
     }
     if (isset($meta_query)) {
         $gmeta_query = $meta_query;
     }
     //TODO: SCOPE
     //
     //we will need to build tax query/meta query
     //type, audio video etc
     //if type is given and it is valid gallery type
     //Pass one or more types
     //should we restrict to active types only here? I guss no, Insteacd the calling scope should take care of that
     if (!empty($type) && mpp_are_registered_types($type)) {
         $type = mpp_string_to_array($type);
         $type = array_map('mpp_underscore_it', $type);
         $tax_query[] = array('taxonomy' => mpp_get_type_taxname(), 'field' => 'slug', 'terms' => $type, 'operator' => 'IN');
     }
     //privacy
     //pass ne or more privacy level
     if (!empty($status) && mpp_are_registered_statuses($status)) {
         $status = mpp_string_to_array($status);
         $status = array_map('mpp_underscore_it', $status);
         $tax_query[] = array('taxonomy' => mpp_get_status_taxname(), 'field' => 'slug', 'terms' => $status, 'operator' => 'IN');
     }
     if (!empty($component) && mpp_are_registered_components($component)) {
         $component = mpp_string_to_array($component);
         $component = array_map('mpp_underscore_it', $component);
         $tax_query[] = array('taxonomy' => mpp_get_component_taxname(), 'field' => 'slug', 'terms' => $component, 'operator' => 'IN');
     }
     //done with the tax query
     if (count($tax_query) > 1) {
         $tax_query['relation'] = 'AND';
     }
     $wp_query_args['tax_query'] = $tax_query;
     // print_nice($wp_query_args);
     //meta query
     //now, for component
     if (!empty($component_id)) {
         $meta_compare = '=';
         if (is_array($component_id)) {
             $meta_compare = 'IN';
         }
         $gmeta_query[] = array('key' => '_mpp_component_id', 'value' => $component_id, 'compare' => $meta_compare, 'type' => 'UNSIGNED');
     }
     //reset meta query
     if (!empty($gmeta_query)) {
         $wp_query_args['meta_query'] = $gmeta_query;
     }
     // print_r($wp_query_args);
     return $wp_query_args;
 }
Example #5
0
/**
 *  Check if the list of provided statuses are enabled for gallery
 * 
 * The provided list could be comma separated like 'private,public' or array like array('private', public')
 * @param type $statuses
 * @return boolean
 * 
 */
function mpp_are_active_statuses($statuses)
{
    if (empty($statuses)) {
        return false;
    }
    //empty can not be valid statuses
    $statuses = mpp_string_to_array($statuses);
    $valid_statuses = mpp_get_active_statuses();
    $valid_statuses = array_keys($valid_statuses);
    //get the valid status keys as array
    $diff = array_diff($statuses, $valid_statuses);
    if (!empty($diff)) {
        //if there exists atleast one status which is not registered as valid
        return false;
    }
    return true;
    //yup valid
}
Example #6
0
 /**
  * Initialize the admin settings panel and fields
  * 
  */
 public function init()
 {
     //'mpp-settings' is used as page slug as well as option to store in the database
     $page = new MPP_Admin_Settings_Page('mpp-settings');
     //MPP_Admin_Page( 'mpp-settings' );
     //Add a panel to to the admin
     //A panel is a Tab and what coms under that tab
     $panel = $page->add_panel('general', _x('General', 'Admin settings panel title', 'mediapress'));
     //A panel can contain one or more sections. each sections or
     $section = $panel->add_section('component-settings', _x('Component Settings', 'Admin settings section title', 'mediapress'));
     $components_details = array();
     $components = mpp_get_registered_components();
     foreach ($components as $key => $component) {
         $components_details[$key] = $component->label;
     }
     $component_keys = array_keys($components_details);
     $default_components = array_combine($component_keys, $component_keys);
     $active_components = array_keys(mpp_get_active_components());
     if (!empty($active_components)) {
         $default_components = array_combine($active_components, $active_components);
     }
     $section->add_field(array('name' => 'active_components', 'label' => _x('Enable Galleries for?', 'Admin settings', 'mediapress'), 'type' => 'multicheck', 'options' => $components_details, 'default' => $default_components));
     //second section
     //
     //enabled status
     //status
     $available_media_stati = mpp_get_registered_statuses();
     $options = array();
     foreach ($available_media_stati as $key => $available_media_status) {
         $options[$key] = $available_media_status->get_label();
     }
     $panel->add_section('status-settings', _x('Privacy Settings', 'Admin settings section title', 'mediapress'))->add_field(array('name' => 'default_status', 'label' => _x('Default status for Gallery/Media', 'Admin settings', 'mediapress'), 'description' => _x('It will be used when we are not allowed to get the status from user', 'Admin settings', 'mediapress'), 'default' => mpp_get_default_status(), 'options' => $options, 'type' => 'select'));
     $section = $panel->get_section('status-settings');
     $registered_statuses = mpp_get_registered_statuses();
     $status_info = array();
     foreach ($registered_statuses as $key => $status) {
         $status_info[$key] = $status->label;
     }
     $active_statuses = array_keys(mpp_get_active_statuses());
     $status_keys = array_keys($status_info);
     $default_statuses = array_combine($status_keys, $status_keys);
     if (!empty($active_statuses)) {
         $default_statuses = array_combine($active_statuses, $active_statuses);
     }
     $section->add_field(array('name' => 'active_statuses', 'label' => _x('Enabled Media/Gallery Statuses', 'Admin settings', 'mediapress'), 'type' => 'multicheck', 'options' => $status_info, 'default' => $default_statuses));
     //3rd section
     //enabled type?
     //types
     $section = $panel->add_section('types-settings', _x('Media Type settings', 'Admin settings section title', 'mediapress'));
     $valid_types = mpp_get_registered_types();
     $options = array();
     $types_info = array();
     $extension_fields = array();
     foreach ($valid_types as $type => $type_object) {
         $types_info[$type] = $type_object->label;
         $extension_fields[] = array('id' => 'extensions-' . $type, 'name' => 'extensions', 'label' => sprintf(_x('Allowed extensions for %s', 'Settings page', 'mediapress'), $type), 'description' => sprintf(_x('Use comma separated list of file extensions for %s ', 'Settings page', 'mediapress '), $type), 'default' => join(',', (array) $type_object->get_registered_extensions()), 'type' => 'extensions', 'extra' => array('key' => $type, 'name' => 'extensions'));
     }
     $type_keys = array_keys($types_info);
     $default_types = array_combine($type_keys, $type_keys);
     $active_types = array_keys(mpp_get_active_types());
     if (!empty($active_types)) {
         $default_types = array_combine($active_types, $active_types);
     }
     $section->add_field(array('name' => 'active_types', 'label' => _x('Enabled Media/Gallery Types', 'Settings page', 'mediapress'), 'type' => 'multicheck', 'options' => $types_info, 'default' => $default_types));
     /*
     		$section->add_field( array(
     						'name'			=> 'allow_mixed_gallery',
     						'label'			=> __( 'Allow mixed Galleries?', 'mediapress' ),
     						'type'			=> 'radio',
     						'default'		=> 0,//10 MB
     						'options'		=> array(
     											1 => 'Yes',
     											0 => 'No',
     											
     										),
     						'description'	=> __( 'Please keep it disabled. It is not truly enabled at the moment', 'mediapress' )				
     						
     		) );*/
     $section->add_fields($extension_fields);
     //4th section
     //enabled storage
     //Storage section
     $panel->add_section('storage-settings', _x('Storage Settings', 'Settings page section title', 'mediapress'))->add_field(array('name' => 'mpp_upload_space', 'label' => _x('maximum Upload space per user(MB)?', 'Admin storage settings', 'mediapress'), 'type' => 'text', 'default' => 10))->add_field(array('name' => 'mpp_upload_space_groups', 'label' => _x('maximum Upload space per group(MB)?', 'Admin storage settings', 'mediapress'), 'type' => 'text', 'default' => 10))->add_field(array('name' => 'show_upload_quota', 'label' => _x('Show upload Quota?', 'Admin storage settings', 'mediapress'), 'type' => 'radio', 'default' => 0, 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $storage_methods = mpp_get_registered_storage_managers();
     $storage_methods = array_keys($storage_methods);
     $storage_method_options = array();
     foreach ($storage_methods as $storage_method) {
         $storage_method_options[$storage_method] = ucfirst($storage_method);
     }
     //->add_field();
     $panel->get_section('storage-settings')->add_field(array('name' => 'default_storage', 'label' => _x('Which should be marked as default storage?', 'Admin storage settings', 'mediapress'), 'default' => mpp_get_default_storage_method(), 'options' => $storage_method_options, 'type' => 'radio'));
     //5th section
     //activity settings
     $activity_section = $panel->add_section('activity-settings', _x('Activity Settings', 'Admin settings section title', 'mediapress'));
     $activity_section->add_field(array('name' => 'activity_upload', 'label' => _x('Allow Activity Upload?', 'Admin settings', 'mediapress'), 'desc' => _x('Allow users to uploading from Activity screen?', 'Admin settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $activity_options = array('create_gallery' => _x('New Gallery is created.', 'Admin settings', 'mediapress'), 'add_media' => _x('New Media added/uploaded.', 'Admin settings', 'mediapress'));
     $default_activities = array_keys($activity_options);
     $default_activities = array_combine($default_activities, $default_activities);
     $activity_section->add_field(array('name' => 'autopublish_activities', 'label' => _x('Automatically Publish to activity When?', 'Admin settings', 'mediapress'), 'type' => 'multicheck', 'options' => $activity_options, 'default' => $default_activities));
     //6th section
     //directory settings
     $panel->add_section('directory-settings', _x('Directory Settings', 'Admin settings section title', 'mediapress'))->add_field(array('name' => 'has_gallery_directory', 'label' => _x('Enable Gallery Directory?', 'Admin settings', 'mediapress'), 'desc' => _x('Create a page to list all galleries?', 'Admin settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))))->add_field(array('name' => 'has_media_directory', 'label' => _x('Enable Media directory?', 'Admin settings', 'mediapress'), 'desc' => _x('Create a page to list all photos, videos etc? Please keep it disabled for now )', 'Admin settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $panel->add_section('group-settings', _x('Group Settings', 'Admin settings section title', 'mediapress'))->add_field(array('name' => 'contributors_can_edit', 'label' => _x('Contributors can edit their own media?', 'Admin settings group section', 'mediapress'), 'type' => 'radio', 'default' => 1, 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))))->add_field(array('name' => 'contributors_can_delete', 'label' => _x('Contributors can delete their own media?', 'Admin settings group section', 'mediapress'), 'type' => 'radio', 'default' => 1, 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $panel->add_section('misc-settings', _x('Miscellaneous Settings', 'Admin settings section title', 'mediapress'))->add_field(array('name' => 'show_orphaned_media', 'label' => _x('Show orphaned media to the user?', 'Admin settings option', 'mediapress'), 'desc' => _x('Do you want to list the media if it was uploaded from activity but the activity was not published?', 'Admin settings', 'mediapress'), 'type' => 'radio', 'default' => 0, 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))))->add_field(array('name' => 'delete_orphaned_media', 'label' => _x('Delete orphaned media automatically?', 'Admin settings', 'mediapress'), 'desc' => _x('Do you want to delete the abandoned media uploade from activity?', 'Admin settings', 'mediapress'), 'type' => 'radio', 'default' => 1, 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $theme_panel = $page->add_panel('theming', _x('Theming', 'Admin settings theme panel tab title', 'mediapress'));
     $theme_panel->add_section('display-settings', _x('Display Settings ', 'Admin settings theme section title', 'mediapress'))->add_field(array('name' => 'galleries_per_page', 'label' => _x('How many galleries to list per page?', 'Admin theme settings', 'mediapress'), 'type' => 'text', 'default' => 12))->add_field(array('name' => 'media_per_page', 'label' => _x('How many Media per page?', 'Admin theme settings', 'mediapress'), 'type' => 'text', 'default' => 12))->add_field(array('name' => 'media_columns', 'label' => _x('How many media per row?', 'Admin theme settings', 'mediapress'), 'type' => 'text', 'default' => 4))->add_field(array('name' => 'gallery_columns', 'label' => _x('How many galleries per row?', 'Admin theme settings', 'mediapress'), 'type' => 'text', 'default' => 4));
     $theme_panel->add_section('audio-video', _x('Audio/Video specific settings', ' Admin theme section title', 'mediapress'))->add_field(array('name' => 'enable_audio_playlist', 'label' => _x('Enable Audio Playlist?', 'admin theme settings', 'mediapress'), 'description' => _x('Should an audio gallery be listed as a playlist?', 'admin theme settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))))->add_field(array('name' => 'enable_video_playlist', 'label' => _x('Enable Video Playlist?', 'admin theme settings', 'mediapress'), 'description' => _x('Should a video gallery be listed as a playlist?', 'admin theme settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $theme_panel->add_section('comments', _x('Comment Settings', 'Admin theme section title', 'mediapress'))->add_field(array('name' => 'enable_media_comment', 'label' => _x('Enable Commenting on single media?', 'admin theme comment settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))))->add_field(array('name' => 'enable_gallery_comment', 'label' => 'Enable Commenting on single Gallery?', 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     $theme_panel->add_section('lightbox', _x('Lightbox Settings', 'admin theme section title', 'mediapress'))->add_field(array('name' => 'load_lightbox', 'label' => _x('Load Lightbox javascript & css )?', 'Admin theme settings', 'mediapress'), 'description' => _x('Should we load the included lightbox script? Set no, if you are not using lightbox or want to use your own', 'Admin settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))))->add_field(array('name' => 'enable_activity_lightbox', 'label' => _x('Open Activity media in lightbox ?', 'Admin theme settings', 'mediapress'), 'description' => _x('If you set yes, the photos etc will be open in lightbox on activity screen.', 'Admin theme settings', 'mediapress'), 'default' => 1, 'type' => 'radio', 'options' => array(1 => _x('Yes', 'Admin settings option', 'mediapress'), 0 => _x('No', 'Admin settings option', 'mediapress'))));
     //add an empty addons panel to allow plugins to register any setting here
     //though a plugin can add a new panel, smaller plugins should use this panel instead
     $page->add_panel('addons', _x('Addons', 'Admin settings Addons panel tab title', 'mediapress'), _x('MediaPress Addon Settings', 'Addons panel description', 'mediapress'));
     //auto posting to activity on gallery upload?
     //should post after the whole gallery is uploaded or just after each media?
     $this->page = $page;
     mpp_admin()->set_page($this->page);
     do_action('mpp_admin_register_settings', $page);
     //allow enab
     $page->init();
 }
Example #7
0
/**
 *  Get total galleries|media based on other parameters
 * 
 * @param type $args {
 *  @type component string|array comma separated sting or array of components eg 'groups,members' or array('groups', 'members' )
 *  @type component_id int numeric component id (user id or group id)
 *  @type status string|array comma separated list or array of statuses e.g. 'public,private,friends' or array ( 'public', 'private', 'friends' )
 *  @type type   string|array comma separated list or array of media types e.g 'audio,video,photo' or array ( 'audio', 'video', 'photo' )
 * @param string $post_type
 * 
 * @return int total no of posts
 */
function mpp_get_object_count($args, $post_type)
{
    global $wpdb;
    $post_type_sql = '';
    $sql = array();
    $default = array('component' => '', 'component_id' => false, 'status' => '', 'type' => '', 'post_status' => 'publish');
    //if component is set to user, we can simply avoid component query
    //may be next iteration someday
    $args = wp_parse_args($args, $default);
    extract($args);
    if (!$status) {
        if ($component && $component_id) {
            $status = mpp_get_accessible_statuses($component, $component_id, get_current_user_id());
        } else {
            $status = array_keys(mpp_get_active_statuses());
        }
    }
    if (!$component) {
        $component = array_keys(mpp_get_active_components());
    }
    if (!$type) {
        $type = array_keys(mpp_get_active_types());
    }
    //do we have a component set
    if ($component) {
        $sql[] = mpp_get_tax_sql($component, mpp_get_component_taxname());
    }
    //do we have a component set
    if ($status) {
        $sql[] = mpp_get_tax_sql($status, mpp_get_status_taxname());
    }
    //for type, repeat it
    if ($type) {
        $sql[] = mpp_get_tax_sql($type, mpp_get_type_taxname());
    }
    //we need to find all the object ids which are present in these terms
    //since mysql does not have intersect clause and inner join will be causing too large data set
    //let us use another apprioach for now
    //in our case
    //theere are 3 taxonomies
    //so we will be looking for the objects appearing thrice
    $tax_object_sql = " (SELECT DISTINCT t.object_id FROM (" . join(" UNION ALL ", $sql) . ") AS t GROUP BY object_id HAVING count(*) >=3 )";
    $post_type_sql = $wpdb->prepare("SELECT COUNT( DISTINCT ID ) FROM {$wpdb->posts} WHERE post_type = %s AND post_status =%s", $post_type, $post_status);
    //if a user or group id is given
    if ($component_id) {
        $post_type_sql = $wpdb->prepare("SELECT COUNT( DISTINCT p.ID ) AS total FROM {$wpdb->posts} AS p INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id WHERE p.post_type= %s AND p.post_status = %s AND pm.meta_key=%s and pm.meta_value=%d", $post_type, $post_status, '_mpp_component_id', $component_id);
    }
    //$sql[] = $post_type_sql;
    $new_sql = $join_sql = '';
    //array();
    //let us generate inner sub queries
    /* if ( $sql )
    	  $join_sql	 = ' (' . join( ' AND object_id IN (', $sql );
    
    	  //we need to append the ) for closing the sub queries
    	  for ( $i = 0; $i < count( $sql ); $i++ )
    	  $join_sql .=')';
    
    	 */
    $join_sql = $tax_object_sql;
    $new_sql = $post_type_sql;
    //if the join sql is present, let us append it
    if ($join_sql) {
        $new_sql .= ' AND ID IN ' . $join_sql;
    }
    //echo $new_sql;
    return $wpdb->get_var($new_sql);
}
 public function build_params($args)
 {
     $defaults = array('type' => array_keys(mpp_get_active_types()), 'id' => false, 'in' => false, 'exclude' => false, 'slug' => false, 'status' => array_keys(mpp_get_active_statuses()), 'component' => array_keys(mpp_get_active_components()), 'component_id' => false, 'gallery_id' => false, 'galleries' => false, 'galleries_exclude' => false, 'storage' => '', 'per_page' => mpp_get_option('media_per_page'), 'offset' => false, 'page' => false, 'nopaging' => false, 'order' => 'DESC', 'orderby' => false, 'user_id' => false, 'user_name' => false, 'scope' => false, 'search_terms' => '', 'year' => false, 'month' => false, 'week' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'yearmonth' => '', 'meta_query' => false, 'fields' => false);
     //build params for WP_Query
     /**
      * If are querying for a single gallery
      * and the gallery media were sorted by the user, show the media s in the sort order insted of the default date 
      */
     if (isset($args['gallery_id']) && mpp_is_gallery_sorted($args['gallery_id'])) {
         $defaults['orderby'] = 'menu_order';
     }
     $r = wp_parse_args($args, $defaults);
     extract($r, EXTR_SKIP);
     //build the wp_query args
     $wp_query_args = array('post_type' => mpp_get_media_post_type(), 'post_status' => 'any', 'p' => $id, 'post__in' => $in, 'post__not_in' => $exclude, 'name' => $slug, 'post_parent' => $gallery_id, 'post_parent__in' => !empty($galleries) ? (array) $galleries : 0, 'post_parent__not_in' => !empty($galleries_exclude) ? (array) $galleries_exclude : 0, 'posts_per_page' => $per_page, 'paged' => $page, 'offset' => $offset, 'nopaging' => $nopaging, 'author' => $user_id, 'author_name' => $user_name, 'year' => $year, 'monthnum' => $month, 'w' => $week, 'day' => $day, 'hour' => $hour, 'minute' => $minute, 'second' => $second, 'm' => $yearmonth, 'order' => $order, 'orderby' => $orderby, 's' => $search_terms, 'fields' => $fields, '_mpp_mapped_query' => true);
     //TODO: SCOPE
     //
     //we will need to build tax query/meta query
     //taxonomy query to filter by component|status|privacy
     $tax_query = array();
     //meta query
     $gmeta_query = array();
     if (isset($meta_key) && $meta_key) {
         $wp_query_args['meta_key'] = $meta_key;
     }
     if (isset($meta_key) && $meta_key && isset($meta_value)) {
         $wp_query_args['meta_value'] = $meta_value;
     }
     //if meta query was specified, let us keep it and we will add our conditions
     if (!empty($meta_query)) {
         $gmeta_query = $meta_query;
     }
     //we will need to build tax query/meta query
     //type, audio video etc
     //if type is given and it is valid gallery type
     //Pass one or more types
     if ($gallery_id) {
         //if gallery id is given, avoid worrying about type
         $type = '';
         $component = '';
     }
     if (!empty($type) && mpp_are_registered_types($type)) {
         $type = mpp_string_to_array($type);
         //we store the terms with _name such as private becomes _private, members become _members to avoid conflicting terms
         $type = array_map('mpp_underscore_it', $type);
         $tax_query[] = array('taxonomy' => mpp_get_type_taxname(), 'field' => 'slug', 'terms' => $type, 'operator' => 'IN');
     }
     //privacy
     //pass one or more privacy level
     if (!empty($status) && mpp_are_registered_statuses($status)) {
         $status = mpp_string_to_array($status);
         $status = array_map('mpp_underscore_it', $status);
         $tax_query[] = array('taxonomy' => mpp_get_status_taxname(), 'field' => 'slug', 'terms' => $status, 'operator' => 'IN');
     }
     if (!empty($component) && mpp_are_registered_components($component)) {
         $component = mpp_string_to_array($component);
         $component = array_map('mpp_underscore_it', $component);
         $tax_query[] = array('taxonomy' => mpp_get_component_taxname(), 'field' => 'slug', 'terms' => $component, 'operator' => 'IN');
     }
     //done with the tax query
     if (count($tax_query) > 1) {
         $tax_query['relation'] = 'AND';
     }
     if (!empty($tax_query)) {
         $wp_query_args['tax_query'] = $tax_query;
     }
     //now, for component
     if (!empty($component_id)) {
         $meta_compare = '=';
         if (is_array($component_id)) {
             $meta_compare = 'IN';
         }
         $gmeta_query[] = array('key' => '_mpp_component_id', 'value' => $component_id, 'compare' => $meta_compare, 'type' => 'UNSIGNED');
     }
     //also make sure that it only looks for gallery media
     $gmeta_query[] = array('key' => '_mpp_is_mpp_media', 'value' => 1, 'compare' => '=', 'type' => 'UNSIGNED');
     //should we avoid the orphaned media
     //Let us discuss with the community and get it here
     if (!mpp_get_option('show_orphaned_media')) {
         $gmeta_query[] = array('key' => '_mpp_is_orphan', 'compare' => 'NOT EXISTS');
     }
     //Let us filter the media by storage method
     if (!empty($storage)) {
         $gmeta_query[] = array('key' => '_mpp_storage_method', 'value' => $storage, 'compare' => '=');
     }
     //and what to do when a user searches by the media source(say youtube|vimeo|xyz.. how do we do that?)
     //reset meta query
     if (!empty($gmeta_query)) {
         $wp_query_args['meta_query'] = $gmeta_query;
     }
     return $wp_query_args;
     //http://wordpress.stackexchange.com/questions/53783/cant-sort-get-posts-by-post-mime-type
 }