/** * Initialization function, similar to __construct() * * @since 0.60 * * @return void */ public static function initialize() { MLATest::$wp_3dot5 = version_compare(get_bloginfo('version'), '3.5.0', '>=') && version_compare(get_bloginfo('version'), '3.5.99', '<='); MLATest::$wp_4dot3_plus = version_compare(get_bloginfo('version'), '4.2.99', '>='); /* * This is the earliest effective place to change error_reporting */ MLACore::$original_php_log = ini_get('error_log'); MLACore::$original_php_reporting = sprintf('0x%1$04X', error_reporting()); $php_reporting = trim(MLACore::mla_get_option(MLACoreOptions::MLA_DEBUG_REPLACE_PHP_REPORTING)); if (!empty($php_reporting)) { @error_reporting(0 + $php_reporting); } /* * This is the earliest effective place to localize values in other plugin components */ MLACoreOptions::mla_localize_option_definitions_array(); if (class_exists('MLASettings')) { MLASettings::mla_localize_tablist(); } if (class_exists('MLAQuery')) { MLAQuery::mla_localize_default_columns_array(); } if (class_exists('MLA_Upload_List_Table')) { MLA_Upload_List_Table::mla_localize_default_columns_array(); } if (class_exists('MLA_Upload_Optional_List_Table')) { MLA_Upload_Optional_List_Table::mla_localize_default_columns_array(); } if (class_exists('MLA_View_List_Table')) { MLA_View_List_Table::mla_localize_default_columns_array(); } }
/** * Returns the Media/Assistant submenu table column slugs/keys * * @since 2.22 * * @return array ( index => 'column_slug' ) */ public function get_default_column_names() { if (!class_exists('MLAQuery')) { require_once MLA_PLUGIN_PATH . 'includes/class-mla-data-query.php'; MLAQuery::initialize(); } return array_keys(apply_filters('mla_list_table_get_columns', MLAQuery::$default_columns)); }
/** * Renders the MLA Gallery in meta box on the Edit Media page. * Declared public because it is a callback function. * * @since 0.80 * * @param object current post * * @return void echoes the HTML markup for the meta box content */ public static function mla_mla_gallery_in_handler($post) { if (is_null(self::$mla_references)) { self::$mla_references = MLAQuery::mla_fetch_attachment_references($post->ID, $post->post_parent); } $galleries = ''; if (is_array(self::$mla_references)) { foreach (self::$mla_references['mla_galleries'] as $gallery_id => $gallery) { if ($gallery_id == $post->post_parent) { $parent = __('PARENT', 'media-library-assistant') . ' '; } else { $parent = ''; } $galleries .= sprintf('%1$s (%2$s %3$s), %4$s', $parent, $gallery['post_type'], $gallery_id, $gallery['post_title']) . "\n"; } // foreach $feature } $galleries = apply_filters('mla_mla_gallery_in_meta_box', array('galleries' => $galleries, 'rows' => 5, 'cols' => 80), self::$mla_references, $post); $html = '<label class="screen-reader-text" for="mla_mla_gallery_in">' . __('MLA Gallery in', 'media-library-assistant') . '</label><textarea class="readonly" id="mla_mla_gallery_in" rows="' . absint($galleries['rows']) . '" cols="' . absint($galleries['cols']) . '" readonly="readonly" name="mla_mla_gallery_in" >' . esc_textarea($galleries['galleries']) . "</textarea>\n"; echo apply_filters('mla_mla_gallery_in_meta_box_html', $html, $galleries, self::$mla_references, $post); }
/** * Ajax handler for Media Manager "Query Attachments" queries * * Adapted from wp_ajax_query_attachments in /wp-admin/includes/ajax-actions.php * * @since 2.20 * * @return void passes array of post arrays to wp_send_json_success() for JSON encoding and transmission */ public static function mla_query_attachments_action() { if (!current_user_can('upload_files')) { wp_send_json_error(); } /* * Pick out and clean up the query terms we can process */ $raw_query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array(); $query = array_intersect_key($raw_query, array_flip(array('order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'mla_filter_month', 'mla_filter_term', 'mla_terms_search', 'mla_search_value', 's', 'mla_search_fields', 'mla_search_connector'))); $query = apply_filters('mla_media_modal_query_initial_terms', $query, $raw_query); if (isset($query['post_mime_type'])) { if ('detached' == $query['post_mime_type']) { $query['detached'] = '1'; unset($query['post_mime_type']); } elseif ('attached' == $query['post_mime_type']) { $query['detached'] = '0'; unset($query['post_mime_type']); } elseif ('trash' == $query['post_mime_type']) { $query['status'] = 'trash'; unset($query['post_mime_type']); } else { $view = $query['post_mime_type']; unset($query['post_mime_type']); $query = array_merge($query, MLACore::mla_prepare_view_query('view', $view)); } } /* * Convert mla_filter_month back to the WordPress "m" parameter */ if (isset($query['mla_filter_month'])) { if ('0' != $query['mla_filter_month']) { $query['m'] = $query['mla_filter_month']; } unset($query['mla_filter_month']); } /* * Process the enhanced search box OR fix up the default search box */ if (isset($query['mla_search_value'])) { if (!empty($query['mla_search_value'])) { $query['s'] = $query['mla_search_value']; } unset($query['mla_search_value']); } if (isset($query['posts_per_page'])) { $count = $query['posts_per_page']; $offset = $count * (isset($query['paged']) ? $query['paged'] - 1 : 0); } else { $count = 0; $offset = 0; } /* * Check for sorting override */ $option = MLACore::mla_get_option(MLACoreOptions::MLA_MEDIA_MODAL_ORDERBY); if ('default' != $option) { /* * Make sure the current orderby choice still exists or revert to default. */ $default_orderby = array_merge(array('none' => array('none', false)), MLAQuery::mla_get_sortable_columns()); $found_current = false; foreach ($default_orderby as $key => $value) { if ($option == $value[0]) { $found_current = true; break; } } if (!$found_current) { MLACore::mla_delete_option(MLACoreOptions::MLA_DEFAULT_ORDERBY); $option = MLACore::mla_get_option(MLACoreOptions::MLA_DEFAULT_ORDERBY); } $query['orderby'] = $option; } $option = MLACore::mla_get_option(MLACoreOptions::MLA_MEDIA_MODAL_ORDER); if ('default' != $option) { $query['order'] = $option; } $query['post_type'] = 'attachment'; if (empty($query['status'])) { $query['post_status'] = 'inherit'; if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) { $query['post_status'] .= ',private'; } } $query = apply_filters('mla_media_modal_query_filtered_terms', $query, $raw_query); $query = MLAQuery::mla_query_media_modal_items($query, $offset, $count); $posts = array_map('wp_prepare_attachment_for_js', $query->posts); $posts = array_filter($posts); wp_send_json_success($posts); }
/** * Find Featured Image and inserted image/link references to an attachment * * Called from MLAQuery::mla_fetch_attachment_references, which handles conditional * loading of this file. * * @since 0.1 * * @param int post ID of attachment * @param int post ID of attachment's parent, if any * @param boolean True to compute references, false to return empty values * * @return array Reference information; see $references array comments */ public static function mla_fetch_attachment_references_handler($ID, $parent, $add_references = true) { global $wpdb; static $save_id = -1, $references, $inserted_in_option = NULL; if ($save_id == $ID) { return $references; } elseif ($ID == -1) { $save_id = -1; return NULL; } /* * inserted_option 'enabled', 'base' or 'disabled' * tested_reference true if any of the four where-used types was processed * found_reference true if any where-used array is not empty() * found_parent true if $parent matches a where-used post ID * is_unattached true if $parent is zero (0) * base_file relative path and name of the uploaded file, e.g., 2012/04/image.jpg * path path to the file, relative to the "uploads/" directory, e.g., 2012/04/ * file The name portion of the base file, e.g., image.jpg * files base file and any other image size files. Array key is path and file name. * Non-image file value is a string containing file name without path * Image file value is an array with file name, width and height * features Array of objects with the post_type and post_title of each post * that has the attachment as a "Featured Image" * inserts Array of specific files (i.e., sizes) found in one or more posts/pages * as an image (<img>) or link (<a href>). The array key is the path and file name. * The array value is an array with the ID, post_type and post_title of each reference * mla_galleries Array of objects with the post_type and post_title of each post * that was returned by an [mla_gallery] shortcode * galleries Array of objects with the post_type and post_title of each post * that was returned by a [gallery] shortcode * parent_type 'post' or 'page' or the custom post type of the attachment's parent * parent_status 'publish', 'private', 'future', 'pending', 'draft' * parent_title post_title of the attachment's parent * parent_errors UNATTACHED, ORPHAN, BAD/INVALID PARENT */ $references = array('inserted_option' => '', 'tested_reference' => false, 'found_reference' => false, 'found_parent' => false, 'is_unattached' => (int) $parent === 0, 'base_file' => '', 'path' => '', 'file' => '', 'files' => array(), 'features' => array(), 'inserts' => array(), 'mla_galleries' => array(), 'galleries' => array(), 'parent_type' => '', 'parent_status' => '', 'parent_title' => '', 'parent_errors' => ''); if (!$add_references) { return $references; } /* * Fill in Parent data */ $parent_data = MLAQuery::mla_fetch_attachment_parent_data($parent); if (isset($parent_data['parent_type'])) { $references['parent_type'] = $parent_data['parent_type']; } if (isset($parent_data['parent_status'])) { $references['parent_status'] = $parent_data['parent_status']; } if (isset($parent_data['parent_title'])) { $references['parent_title'] = $parent_data['parent_title']; } $references['base_file'] = get_post_meta($ID, '_wp_attached_file', true); $pathinfo = pathinfo($references['base_file']); $references['file'] = $pathinfo['basename']; if (!isset($pathinfo['dirname']) || '.' == $pathinfo['dirname']) { $references['path'] = '/'; } else { $references['path'] = $pathinfo['dirname'] . '/'; } $attachment_metadata = get_post_meta($ID, '_wp_attachment_metadata', true); $sizes = isset($attachment_metadata['sizes']) ? $attachment_metadata['sizes'] : NULL; if (is_array($sizes)) { // Using the name as the array key ensures each name is added only once foreach ($sizes as $size => $size_info) { $size_info['size'] = $size; $references['files'][$references['path'] . $size_info['file']] = $size_info; } } $base_type = wp_check_filetype($references['file']); $base_reference = array('file' => $references['file'], 'width' => isset($attachment_metadata['width']) ? $attachment_metadata['width'] : 0, 'height' => isset($attachment_metadata['height']) ? $attachment_metadata['height'] : 0, 'mime_type' => isset($base_type['type']) ? $base_type['type'] : 'unknown', 'size' => 'full'); $references['files'][$references['base_file']] = $base_reference; /* * Process the where-used settings option */ if ('checked' == MLACore::mla_get_option(MLACore::MLA_EXCLUDE_REVISIONS)) { $exclude_revisions = "(post_type <> 'revision') AND "; } else { $exclude_revisions = ''; } /* * Accumulate reference test types, e.g., 0 = no tests, 4 = all tests */ $reference_tests = 0; /* * Look for the "Featured Image(s)", if enabled */ if (MLACore::$process_featured_in) { $reference_tests++; $features = $wpdb->get_results("\r\n\t\t\t\t\tSELECT post_id\r\n\t\t\t\t\tFROM {$wpdb->postmeta}\r\n\t\t\t\t\tWHERE meta_key = '_thumbnail_id' AND meta_value = {$ID}\r\n\t\t\t\t\t"); if (!empty($features)) { foreach ($features as $feature) { $feature_results = $wpdb->get_results("\r\n\t\t\t\t\t\t\tSELECT ID, post_type, post_status, post_title\r\n\t\t\t\t\t\t\tFROM {$wpdb->posts}\r\n\t\t\t\t\t\t\tWHERE {$exclude_revisions}(ID = {$feature->post_id})\r\n\t\t\t\t\t\t\t"); if (!empty($feature_results)) { $references['found_reference'] = true; $references['features'][$feature->post_id] = $feature_results[0]; if ($feature->post_id == $parent) { $references['found_parent'] = true; } } // ! empty } // foreach $feature } } // $process_featured_in /* * Look for item(s) inserted in post_content */ $references['inserted_option'] = $inserted_in_option; if (MLACore::$process_inserted_in) { $reference_tests++; if (NULL == $inserted_in_option) { $inserted_in_option = MLACore::mla_get_option(MLACore::MLA_INSERTED_IN_TUNING); $references['inserted_option'] = $inserted_in_option; } if ('base' == $inserted_in_option) { $query_parameters = array(); $query = array(); $query[] = "SELECT ID, post_type, post_status, post_title, CONVERT(`post_content` USING utf8 ) AS POST_CONTENT FROM {$wpdb->posts} WHERE {$exclude_revisions} ( %s=%s"; $query_parameters[] = '1'; // for empty file name array $query_parameters[] = '0'; // for empty file name array foreach ($references['files'] as $file => $file_data) { if (empty($file)) { continue; } $query[] = 'OR ( POST_CONTENT LIKE %s)'; if (MLAQuery::$wp_4dot0_plus) { $query_parameters[] = '%' . $wpdb->esc_like($file) . '%'; } else { $query_parameters[] = '%' . like_escape($file) . '%'; } } $query[] = ')'; $query = join(' ', $query); $inserts = $wpdb->get_results($wpdb->prepare($query, $query_parameters)); if (!empty($inserts)) { $references['found_reference'] = true; $references['inserts'][$pathinfo['filename']] = $inserts; foreach ($inserts as $index => $insert) { unset($references['inserts'][$pathinfo['filename']][$index]->POST_CONTENT); if ($insert->ID == $parent) { $references['found_parent'] = true; } } // foreach $insert } // ! empty } else { // process base names foreach ($references['files'] as $file => $file_data) { if (empty($file)) { continue; } if (MLAQuery::$wp_4dot0_plus) { $like = $wpdb->esc_like($file); } else { $like = like_escape($file); } $inserts = $wpdb->get_results($wpdb->prepare("SELECT ID, post_type, post_status, post_title FROM {$wpdb->posts}\r\n\t\t\t\t\t\t\tWHERE {$exclude_revisions}(CONVERT(`post_content` USING utf8 ) LIKE %s)", "%{$like}%")); if (!empty($inserts)) { $references['found_reference'] = true; $references['inserts'][$file_data['file']] = $inserts; foreach ($inserts as $insert) { if ($insert->ID == $parent) { $references['found_parent'] = true; } } // foreach $insert } // ! empty } // foreach $file } // process intermediate sizes } // $process_inserted_in /* * Look for [mla_gallery] references */ if (MLACore::$process_mla_gallery_in) { $reference_tests++; if (self::_build_mla_galleries(MLACore::MLA_MLA_GALLERY_IN_TUNING, self::$mla_galleries, '[mla_gallery', $exclude_revisions)) { $galleries = self::_search_mla_galleries(self::$mla_galleries, $ID); if (!empty($galleries)) { $references['found_reference'] = true; $references['mla_galleries'] = $galleries; foreach ($galleries as $post_id => $gallery) { if ($post_id == $parent) { $references['found_parent'] = true; } } // foreach $gallery } else { // ! empty $references['mla_galleries'] = array(); } } } // $process_mla_gallery_in /* * Look for [gallery] references */ if (MLACore::$process_gallery_in) { $reference_tests++; if (self::_build_mla_galleries(MLACore::MLA_GALLERY_IN_TUNING, self::$galleries, '[gallery', $exclude_revisions)) { $galleries = self::_search_mla_galleries(self::$galleries, $ID); if (!empty($galleries)) { $references['found_reference'] = true; $references['galleries'] = $galleries; foreach ($galleries as $post_id => $gallery) { if ($post_id == $parent) { $references['found_parent'] = true; } } // foreach $gallery } else { // ! empty $references['galleries'] = array(); } } } // $process_gallery_in /* * Evaluate and summarize reference tests */ $errors = ''; if (0 == $reference_tests) { $references['tested_reference'] = false; $errors .= '(' . __('NO REFERENCE TESTS', 'media-library-assistant') . ')'; } else { $references['tested_reference'] = true; $suffix = 4 == $reference_tests ? '' : '?'; if (!$references['found_reference']) { $errors .= '(' . sprintf(__('ORPHAN', 'media-library-assistant') . '%1$s) ', $suffix); } if (!$references['found_parent'] && !empty($references['parent_title'])) { $errors .= '(' . sprintf(__('UNUSED', 'media-library-assistant') . '%1$s) ', $suffix); } } if ($references['is_unattached']) { $errors .= '(' . __('UNATTACHED', 'media-library-assistant') . ')'; } elseif (empty($references['parent_title'])) { $errors .= '(' . __('INVALID PARENT', 'media-library-assistant') . ')'; } $references['parent_errors'] = trim($errors); $save_id = $ID; $references = apply_filters('mla_fetch_attachment_references', $references, $ID, $parent); return $references; }
/** * Sanitize and expand query arguments from request variables * * Prepare the arguments for WP_Query. * Modeled after wp_edit_attachments_query in wp-admin/post.php * * @since 0.1 * * @param array query parameters from web page, usually found in $_REQUEST * @param int Optional number of rows (default 0) to skip over to reach desired page * @param int Optional number of rows on each page (0 = all rows, default) * * @return array revised arguments suitable for WP_Query */ private static function _prepare_list_table_query($raw_request, $offset = 0, $count = 0) { /* * Go through the $raw_request, take only the arguments that are used in the query and * sanitize or validate them. */ if (!is_array($raw_request)) { /* translators: 1: ERROR tag 2: function name 3: non-array value */ error_log(sprintf(_x('%1$s: %2$s non-array "%3$s"', 'error_log', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), 'MLAQuery::_prepare_list_table_query', var_export($raw_request, true)), 0); return NULL; } /* * Make sure the current orderby choice still exists or revert to default. */ $default_orderby = array_merge(array('none' => array('none', false)), self::mla_get_sortable_columns()); $current_orderby = MLACore::mla_get_option(MLACore::MLA_DEFAULT_ORDERBY); $found_current = false; foreach ($default_orderby as $key => $value) { if ($current_orderby == $value[0]) { $found_current = true; break; } } if ($found_current) { /* * Custom fields can have HTML reserved characters, which are encoded by * mla_get_sortable_columns, so a separate, unencoded list is required. */ $default_orderby = MLACore::mla_custom_field_support('custom_sortable_columns'); foreach ($default_orderby as $sort_key => $sort_value) { if ($current_orderby == $sort_key) { $current_orderby = 'c_' . $sort_value[0]; break; } } // foreach } else { MLACore::mla_delete_option(MLACore::MLA_DEFAULT_ORDERBY); $current_orderby = MLACore::mla_get_option(MLACore::MLA_DEFAULT_ORDERBY); } $clean_request = array('m' => 0, 'orderby' => $current_orderby, 'order' => MLACore::mla_get_option(MLACore::MLA_DEFAULT_ORDER), 'post_type' => 'attachment', 'post_status' => 'inherit', 'mla_search_connector' => 'AND', 'mla_search_fields' => array()); foreach ($raw_request as $key => $value) { switch ($key) { /* * 'sentence' and 'exact' modify the keyword search ('s') * Their value is not important, only their presence. */ case 'sentence': case 'exact': case 'mla-tax': case 'mla-term': $clean_request[$key] = sanitize_key($value); break; case 'orderby': if (in_array($value, array('none', 'post__in'))) { $clean_request[$key] = $value; } else { $orderby = NULL; /* * Custom fields can have HTML reserved characters, which are encoded by * mla_get_sortable_columns, so a separate, unencoded list is required. */ $sortable_columns = MLACore::mla_custom_field_support('custom_sortable_columns'); foreach ($sortable_columns as $sort_key => $sort_value) { if ($value == $sort_key) { $orderby = 'c_' . $sort_value[0]; break; } } // foreach if (NULL === $orderby) { $sortable_columns = MLAQuery::mla_get_sortable_columns(); foreach ($sortable_columns as $sort_key => $sort_value) { if ($value == $sort_value[0]) { $orderby = $value; break; } } // foreach } if (NULL !== $orderby) { $clean_request[$key] = $orderby; } } break; /* * ids allows hooks to supply a persistent list of items */ /* * ids allows hooks to supply a persistent list of items */ case 'ids': if (is_array($value)) { $clean_request['post__in'] = $value; } else { $clean_request['post__in'] = array_map('absint', explode(',', $value)); } break; /* * post__in and post__not_in are used in the Media Modal Ajax queries */ /* * post__in and post__not_in are used in the Media Modal Ajax queries */ case 'post__in': case 'post__not_in': case 'post_mime_type': $clean_request[$key] = $value; break; case 'parent': case 'post_parent': $clean_request['post_parent'] = absint($value); break; /* * ['m'] - filter by year and month of post, e.g., 201204 */ /* * ['m'] - filter by year and month of post, e.g., 201204 */ case 'author': case 'm': $clean_request[$key] = absint($value); break; /* * ['mla_filter_term'] - filter by category or tag ID; -1 allowed */ /* * ['mla_filter_term'] - filter by category or tag ID; -1 allowed */ case 'mla_filter_term': $clean_request[$key] = intval($value); break; case 'order': switch ($value = strtoupper($value)) { case 'ASC': case 'DESC': $clean_request[$key] = $value; break; default: $clean_request[$key] = 'ASC'; } break; case 'detached': if ('0' == $value || '1' == $value) { $clean_request['detached'] = $value; } break; case 'status': if ('trash' == $value) { $clean_request['post_status'] = 'trash'; } break; /* * ['s'] - Search Media by one or more keywords * ['mla_search_connector'], ['mla_search_fields'] - Search Media options */ /* * ['s'] - Search Media by one or more keywords * ['mla_search_connector'], ['mla_search_fields'] - Search Media options */ case 's': switch (substr($value, 0, 3)) { case '>|<': $clean_request['debug'] = 'console'; break; case '<|>': $clean_request['debug'] = 'log'; break; } if (isset($clean_request['debug'])) { $value = substr($value, 3); } $value = stripslashes(trim($value)); if (!empty($value)) { $clean_request[$key] = $value; } break; case 'mla_terms_search': if (!empty($value['phrases']) && !empty($value['taxonomies'])) { $value['phrases'] = stripslashes(trim($value['phrases'])); if (!empty($value['phrases'])) { $clean_request[$key] = $value; } } break; case 'mla_search_connector': case 'mla_search_fields': $clean_request[$key] = $value; break; case 'mla-metakey': case 'mla-metavalue': $clean_request[$key] = stripslashes($value); break; case 'meta_query': if (!empty($value)) { if (is_array($value)) { $clean_request[$key] = $value; } else { $clean_request[$key] = unserialize(stripslashes($value)); unset($clean_request[$key]['slug']); } // not array } break; default: // ignore anything else in $_REQUEST } // switch $key } // foreach $raw_request /* * Pass query and search parameters to the filters for _execute_list_table_query */ self::$query_parameters = array('use_alt_text_view' => false, 'use_postmeta_view' => false, 'use_orderby_view' => false, 'orderby' => $clean_request['orderby'], 'order' => $clean_request['order']); self::$query_parameters['detached'] = isset($clean_request['detached']) ? $clean_request['detached'] : NULL; self::$search_parameters = array('debug' => 'none'); /* * Matching a meta_value to NULL requires a LEFT JOIN to a view and a special WHERE clause * Matching a wildcard pattern requires mainpulating the WHERE clause, too */ if (isset($clean_request['meta_query']['key'])) { self::$query_parameters['use_postmeta_view'] = true; self::$query_parameters['postmeta_key'] = $clean_request['meta_query']['key']; self::$query_parameters['postmeta_value'] = NULL; unset($clean_request['meta_query']); } elseif (isset($clean_request['meta_query']['patterns'])) { self::$query_parameters['patterns'] = $clean_request['meta_query']['patterns']; unset($clean_request['meta_query']['patterns']); } if (isset($clean_request['debug'])) { self::$query_parameters['debug'] = $clean_request['debug']; self::$search_parameters['debug'] = $clean_request['debug']; MLACore::mla_debug_mode($clean_request['debug']); unset($clean_request['debug']); } /* * We must patch the WHERE clause if there are leading spaces in the meta_value */ if (isset($clean_request['mla-metavalue']) && 0 < strlen($clean_request['mla-metavalue']) && ' ' == $clean_request['mla-metavalue'][0]) { self::$query_parameters['mla-metavalue'] = $clean_request['mla-metavalue']; } /* * We will handle "Terms Search" in the mla_query_posts_search_filter. */ if (isset($clean_request['mla_terms_search'])) { self::$search_parameters['mla_terms_search'] = $clean_request['mla_terms_search']; /* * The Terms Search overrides any terms-based keyword search for now; too complicated. */ if (isset($clean_request['mla_search_fields'])) { foreach ($clean_request['mla_search_fields'] as $index => $field) { if ('terms' == $field) { unset($clean_request['mla_search_fields'][$index]); } } } } /* * We will handle keyword search in the mla_query_posts_search_filter. */ if (isset($clean_request['s'])) { self::$search_parameters['s'] = $clean_request['s']; self::$search_parameters['mla_search_fields'] = apply_filters('mla_list_table_search_filter_fields', $clean_request['mla_search_fields'], array('content', 'title', 'excerpt', 'alt-text', 'name', 'terms')); self::$search_parameters['mla_search_connector'] = $clean_request['mla_search_connector']; self::$search_parameters['sentence'] = isset($clean_request['sentence']); self::$search_parameters['exact'] = isset($clean_request['exact']); if (in_array('alt-text', self::$search_parameters['mla_search_fields'])) { self::$query_parameters['use_alt_text_view'] = true; } if (in_array('terms', self::$search_parameters['mla_search_fields'])) { self::$search_parameters['mla_search_taxonomies'] = MLACore::mla_supported_taxonomies('term-search'); } unset($clean_request['s']); unset($clean_request['mla_search_connector']); unset($clean_request['mla_search_fields']); unset($clean_request['sentence']); unset($clean_request['exact']); } /* * We have to handle custom field/post_meta values here * because they need a JOIN clause supplied by WP_Query */ if ('c_' == substr($clean_request['orderby'], 0, 2)) { $option_value = MLAQuery::mla_custom_field_option_value($clean_request['orderby']); if (isset($option_value['name'])) { self::$query_parameters['use_orderby_view'] = true; self::$query_parameters['postmeta_key'] = $option_value['name']; if (isset($clean_request['orderby'])) { unset($clean_request['orderby']); } if (isset($clean_request['order'])) { unset($clean_request['order']); } } } else { // custom field switch (self::$query_parameters['orderby']) { /* * '_wp_attachment_image_alt' is special; it can have NULL values, * so we'll handle it in the JOIN and ORDERBY filters */ case '_wp_attachment_image_alt': self::$query_parameters['use_orderby_view'] = true; self::$query_parameters['postmeta_key'] = '_wp_attachment_image_alt'; if (isset($clean_request['orderby'])) { unset($clean_request['orderby']); } if (isset($clean_request['order'])) { unset($clean_request['order']); } break; case '_wp_attached_file': $clean_request['meta_key'] = '_wp_attached_file'; $clean_request['orderby'] = 'meta_value'; $clean_request['order'] = self::$query_parameters['order']; break; } // switch $orderby } /* * Ignore incoming paged value; use offset and count instead */ if ((int) $count > 0) { $clean_request['offset'] = $offset; $clean_request['posts_per_page'] = $count; } elseif ((int) $count == -1) { $clean_request['posts_per_page'] = $count; } /* * ['mla_filter_term'] - filter by taxonomy * * cat = 0 is "All Categories", i.e., no filtering * cat = -1 is "No Categories" */ if (isset($clean_request['mla_filter_term'])) { if ($clean_request['mla_filter_term'] != 0) { $tax_filter = MLACore::mla_taxonomy_support('', 'filter'); if ($clean_request['mla_filter_term'] == -1) { $term_list = get_terms($tax_filter, array('fields' => 'ids', 'hide_empty' => false)); $clean_request['tax_query'] = array(array('taxonomy' => $tax_filter, 'field' => 'id', 'terms' => $term_list, 'operator' => 'NOT IN')); } else { // mla_filter_term == -1 $clean_request['tax_query'] = array(array('taxonomy' => $tax_filter, 'field' => 'id', 'terms' => array((int) $clean_request['mla_filter_term']), 'include_children' => 'checked' == MLACore::mla_get_option(MLACore::MLA_TAXONOMY_FILTER_INCLUDE_CHILDREN))); } // mla_filter_term != -1 } // mla_filter_term != 0 unset($clean_request['mla_filter_term']); } // isset mla_filter_term if (isset($clean_request['mla-tax']) && isset($clean_request['mla-term'])) { $clean_request['tax_query'] = array(array('taxonomy' => $clean_request['mla-tax'], 'field' => 'slug', 'terms' => $clean_request['mla-term'], 'include_children' => false)); unset($clean_request['mla-tax']); unset($clean_request['mla-term']); } // isset mla_tax if (isset($clean_request['mla-metakey']) && isset($clean_request['mla-metavalue'])) { $clean_request['meta_key'] = $clean_request['mla-metakey']; $clean_request['meta_value'] = $clean_request['mla-metavalue']; unset($clean_request['mla-metakey']); unset($clean_request['mla-metavalue']); } // isset mla_tax return $clean_request; }
/** * Evaluate custom field mapping data source * * @since 2.20 * * @param integer post->ID of attachment * @param string category/scope to evaluate against: custom_field_mapping or single_attachment_mapping * @param array data source specification ( name, *data_source, *keep_existing, *format, mla_column, quick_edit, bulk_edit, *meta_name, *option, no_null ) * @param array (optional) _wp_attachment_metadata, default NULL (use current postmeta database value) * * @return string|array data source value */ private static function _evaluate_data_source($post_id, $category, $data_value, $attachment_metadata = NULL) { global $wpdb; static $upload_dir, $intermediate_sizes = NULL, $wp_attached_files = NULL, $wp_attachment_metadata = NULL; static $current_id = 0, $file_info = NULL, $parent_info = NULL, $references = NULL, $alt_text = NULL; if ('none' == $data_value['data_source']) { return ''; } $data_source = $data_value['data_source']; /* * Do this once per page load; cache attachment metadata if mapping all attachments */ if (NULL == $intermediate_sizes) { $upload_dir = wp_upload_dir(); $upload_dir = $upload_dir['basedir'] . '/'; $intermediate_sizes = get_intermediate_image_sizes(); if ('custom_field_mapping' == $category) { if (!($table = _get_meta_table('post'))) { $wp_attached_files = array(); $wp_attachment_metadata = array(); } else { $wp_attachment_metadata = $wpdb->get_results("SELECT post_id, meta_value FROM {$table} WHERE meta_key = '_wp_attachment_metadata'", OBJECT_K); $wp_attached_files = $wpdb->get_results("SELECT post_id, meta_value FROM {$table} WHERE meta_key = '_wp_attached_file'", OBJECT_K); } } // custom_field_mapping, i.e., mapping all attachments } // first call after page load /* * Do this once per post. Simulate SQL results for $wp_attached_files and $wp_attachment_metadata. */ if ($current_id != $post_id) { $current_id = $post_id; $parent_info = NULL; $references = NULL; $alt_text = NULL; if ('single_attachment_mapping' == $category) { $metadata = get_metadata('post', $post_id, '_wp_attached_file'); if (isset($metadata[0])) { $wp_attached_files = array($post_id => (object) array('post_id' => $post_id, 'meta_value' => $metadata[0])); } else { $wp_attached_files = array(); } if (NULL == $attachment_metadata) { $metadata = get_metadata('post', $post_id, '_wp_attachment_metadata'); if (isset($metadata[0])) { $attachment_metadata = $metadata[0]; } } if (empty($attachment_metadata)) { $attachment_metadata = array(); } $wp_attachment_metadata = array($post_id => (object) array('post_id' => $post_id, 'meta_value' => serialize($attachment_metadata))); } $file_info = MLAData_Source::_evaluate_file_information($upload_dir, $wp_attached_files, $wp_attachment_metadata, $post_id); } $size_info = array('file' => '', 'width' => '', 'height' => ''); $match_count = preg_match('/(.+)\\[(.+)\\]/', $data_source, $matches); if (1 == $match_count) { $data_source = $matches[1] . '[size]'; if (isset($file_info['sizes'][$matches[2]])) { $size_info = $file_info['sizes'][$matches[2]]; } } $result = ''; switch ($data_source) { case 'meta': $attachment_metadata = isset($wp_attachment_metadata[$post_id]->meta_value) ? maybe_unserialize($wp_attachment_metadata[$post_id]->meta_value) : array(); $result = MLAData::mla_find_array_element($data_value['meta_name'], $attachment_metadata, $data_value['option'], $data_value['keep_existing']); break; case 'template': if (in_array($data_value['option'], array('single', 'export', 'array', 'multi'))) { $default_option = 'array'; } else { $default_option = 'text'; } /* * Go through the template and expand the non-prefixed elements * as Data Sources */ $item_values = array(); $placeholders = MLAData::mla_get_template_placeholders($data_value['meta_name'], $default_option); foreach ($placeholders as $key => $placeholder) { if (empty($placeholder['prefix'])) { $field_value = $data_value; $field_value['data_source'] = $placeholder['value']; $field_value['meta_name'] = ''; $field_value['option'] = $placeholder['option']; $field_value['format'] = $placeholder['format']; if (isset($placeholder['args'])) { $field_value['args'] = $placeholder['args']; } $field_value = MLAData_Source::_evaluate_data_source($post_id, $category, $field_value, $attachment_metadata); $item_values[$key] = MLAData::mla_apply_field_level_format($field_value, $placeholder); } // Data Source } // foreach placeholder /* * Now expand the template using the above Data Source values */ $template = '[+template:' . $data_value['meta_name'] . '+]'; $item_values = MLAData::mla_expand_field_level_parameters($template, NULL, $item_values, $post_id, $data_value['keep_existing'], $default_option); if ('array' == $default_option) { $result = MLAData::mla_parse_array_template($template, $item_values); $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); } else { $result = MLAData::mla_parse_template($template, $item_values); } break; case 'parent': $data_source = 'post_parent'; /* fallthru */ /* fallthru */ case 'ID': case 'post_id': case 'post_author': case 'post_parent': case 'menu_order': case 'comment_count': $result = absint(MLAData_Source::_evaluate_post_information($post_id, $category, $data_source)); break; case 'alt_text': if (NULL == $alt_text) { $metadata = get_metadata('post', $post_id, '_wp_attachment_image_alt'); if (is_array($metadata)) { if (count($metadata) == 1) { $alt_text = maybe_unserialize($metadata[0]); } else { $alt_text = array(); foreach ($metadata as $single_key => $single_value) { $alt_text[$single_key] = maybe_unserialize($single_value); } } } } if (!empty($alt_text)) { $result = MLAData_Source::_evaluate_array_result($alt_text, $data_value['option'], $data_value['keep_existing']); } break; case 'mime_type': $data_source = 'post_mime_type'; /* fallthru */ /* fallthru */ case 'post_date': case 'post_date_gmt': case 'post_content': case 'post_title': case 'post_excerpt': case 'post_status': case 'comment_status': case 'ping_status': case 'post_name': case 'post_modified': case 'post_modified_gmt': case 'post_content_filtered': case 'guid': case 'post_mime_type': $result = MLAData_Source::_evaluate_post_information($post_id, $category, $data_source); break; case 'absolute_path': case 'absolute_file_name': case 'base_file': case 'path': case 'file_name': case 'name_only': case 'extension': case 'width': case 'height': case 'orientation': case 'hwstring_small': case 'aperture': case 'credit': case 'camera': case 'caption': case 'created_timestamp': case 'copyright': case 'focal_length': case 'iso': case 'shutter_speed': case 'title': if (isset($file_info[$data_source])) { $result = $file_info[$data_source]; } break; case 'file_size': $filesize = @filesize($file_info['absolute_file_name_raw']); if (!(false === $filesize)) { $result = $filesize; } break; case 'upload_date': $result = MLAData_Source::_evaluate_post_information($post_id, $category, 'post_date'); break; case 'dimensions': $result = $file_info['width'] . 'x' . $file_info['height']; if ('x' == $result) { $result = ''; } break; case 'pixels': $result = absint((int) $file_info['width'] * (int) $file_info['height']); if (0 == $result) { $result = ''; } else { $result = (string) $result; } break; case 'size_keys': $result = array(); foreach ($file_info['sizes'] as $key => $value) { $result[] = $key; } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); break; case 'size_names': $result = array(); foreach ($file_info['sizes'] as $key => $value) { $result[] = $value['file']; } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); break; case 'size_bytes': $result = array(); foreach ($file_info['sizes'] as $key => $value) { $filesize = @filesize($file_info['absolute_path_raw'] . $value['file']); if (false === $filesize) { $result[] = '?'; } else { switch ($data_value['format']) { case 'commas': if (is_numeric($filesize)) { $filesize = number_format((double) $filesize); } break; default: // no change } // format $result[] = $filesize; } } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); break; case 'size_pixels': $result = array(); foreach ($file_info['sizes'] as $key => $value) { $pixels = absint((int) $value['width'] * (int) $value['height']); switch ($data_value['format']) { case 'commas': if (is_numeric($pixels)) { $pixels = number_format((double) $pixels); } break; default: // no change } // format $result[] = $pixels; } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); break; case 'size_dimensions': $result = array(); foreach ($file_info['sizes'] as $key => $value) { $result[] = $value['width'] . 'x' . $value['height']; } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); break; case 'size_name[size]': $result = $size_info['file']; break; case 'size_bytes[size]': $result = @filesize($file_info['absolute_path_raw'] . $size_info['file']); if (false === $result) { $result = '?'; } break; case 'size_pixels[size]': $result = absint((int) $size_info['width'] * (int) $size_info['height']); break; case 'size_dimensions[size]': $result = $size_info['width'] . 'x' . $size_info['height']; if ('x' == $result) { $result = ''; } break; case 'parent_date': case 'parent_type': case 'parent_title': if (is_null($parent_info)) { $parent_info = MLAQuery::mla_fetch_attachment_parent_data(MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (isset($parent_info[$data_source])) { $result = $parent_info[$data_source]; } break; case 'parent_issues': if (is_null($references)) { $references = MLAQuery::mla_fetch_attachment_references($post_id, MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (!empty($references['parent_errors'])) { $result = $references['parent_errors']; /* * Remove (ORPHAN... */ $orphan_certain = '(' . __('ORPHAN', 'media-library-assistant') . ')'; $orphan_possible = '(' . __('ORPHAN', 'media-library-assistant') . '?)'; if (false !== strpos($result, $orphan_certain)) { $result = trim(substr($result, strlen($orphan_certain))); } elseif (false !== strpos($result, $orphan_possible)) { $result = trim(substr($result, strlen($orphan_possible))); } } break; case 'reference_issues': if (is_null($references)) { $references = MLAQuery::mla_fetch_attachment_references($post_id, MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (!empty($references['parent_errors'])) { $result = $references['parent_errors']; } break; case 'featured_in': case 'featured_in_title': if (is_null($references)) { $references = MLAQuery::mla_fetch_attachment_references($post_id, MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (!empty($references['features'])) { $result = array(); foreach ($references['features'] as $ID => $value) { if ('featured_in' == $data_source) { $result[] = sprintf('%1$s (%2$s %3$d)', $value->post_title, $value->post_type, $ID); } else { $result[] = $value->post_title; } } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); } else { $result = ''; } break; case 'inserted_in': case 'inserted_in_title': if (is_null($references)) { $references = MLAQuery::mla_fetch_attachment_references($post_id, MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (!empty($references['inserts'])) { $result = array(); foreach ($references['inserts'] as $base_file => $inserts) { foreach ($inserts as $value) { if ('inserted_in' == $data_source) { $result[] = sprintf('%1$s (%2$s %3$d)', $value->post_title, $value->post_type, $value->ID); } else { $result[] = $value->post_title; } } } ksort($result); $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); } else { $result = ''; } break; case 'gallery_in': case 'gallery_in_title': if (is_null($references)) { $references = MLAQuery::mla_fetch_attachment_references($post_id, MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (!empty($references['galleries'])) { $result = array(); foreach ($references['galleries'] as $ID => $value) { if ('gallery_in' == $data_source) { $result[] = sprintf('%1$s (%2$s %3$d)', $value['post_title'], $value['post_type'], $ID); } else { $result[] = $value['post_title']; } } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); } else { $result = ''; } break; case 'mla_gallery_in': case 'mla_gallery_in_title': if (is_null($references)) { $references = MLAQuery::mla_fetch_attachment_references($post_id, MLAData_Source::_evaluate_post_information($post_id, $category, 'post_parent')); } if (!empty($references['mla_galleries'])) { $result = array(); foreach ($references['mla_galleries'] as $ID => $value) { if ('mla_gallery_in' == $data_source) { $result[] = sprintf('%1$s (%2$s %3$d)', $value['post_title'], $value['post_type'], $ID); } else { $result[] = $value['post_title']; } } $result = MLAData_Source::_evaluate_array_result($result, $data_value['option'], $data_value['keep_existing']); } else { $result = ''; } break; default: $custom_value = apply_filters('mla_evaluate_custom_data_source', NULL, $post_id, $category, $data_value, $attachment_metadata); if (!is_null($custom_value)) { return $custom_value; } return ''; } // switch $data_source switch ($data_value['format']) { case 'raw': return $result; case 'commas': if (is_numeric($result)) { $result = str_pad(number_format((double) $result), 15, ' ', STR_PAD_LEFT); } break; case 'native': default: /* * Make some numeric values sortable as strings, make all value non-empty */ if (in_array($data_source, array('file_size', 'pixels', 'width', 'height'))) { $result = str_pad($result, 15, ' ', STR_PAD_LEFT); } elseif (empty($result)) { $result = ' '; } } // format return $result; }
/** * Parses shortcode parameters and returns the gallery objects * * @since 2.20 * * @param int Post ID of the parent * @param array Attributes of the shortcode * @param boolean true to calculate and return ['found_posts'] as an array element * * @return array List of attachments returned from WP_Query */ public static function mla_get_shortcode_attachments($post_parent, $attr, $return_found_rows = NULL) { global $wp_query; /* * Parameters passed to the where and orderby filter functions */ self::$query_parameters = array(); /* * Parameters passed to the posts_search filter function in MLAData */ MLAQuery::$search_parameters = array('debug' => 'none'); /* * Make sure $attr is an array, even if it's empty */ if (empty($attr)) { $attr = array(); } elseif (is_string($attr)) { $attr = shortcode_parse_atts($attr); } /* * The "where used" queries have no $_REQUEST context available to them, * so tax_, date_ and meta_query evaluation will fail if they contain "{+request:" * parameters. Ignore these errors. */ if (isset($attr['where_used_query']) && 'this-is-a-where-used-query' == $attr['where_used_query']) { $where_used_query = true; unset($attr['where_used_query']); } else { $where_used_query = false; } /* * Merge input arguments with defaults, then extract the query arguments. * * $return_found_rows is used to indicate that the call comes from gallery_shortcode(), * which is the only call that supplies it. */ if (!is_null($return_found_rows)) { $attr = apply_filters('mla_gallery_query_attributes', $attr); } $arguments = shortcode_atts(self::$mla_get_shortcode_attachments_parameters, $attr); $mla_page_parameter = $arguments['mla_page_parameter']; unset($arguments['mla_page_parameter']); /* * $mla_page_parameter, if set, doesn't make it through the shortcode_atts filter, * so we handle it separately */ if (!isset($arguments[$mla_page_parameter])) { if (isset($attr[$mla_page_parameter])) { $arguments[$mla_page_parameter] = $attr[$mla_page_parameter]; } else { $arguments[$mla_page_parameter] = NULL; } } if (!empty($arguments['ids'])) { // 'ids' is explicitly ordered, unless you specify otherwise. if (empty($attr['orderby'])) { $arguments['orderby'] = 'post__in'; } $arguments['include'] = $arguments['ids']; } unset($arguments['ids']); if (!is_null($return_found_rows)) { $arguments = apply_filters('mla_gallery_query_arguments', $arguments); } /* * Extract taxonomy arguments */ $query_arguments = array(); if (!empty($attr)) { $all_taxonomies = get_taxonomies(array('show_ui' => true), 'names'); $simple_tax_queries = array(); foreach ($attr as $key => $value) { if ('tax_query' == $key) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $tax_query = NULL; $value = self::_sanitize_query_specification($value); /* * Replace invalid queries from "where-used" callers with a harmless equivalent */ if ($where_used_query && false !== strpos($value, '{+')) { $value = "array( array( 'taxonomy' => 'none', 'field' => 'slug', 'terms' => 'none' ) )"; } $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $tax_query = $function(); } if (is_array($tax_query)) { $query_arguments[$key] = $tax_query; break; // Done - the tax_query overrides all other taxonomy parameters } else { return '<p>' . __('ERROR', 'media-library-assistant') . ': ' . __('Invalid mla_gallery', 'media-library-assistant') . ' tax_query = ' . var_export($value, true) . '</p>'; } } // not array } elseif (array_key_exists($key, $all_taxonomies)) { $simple_tax_queries[$key] = implode(',', array_filter(array_map('trim', explode(',', $value)))); } // array_key_exists } //foreach $attr /* * One of five outcomes: * 1) An explicit tax_query was found; use it and ignore all other taxonomy parameters * 2) No tax query is present; no further processing required * 3) Two or more simple tax queries are present; compose a tax_query * 4) One simple tax query and (tax_operator or tax_include_children) are present; compose a tax_query * 5) One simple tax query is present; use it as-is or convert 'category' to 'category_name' */ if (isset($query_arguments['tax_query']) || empty($simple_tax_queries)) { // No further action required } elseif (1 < count($simple_tax_queries) || isset($attr['tax_operator']) || isset($attr['tax_include_children'])) { // Build a tax_query if (1 < count($simple_tax_queries)) { $tax_relation = 'AND'; if (isset($attr['tax_relation'])) { if ('OR' == strtoupper($attr['tax_relation'])) { $tax_relation = 'OR'; } } $tax_query = array('relation' => $tax_relation); } else { $tax_query = array(); } // Validate other tax_query parameters or set defaults $tax_operator = 'IN'; if (isset($attr['tax_operator'])) { $attr_value = strtoupper($attr['tax_operator']); if (in_array($attr_value, array('IN', 'NOT IN', 'AND'))) { $tax_operator = $attr_value; } } $tax_include_children = true; if (isset($attr['tax_include_children'])) { if ('FALSE' == strtoupper($attr['tax_include_children'])) { $tax_include_children = false; } } foreach ($simple_tax_queries as $key => $value) { $tax_query[] = array('taxonomy' => $key, 'field' => 'slug', 'terms' => explode(',', $value), 'operator' => $tax_operator, 'include_children' => $tax_include_children); } $query_arguments['tax_query'] = $tax_query; } else { // exactly one simple query is present if (isset($simple_tax_queries['category'])) { $arguments['category_name'] = $simple_tax_queries['category']; } else { $query_arguments = $simple_tax_queries; } } } // ! empty /* * $query_arguments has been initialized in the taxonomy code above. */ $is_tax_query = !($use_children = empty($query_arguments)); foreach ($arguments as $key => $value) { /* * There are several "fallthru" cases in this switch statement that decide * whether or not to limit the query to children of a specific post. */ $children_ok = true; switch ($key) { case 'post_parent': switch (strtolower($value)) { case 'all': $value = NULL; $use_children = false; break; case 'any': self::$query_parameters['post_parent'] = 'any'; $value = NULL; $use_children = false; break; case 'current': $value = $post_parent; $use_children = true; break; case 'none': self::$query_parameters['post_parent'] = 'none'; $value = NULL; $use_children = false; break; } // fallthru // fallthru case 'id': if (is_numeric($value)) { $query_arguments[$key] = intval($value); if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'numberposts': case 'posts_per_page': case 'posts_per_archive_page': if (is_numeric($value)) { $value = intval($value); if (!empty($value)) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; case 'meta_value_num': $children_ok = false; // fallthru // fallthru case 'offset': if (is_numeric($value)) { $query_arguments[$key] = intval($value); if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'paged': if ('current' == strtolower($value)) { /* * Note: The query variable 'page' holds the pagenumber for a single paginated * Post or Page that includes the <!--nextpage--> Quicktag in the post content. */ if (get_query_var('page')) { $query_arguments[$key] = get_query_var('page'); } else { $query_arguments[$key] = get_query_var('paged') ? get_query_var('paged') : 1; } } elseif (is_numeric($value)) { $query_arguments[$key] = intval($value); } elseif ('' === $value) { $query_arguments[$key] = 1; } unset($arguments[$key]); break; case $mla_page_parameter: case 'mla_paginate_total': if (is_numeric($value)) { $query_arguments[$key] = intval($value); } elseif ('' === $value) { $query_arguments[$key] = 1; } unset($arguments[$key]); break; case 'author': case 'cat': case 'tag_id': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = array_filter($value); } else { $query_arguments[$key] = array_filter(array_map('intval', explode(",", $value))); } if (1 == count($query_arguments[$key])) { $query_arguments[$key] = $query_arguments[$key][0]; } else { $query_arguments[$key] = implode(',', $query_arguments[$key]); } $use_children = false; } unset($arguments[$key]); break; case 'category__and': case 'category__in': case 'category__not_in': case 'tag__and': case 'tag__in': case 'tag__not_in': case 'include': $children_ok = false; // fallthru // fallthru case 'exclude': if (!empty($value)) { if (is_array($value)) { $value = array_filter($value); } else { $value = array_filter(array_map('intval', explode(",", $value))); } if (!empty($value)) { $query_arguments[$key] = $value; if (!$children_ok) { $use_children = false; } } } unset($arguments[$key]); break; case 'tag_slug__and': case 'tag_slug__in': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $query_arguments[$key] = array_filter(array_map('trim', explode(",", $value))); } $use_children = false; } unset($arguments[$key]); break; case 'nopaging': // boolean value, default false if (!empty($value) && 'false' != strtolower($value)) { $query_arguments[$key] = true; } unset($arguments[$key]); break; // boolean values, default true // boolean values, default true case 'cache_results': case 'update_post_meta_cache': case 'update_post_term_cache': if (!empty($value) && 'true' != strtolower($value)) { $query_arguments[$key] = false; } unset($arguments[$key]); break; case 'sentence': case 'exact': if (!empty($value) && 'true' == strtolower($value)) { MLAQuery::$search_parameters[$key] = true; } else { MLAQuery::$search_parameters[$key] = false; } unset($arguments[$key]); break; case 'mla_search_connector': case 'mla_phrase_connector': case 'mla_term_connector': if (!empty($value) && 'OR' == strtoupper($value)) { MLAQuery::$search_parameters[$key] = 'OR'; } else { MLAQuery::$search_parameters[$key] = 'AND'; } unset($arguments[$key]); break; case 'mla_terms_phrases': $children_ok = false; // fallthru // fallthru case 'mla_terms_taxonomies': case 'mla_search_fields': if (!empty($value)) { MLAQuery::$search_parameters[$key] = $value; if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 's': MLAQuery::$search_parameters['s'] = trim($value); // fallthru // fallthru case 'author_name': case 'category_name': case 'tag': case 'meta_key': case 'meta_value': case 'meta_compare': $children_ok = false; // fallthru // fallthru case 'post_type': case 'post_status': case 'post_mime_type': case 'orderby': if (!empty($value)) { $query_arguments[$key] = $value; if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'order': if (!empty($value)) { $value = strtoupper($value); if (in_array($value, array('ASC', 'DESC'))) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; case 'date_query': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $date_query = NULL; $value = self::_sanitize_query_specification($value); /* * Replace invalid queries from "where-used" callers with a harmless equivalent */ if ($where_used_query && false !== strpos($value, '{+')) { $value = "array( array( 'key' => 'unlikely', 'value' => 'none or otherwise unlikely' ) )"; } $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $date_query = $function(); } if (is_array($date_query)) { $query_arguments[$key] = $date_query; } else { return '<p>' . __('ERROR', 'media-library-assistant') . ': ' . __('Invalid mla_gallery', 'media-library-assistant') . ' date_query = ' . var_export($value, true) . '</p>'; } } // not array $use_children = false; } unset($arguments[$key]); break; case 'meta_query': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $meta_query = NULL; $value = self::_sanitize_query_specification($value); /* * Replace invalid queries from "where-used" callers with a harmless equivalent */ if ($where_used_query && false !== strpos($value, '{+')) { $value = "array( array( 'key' => 'unlikely', 'value' => 'none or otherwise unlikely' ) )"; } $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $meta_query = $function(); } if (is_array($meta_query)) { $query_arguments[$key] = $meta_query; } else { return '<p>' . __('ERROR', 'media-library-assistant') . ': ' . __('Invalid mla_gallery', 'media-library-assistant') . ' meta_query = ' . var_export($value, true) . '</p>'; } } // not array $use_children = false; } unset($arguments[$key]); break; case 'fields': if (!empty($value)) { $value = strtolower($value); if (in_array($value, array('ids', 'id=>parent'))) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; default: // ignore anything else } // switch $key } // foreach $arguments /* * Decide whether to use a "get_children" style query */ self::$query_parameters['disable_tax_join'] = $is_tax_query && !$use_children; if ($use_children && !isset($query_arguments['post_parent'])) { if (!isset($query_arguments['id'])) { $query_arguments['post_parent'] = $post_parent; } else { $query_arguments['post_parent'] = $query_arguments['id']; } unset($query_arguments['id']); } if (isset($query_arguments['numberposts']) && !isset($query_arguments['posts_per_page'])) { $query_arguments['posts_per_page'] = $query_arguments['numberposts']; } unset($query_arguments['numberposts']); /* * MLA pagination will override WordPress pagination */ if (isset($query_arguments[$mla_page_parameter])) { unset($query_arguments['nopaging']); unset($query_arguments['offset']); unset($query_arguments['paged']); if (isset($query_arguments['mla_paginate_total']) && $query_arguments[$mla_page_parameter] > $query_arguments['mla_paginate_total']) { $query_arguments['offset'] = 0x7fffffff; // suppress further output } else { $query_arguments['paged'] = $query_arguments[$mla_page_parameter]; } } else { if (isset($query_arguments['posts_per_page']) || isset($query_arguments['posts_per_archive_page']) || isset($query_arguments['paged']) || isset($query_arguments['offset'])) { unset($query_arguments['nopaging']); } } unset($query_arguments[$mla_page_parameter]); unset($query_arguments['mla_paginate_total']); if (isset($query_arguments['post_mime_type']) && 'all' == strtolower($query_arguments['post_mime_type'])) { unset($query_arguments['post_mime_type']); } if (!empty($query_arguments['include'])) { $incposts = wp_parse_id_list($query_arguments['include']); $query_arguments['posts_per_page'] = count($incposts); // only the number of posts included $query_arguments['post__in'] = $incposts; unset($query_arguments['include']); } elseif (!empty($query_arguments['exclude'])) { $query_arguments['post__not_in'] = wp_parse_id_list($query_arguments['exclude']); unset($query_arguments['exclude']); } $query_arguments['ignore_sticky_posts'] = true; $query_arguments['no_found_rows'] = is_null($return_found_rows) ? true : !$return_found_rows; /* * We will always handle "orderby" in our filter */ self::$query_parameters['orderby'] = self::_validate_sql_orderby($query_arguments); if (false === self::$query_parameters['orderby']) { unset(self::$query_parameters['orderby']); } unset($query_arguments['orderby']); unset($query_arguments['order']); if (self::$mla_debug) { add_filter('posts_clauses', 'MLAShortcode_Support::mla_shortcode_query_posts_clauses_filter', 0x7fffffff, 1); add_filter('posts_clauses_request', 'MLAShortcode_Support::mla_shortcode_query_posts_clauses_request_filter', 0x7fffffff, 1); } add_filter('posts_join', 'MLAShortcode_Support::mla_shortcode_query_posts_join_filter', 0x7fffffff, 1); add_filter('posts_where', 'MLAShortcode_Support::mla_shortcode_query_posts_where_filter', 0x7fffffff, 1); add_filter('posts_orderby', 'MLAShortcode_Support::mla_shortcode_query_posts_orderby_filter', 0x7fffffff, 1); /* * Handle the keyword and terms search in the posts_search filter. * One or both of 'mla_terms_phrases' and 's' must be present to * trigger the search. */ if (empty(MLAQuery::$search_parameters['mla_terms_phrases']) && empty(MLAQuery::$search_parameters['s'])) { MLAQuery::$search_parameters = array('debug' => 'none'); } else { /* * Convert Terms Search parameters to the filter's requirements. * mla_terms_taxonomies is shared with keyword search. */ if (empty(MLAQuery::$search_parameters['mla_terms_taxonomies'])) { MLAQuery::$search_parameters['mla_terms_search']['taxonomies'] = MLACore::mla_supported_taxonomies('term-search'); } else { MLAQuery::$search_parameters['mla_terms_search']['taxonomies'] = array_filter(array_map('trim', explode(',', MLAQuery::$search_parameters['mla_terms_taxonomies']))); } if (!empty(MLAQuery::$search_parameters['mla_terms_phrases'])) { MLAQuery::$search_parameters['mla_terms_search']['phrases'] = MLAQuery::$search_parameters['mla_terms_phrases']; if (empty(MLAQuery::$search_parameters['mla_phrase_connector'])) { MLAQuery::$search_parameters['mla_terms_search']['radio_phrases'] = 'AND'; } else { MLAQuery::$search_parameters['mla_terms_search']['radio_phrases'] = MLAQuery::$search_parameters['mla_phrase_connector']; } if (empty(MLAQuery::$search_parameters['mla_term_connector'])) { MLAQuery::$search_parameters['mla_terms_search']['radio_terms'] = 'OR'; } else { MLAQuery::$search_parameters['mla_terms_search']['radio_terms'] = MLAQuery::$search_parameters['mla_phrase_connector']; } } unset(MLAQuery::$search_parameters['mla_terms_phrases']); unset(MLAQuery::$search_parameters['mla_terms_taxonomies']); unset(MLAQuery::$search_parameters['mla_phrase_connector']); unset(MLAQuery::$search_parameters['mla_term_connector']); if (empty(MLAQuery::$search_parameters['mla_search_fields'])) { MLAQuery::$search_parameters['mla_search_fields'] = array('title', 'content'); } else { MLAQuery::$search_parameters['mla_search_fields'] = array_filter(array_map('trim', explode(',', MLAQuery::$search_parameters['mla_search_fields']))); MLAQuery::$search_parameters['mla_search_fields'] = array_intersect(array('title', 'content', 'excerpt', 'name', 'terms'), MLAQuery::$search_parameters['mla_search_fields']); /* * Look for keyword search including 'terms' */ foreach (MLAQuery::$search_parameters['mla_search_fields'] as $index => $field) { if ('terms' == $field) { if (isset(MLAQuery::$search_parameters['mla_terms_search']['phrases'])) { /* * The Terms Search overrides any terms-based keyword search for now; too complicated. */ unset(MLAQuery::$search_parameters['mla_search_fields'][$index]); } else { MLAQuery::$search_parameters['mla_search_taxonomies'] = MLAQuery::$search_parameters['mla_terms_search']['taxonomies']; unset(MLAQuery::$search_parameters['mla_terms_search']['taxonomies']); } } // terms in search fields } } // mla_search_fields present if (empty(MLAQuery::$search_parameters['mla_search_connector'])) { MLAQuery::$search_parameters['mla_search_connector'] = 'AND'; } if (empty(MLAQuery::$search_parameters['sentence'])) { MLAQuery::$search_parameters['sentence'] = false; } if (empty(MLAQuery::$search_parameters['exact'])) { MLAQuery::$search_parameters['exact'] = false; } MLAQuery::$search_parameters['debug'] = self::$mla_debug ? 'shortcode' : 'none'; add_filter('posts_search', 'MLAQuery::mla_query_posts_search_filter', 10, 2); add_filter('posts_groupby', 'MLAQuery::mla_query_posts_groupby_filter'); } if (self::$mla_debug) { global $wp_filter; foreach ($wp_filter['posts_where'] as $priority => $filters) { $debug_message = '<strong>mla_debug $wp_filter[posts_where]</strong> priority = ' . var_export($priority, true) . '<br />'; foreach ($filters as $name => $descriptor) { $debug_message .= 'filter name = ' . var_export($name, true) . '<br />'; } MLACore::mla_debug_add($debug_message); } foreach ($wp_filter['posts_orderby'] as $priority => $filters) { $debug_message = '<strong>mla_debug $wp_filter[posts_orderby]</strong> priority = ' . var_export($priority, true) . '<br />'; foreach ($filters as $name => $descriptor) { $debug_message .= 'filter name = ' . var_export($name, true) . '<br />'; } MLACore::mla_debug_add($debug_message); } } /* * Disable Relevanssi - A Better Search, v3.2 by Mikko Saari * relevanssi_prevent_default_request( $request, $query ) * apply_filters('relevanssi_admin_search_ok', $admin_search_ok, $query ); */ if (function_exists('relevanssi_prevent_default_request')) { add_filter('relevanssi_admin_search_ok', 'MLAData::mla_query_relevanssi_admin_search_ok_filter'); } if (class_exists('MLA_Polylang')) { $query_arguments = apply_filters('mla_get_shortcode_attachments_final_terms', $query_arguments, $return_found_rows); } MLAShortcodes::$mla_gallery_wp_query_object = new WP_Query(); $attachments = MLAShortcodes::$mla_gallery_wp_query_object->query($query_arguments); /* * $return_found_rows is used to indicate that the call comes from gallery_shortcode(), * which is the only call that supplies it. */ if (is_null($return_found_rows)) { $return_found_rows = false; } else { do_action('mla_gallery_wp_query_object', $query_arguments); } if ($return_found_rows) { $attachments['found_rows'] = MLAShortcodes::$mla_gallery_wp_query_object->found_posts; } if (!empty(MLAQuery::$search_parameters)) { remove_filter('posts_groupby', 'MLAQuery::mla_query_posts_groupby_filter'); remove_filter('posts_search', 'MLAQuery::mla_query_posts_search_filter'); } if (function_exists('relevanssi_prevent_default_request')) { remove_filter('relevanssi_admin_search_ok', 'MLAData::mla_query_relevanssi_admin_search_ok_filter'); } remove_filter('posts_join', 'MLAShortcode_Support::mla_shortcode_query_posts_join_filter', 0x7fffffff); remove_filter('posts_where', 'MLAShortcode_Support::mla_shortcode_query_posts_where_filter', 0x7fffffff); remove_filter('posts_orderby', 'MLAShortcode_Support::mla_shortcode_query_posts_orderby_filter', 0x7fffffff); if (self::$mla_debug) { remove_filter('posts_clauses', 'MLAShortcode_Support::mla_shortcode_query_posts_clauses_filter', 0x7fffffff); remove_filter('posts_clauses_request', 'MLAShortcode_Support::mla_shortcode_query_posts_clauses_request_filter', 0x7fffffff); MLACore::mla_debug_add('<strong>' . __('mla_debug query', 'media-library-assistant') . '</strong> = ' . var_export($query_arguments, true)); MLACore::mla_debug_add('<strong>' . __('mla_debug request', 'media-library-assistant') . '</strong> = ' . var_export(MLAShortcodes::$mla_gallery_wp_query_object->request, true)); MLACore::mla_debug_add('<strong>' . __('mla_debug query_vars', 'media-library-assistant') . '</strong> = ' . var_export(MLAShortcodes::$mla_gallery_wp_query_object->query_vars, true)); MLACore::mla_debug_add('<strong>' . __('mla_debug post_count', 'media-library-assistant') . '</strong> = ' . var_export(MLAShortcodes::$mla_gallery_wp_query_object->post_count, true)); } MLAShortcodes::$mla_gallery_wp_query_object = NULL; return $attachments; }
/** * Update a single item; change the "post" data, taxonomy terms * and meta data for a single attachment * * @since 0.1 * * @param int The ID of the attachment to be updated * @param array Field name => value pairs * @param array Optional taxonomy term values, default null * @param array Optional taxonomy actions (add, remove, replace), default null * * @return array success/failure message and NULL content */ public static function mla_update_single_item($post_id, $new_data, $tax_input = NULL, $tax_actions = NULL) { $post_data = self::mla_get_attachment_by_id($post_id, false); if (!isset($post_data)) { return array('message' => __('ERROR', 'media-library-assistant') . ': ' . __('Could not retrieve Attachment.', 'media-library-assistant'), 'body' => ''); } $updates = apply_filters('mla_update_single_item', compact(array('new_data', 'tax_input', 'tax_actions')), $post_id, $post_data); $new_data = isset($updates['new_data']) ? $updates['new_data'] : array(); $tax_input = isset($updates['tax_input']) ? $updates['tax_input'] : NULL; $tax_actions = isset($updates['tax_actions']) ? $updates['tax_actions'] : NULL; $message = ''; $updates = array('ID' => $post_id); $new_data = stripslashes_deep($new_data); $new_meta = NULL; foreach ($new_data as $key => $value) { switch ($key) { case 'post_title': if ($value == $post_data[$key]) { break; } /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Title', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); $updates[$key] = $value; break; case 'post_name': if ($value == $post_data[$key]) { break; } $value = sanitize_title($value); /* * Make sure new slug is unique */ $args = array('name' => $value, 'post_type' => 'attachment', 'post_status' => 'inherit', 'showposts' => 1); $my_posts = get_posts($args); if ($my_posts) { /* translators: 1: ERROR tag 2: old_value */ $message .= sprintf(__('%1$s: Could not change Name/Slug "%2$s"; name already exists', 'media-library-assistant') . '<br>', __('ERROR', 'media-library-assistant'), $value); } else { /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Name/Slug', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); $updates[$key] = $value; } break; /* * bulk_image_alt requires a separate key because some attachment types * should not get a value, e.g., text or PDF documents */ /* * bulk_image_alt requires a separate key because some attachment types * should not get a value, e.g., text or PDF documents */ case 'bulk_image_alt': if ('image/' !== substr($post_data['post_mime_type'], 0, 6)) { break; } // fallthru // fallthru case 'image_alt': $key = 'mla_wp_attachment_image_alt'; if (!isset($post_data[$key])) { $post_data[$key] = NULL; } if ($value == $post_data[$key]) { break; } if (empty($value)) { if (delete_post_meta($post_id, '_wp_attachment_image_alt')) { /* translators: 1: old_value */ $message .= sprintf(__('Deleting ALT Text, was "%1$s"', 'media-library-assistant') . '<br>', esc_attr($post_data[$key])); } else { /* translators: 1: ERROR tag 2: old_value */ $message .= sprintf(__('%1$s: Could not delete ALT Text, remains "%2$s"', 'media-library-assistant') . '<br>', __('ERROR', 'media-library-assistant'), esc_attr($post_data[$key])); } } else { /* * ALT Text isn't supposed to have multiple values, but it happens. * Delete multiple values and start over. */ if (is_array($post_data[$key])) { delete_post_meta($post_id, '_wp_attachment_image_alt'); } if (update_post_meta($post_id, '_wp_attachment_image_alt', $value)) { /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('ALT Text', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); } else { /* translators: 1: ERROR tag 2: old_value 3: new_value */ $message .= sprintf(__('%1$s: Could not change ALT Text from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('ERROR', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); } } break; case 'post_excerpt': if ($value == $post_data[$key]) { break; } /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Caption', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); $updates[$key] = $value; break; case 'post_content': if ($value == $post_data[$key]) { break; } /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Description', 'media-library-assistant'), esc_textarea($post_data[$key]), esc_textarea($value)); $updates[$key] = $value; break; case 'post_parent': if ($value == $post_data[$key]) { break; } $value = absint($value); /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Parent', 'media-library-assistant'), $post_data[$key], $value); $updates[$key] = $value; break; case 'menu_order': if ($value == $post_data[$key]) { break; } $value = absint($value); /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Menu Order', 'media-library-assistant'), $post_data[$key], $value); $updates[$key] = $value; break; case 'post_author': if ($value == $post_data[$key]) { break; } $value = absint($value); $from_user = get_userdata($post_data[$key]); $to_user = get_userdata($value); /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Author', 'media-library-assistant'), $from_user->display_name, $to_user->display_name); $updates[$key] = $value; break; case 'comment_status': if ($value == $post_data[$key]) { break; } /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Comments', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); $updates[$key] = $value; break; case 'ping_status': if ($value == $post_data[$key]) { break; } /* translators: 1: element name 2: old_value 3: new_value */ $message .= sprintf(__('Changing %1$s from "%2$s" to "%3$s"', 'media-library-assistant') . '<br>', __('Pings', 'media-library-assistant'), esc_attr($post_data[$key]), esc_attr($value)); $updates[$key] = $value; break; case 'taxonomy_updates': $tax_input = $value['inputs']; $tax_actions = $value['actions']; break; case 'custom_updates': $new_meta = $value; break; default: // Ignore anything else } // switch $key } // foreach $new_data if (!empty($tax_input)) { foreach ($tax_input as $taxonomy => $tags) { if (!empty($tax_actions)) { $tax_action = $tax_actions[$taxonomy]; } else { $tax_action = 'replace'; } $taxonomy_obj = get_taxonomy($taxonomy); if (current_user_can($taxonomy_obj->cap->assign_terms)) { if (is_array($tags)) { // array of int = hierarchical, comma-delimited string = non-hierarchical. $tags = array_filter($tags); } switch ($tax_action) { case 'add': if (!empty($tags)) { $action_name = __('Adding', 'media-library-assistant'); $result = wp_set_post_terms($post_id, $tags, $taxonomy, true); } break; case 'remove': $action_name = __('Removing', 'media-library-assistant'); $tags = self::_remove_terms($post_id, $tags, $taxonomy_obj); $result = wp_set_post_terms($post_id, $tags, $taxonomy); if (empty($tags)) { $result = true; } break; case 'replace': $action_name = __('Replacing', 'media-library-assistant'); $result = wp_set_post_terms($post_id, $tags, $taxonomy); if (empty($tags)) { $result = true; } break; default: $action_name = __('Ignoring', 'media-library-assistant'); $result = NULL; // ignore anything else } /* * Definitive results check would use: * do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ); * in /wp_includes/taxonomy.php function wp_set_object_terms() */ if (!empty($result)) { delete_transient(MLA_OPTION_PREFIX . 't_term_counts_' . $taxonomy); /* translators: 1: action_name, 2: taxonomy */ $message .= sprintf(__('%1$s "%2$s" terms', 'media-library-assistant') . '<br>', $action_name, $taxonomy); } } else { // current_user_can /* translators: 1: taxonomy */ $message .= sprintf(__('You cannot assign "%1$s" terms', 'media-library-assistant') . '<br>', $taxonomy); } } // foreach $tax_input } // ! empty $tax_input if (is_array($new_meta)) { $message .= self::mla_update_item_postmeta($post_id, $new_meta); } if (empty($message)) { return array('message' => sprintf(__('Item %1$d, no changes detected.', 'media-library-assistant'), $post_id), 'body' => ''); } else { // invalidate the cached item self::mla_get_attachment_by_id(-1); MLAQuery::mla_fetch_attachment_parent_data(-1); MLAQuery::mla_fetch_attachment_metadata(-1); MLAQuery::mla_fetch_attachment_references(-1, 0); // See if anything else has changed if (1 < count($updates)) { $result = wp_update_post($updates); } else { $result = $post_id; } do_action('mla_updated_single_item', $post_id, $result); if ($result) { /* translators: 1: post ID */ $final_message = sprintf(__('Item %1$d updated.', 'media-library-assistant'), $post_id); /* * Uncomment this for debugging. */ // $final_message .= '<br>' . $message; //error_log( 'DEBUG: mla_update_single_item message = ' . var_export( $message, true ), 0 ); return array('message' => $final_message, 'body' => ''); } else { return array('message' => sprintf(__('%1$s: Item %2$d update failed.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $post_id), 'body' => ''); } } }
/** * Save General settings to the options table * * @since 0.1 * * @uses $_REQUEST * * @return array Message(s) reflecting the results of the operation */ private static function _save_general_settings() { $message_list = ''; foreach (MLACore::$mla_option_definitions as $key => $value) { if ('general' == $value['tab']) { switch ($key) { case MLACore::MLA_FEATURED_IN_TUNING: MLACore::$process_featured_in = 'disabled' != $_REQUEST[MLA_OPTION_PREFIX . $key]; break; case MLACore::MLA_INSERTED_IN_TUNING: MLACore::$process_inserted_in = 'disabled' != $_REQUEST[MLA_OPTION_PREFIX . $key]; break; case MLACore::MLA_GALLERY_IN_TUNING: MLACore::$process_gallery_in = 'disabled' != $_REQUEST[MLA_OPTION_PREFIX . $key]; if ('refresh' == $_REQUEST[MLA_OPTION_PREFIX . $key]) { MLAQuery::mla_flush_mla_galleries(MLACore::MLA_GALLERY_IN_TUNING); /* translators: 1: reference type, e.g., Gallery in */ $message_list .= "<br>" . sprintf(_x('%1$s - references updated.', 'message_list', 'media-library-assistant'), __('Gallery in', 'media-library-assistant')) . "\r\n"; $_REQUEST[MLA_OPTION_PREFIX . $key] = 'cached'; } break; case MLACore::MLA_MLA_GALLERY_IN_TUNING: MLACore::$process_mla_gallery_in = 'disabled' != $_REQUEST[MLA_OPTION_PREFIX . $key]; if ('refresh' == $_REQUEST[MLA_OPTION_PREFIX . $key]) { MLAQuery::mla_flush_mla_galleries(MLACore::MLA_MLA_GALLERY_IN_TUNING); /* translators: 1: reference type, e.g., Gallery in */ $message_list .= "<br>" . sprintf(_x('%1$s - references updated.', 'message_list', 'media-library-assistant'), __('MLA Gallery in', 'media-library-assistant')) . "\r\n"; $_REQUEST[MLA_OPTION_PREFIX . $key] = 'cached'; } break; case MLACore::MLA_TAXONOMY_SUPPORT: /* * Replace missing "checkbox" arguments with empty arrays, * denoting that all of the boxes are unchecked. */ if (!isset($_REQUEST['tax_support'])) { $_REQUEST['tax_support'] = array(); } if (!isset($_REQUEST['tax_quick_edit'])) { $_REQUEST['tax_quick_edit'] = array(); } if (!isset($_REQUEST['tax_term_search'])) { $_REQUEST['tax_term_search'] = array(); } if (!isset($_REQUEST['tax_flat_checklist'])) { $_REQUEST['tax_flat_checklist'] = array(); } if (!isset($_REQUEST['tax_checked_on_top'])) { $_REQUEST['tax_checked_on_top'] = array(); } break; case MLACore::MLA_SEARCH_MEDIA_FILTER_DEFAULTS: /* * Replace missing "checkbox" arguments with empty arrays, * denoting that all of the boxes are unchecked. */ if (!isset($_REQUEST['search_fields'])) { $_REQUEST['search_fields'] = array(); } break; default: // ignore everything else } // switch $message_list .= self::mla_update_option_row($key, $value); } // general option } // foreach mla_options $page_content = array('message' => __('General settings saved.', 'media-library-assistant') . "\r\n", 'body' => ''); /* * Uncomment this for debugging. */ // $page_content['message'] .= $message_list; return $page_content; }
/** * Prepares the list of items for displaying * * This is where you prepare your data for display. This method will usually * be used to query the database, sort and filter the data, and generally * get it ready to be displayed. At a minimum, we should set $this->items and * $this->set_pagination_args(). * * @since 0.1 */ function prepare_items() { // Initialize $this->_column_headers $this->get_column_info(); /* * Calculate and filter pagination arguments. */ $user = get_current_user_id(); $option = $this->screen->get_option('per_page', 'option'); $per_page = (int) get_user_meta($user, $option, true); if (empty($per_page) || $per_page < 1) { $per_page = (int) $this->screen->get_option('per_page', 'default'); } $current_page = isset($_REQUEST['paged']) ? absint($_REQUEST['paged']) : 1; $pagination = apply_filters_ref_array('mla_list_table_prepare_items_pagination', array(compact(array('per_page', 'current_page')), &$this)); $per_page = isset($pagination['per_page']) ? $pagination['per_page'] : $per_page; $current_page = isset($pagination['current_page']) ? $pagination['current_page'] : $current_page; /* * Assign sorted and paginated data to the items property, where * it can be used by the rest of the class. */ $total_items = apply_filters_ref_array('mla_list_table_prepare_items_total_items', array(NULL, &$this)); if (is_null($total_items)) { $total_items = MLAQuery::mla_count_list_table_items($_REQUEST, ($current_page - 1) * $per_page, $per_page); } /* * Register the pagination options & calculations. */ $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page))); $this->items = apply_filters_ref_array('mla_list_table_prepare_items_the_items', array(NULL, &$this)); if (is_null($this->items)) { $this->items = MLAQuery::mla_query_list_table_items($_REQUEST, ($current_page - 1) * $per_page, $per_page); } do_action_ref_array('mla_list_table_prepare_items', array(&$this)); }