function mbdb_book_grid_meta_boxes() { $mbdb_book_grid_metabox = new_cmb2_box(array('id' => 'mbdb_book_grid_metabox', 'title' => __('Book Grid Settings', 'mooberry-book-manager'), 'object_types' => array('page'), 'context' => 'normal', 'priority' => 'default', 'show_names' => true)); $mbdb_book_grid_metabox->add_field(array('name' => __('Display Books on This Page?', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_display', 'type' => 'select', 'default' => 'no', 'options' => array('yes' => __('Yes', 'mooberry-book-manager'), 'no' => __('No', 'mooberry-book-manager')))); $mbdb_book_grid_metabox->add_field(array('name' => __('Books to Display', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_books', 'type' => 'select', 'options' => mbdb_book_grid_selection_options())); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Books', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_custom', 'type' => 'multicheck', 'options' => mbdb_get_book_array())); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Genres', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_genre', 'type' => 'multicheck', 'options' => mbdb_get_term_options('mbdb_genre'))); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Series', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_series', 'type' => 'multicheck', 'options' => mbdb_get_term_options('mbdb_series'))); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Tags', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_tag', 'type' => 'multicheck', 'options' => mbdb_get_term_options('mbdb_tag'))); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Publishers', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_publisher', 'type' => 'multicheck', 'options' => mbdb_get_publishers('no'))); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Editors', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_editor', 'type' => 'multicheck', 'options' => mbdb_get_term_options('mbdb_editor'))); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Illustrators', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_illustrator', 'type' => 'multicheck', 'options' => mbdb_get_term_options('mbdb_illustrator'))); $mbdb_book_grid_metabox->add_field(array('name' => __('Select Cover Artists', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_cover_artist', 'type' => 'multicheck', 'options' => mbdb_get_term_options('mbdb_cover_artist'))); $group_by_options = mbdb_book_grid_group_by_options(); $count = count($group_by_options); for ($x = 1; $x < $count; $x++) { $mbdb_book_grid_metabox->add_field(array('name' => __('Group Books By', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_group_by_level_' . $x, 'type' => 'select', 'options' => $group_by_options)); // put a warning at the 5th level if ($x == 5) { $mbdb_book_grid_metabox->add_field(array('name' => __('Warning: Setting more than 5 levels could cause the page to timeout and not display.', 'mooberry-book-manager'), 'type' => 'title', 'id' => '_mbdb_book_grid_warning', 'attributes' => array('display' => 'none'))); } } $mbdb_book_grid_metabox->add_field(array('name' => __('Order By', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_order', 'type' => 'select', 'options' => mbdb_book_grid_order_options())); $mbdb_book_grid_metabox->add_field(array('name' => __('Use default cover height?', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_cover_height_default', 'type' => 'select', 'default' => 'yes', 'options' => array('yes' => __('Yes', 'mooberry-book-manager'), 'no' => __('No', 'mooberry-book-manager')))); $mbdb_book_grid_metabox->add_field(array('name' => __('Book Cover Height (px)', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_cover_height', 'type' => 'text_small', 'attributes' => array('type' => 'number', 'pattern' => '\\d*', 'min' => 50))); $mbdb_book_grid_metabox->add_field(array('name' => __('Additional Content (bottom)', 'mooberry-book-manager'), 'id' => '_mbdb_book_grid_description_bottom', 'type' => 'wysiwyg', 'description' => __('This displays under the book grid.', 'mooberry-book-manager'), 'options' => array('wpautop' => true, 'media_buttons' => true, 'textarea_rows' => 10, 'tabindex' => '', 'editor_css' => '', 'editor_class' => '', 'teeny' => false, 'dfw' => false, 'tinymce' => true, 'quicktags' => true))); $mbdb_book_grid_metabox = apply_filters('mbdb_book_grid_meta_boxes', $mbdb_book_grid_metabox); }
public function get_ordered_selection($selection, $selection_ids, $sort, $book_ids = null, $taxonomy = null) { // selection_ids = book_ids if selection = "custom" // = tax_ids if selection is a taxonomy // = publisher_ids if selection = "publisher" // should be null otherwise // book_ids is an optional, additional filtering of book ids // taxonmy: array of taxonomy and id(s) to filter on. Also includes publisher. // Examples: // { series => 24 } // { genre => { 20, 21 } } // { publisher => 15 } // publisher is expected to be a single value. If it's an array, only the 1st is selected // $sort = titleA, titleD, pubdateA, pubdateD, series_order // OR { field, direction } ie { release_date, DESC } // validate inputs // SORT VARIABLES //if an array is passed in, separate into field, direction if (is_array($sort)) { list($sort, $order) = $sort; } else { // otherwise, set field, direction based on value list($sort, $order) = $this->get_sort_fields($sort); } // if getting books by series, the sort should be by series aascending if ($taxonomy == null && $selection == 'series') { $sort = 'series_order'; $order = 'ASC'; } else { if ($taxonomy != null && array_key_exists('series', $taxonomy)) { $sort = 'series_order'; $order = 'ASC'; } } // ensure that the sort field is a column in the table // and that the direction is either ASC or DESC $sort = $this->validate_orderby($sort); $order = $this->validate_order($order); // SELECTION VARIABLES // default to all books $book_selection_options = mbdb_book_grid_selection_options(); if (!array_key_exists($selection, $book_selection_options)) { $selection = 'all'; } $taxonomies = array('genre', 'series', 'tag', 'illustrator', 'editor', 'cover_artist'); // if custom, genre, series, tag, illustrator, editor, cover artist, or publisher and no selection ids are passed, default to all books // otherwise if selection ids is not an array, make it an array if (in_array($selection, array_merge(array('custom', 'publisher'), $taxonomies))) { if ($selection_ids == null || $selection_ids == '') { $selection = 'all'; } else { if (!is_array($selection_ids)) { $selection_ids = array($selection_ids); } } } // TAXONOMY ARRAY // if taxonomy is supplied, the keys must be one of the options if ($taxonomy) { $tax_options = array_keys(mbdb_book_grid_group_by_options()); foreach ($taxonomy as $tax => $tax_ids) { if (!in_array($tax, $tax_options)) { unset($taxonomy[$tax]); } } if (count($taxonomy) == 0) { $taxonomy = null; } } $select = 'SELECT DISTINCT '; $join = ' JOIN ' . $this->prefix() . 'posts p ON p.id = b.book_id '; $where = ' WHERE p.post_status = "publish" '; $orderby = ' ORDER BY '; // if book_ids are sent, filter by them if ($book_ids != null) { if (!is_array($book_ids)) { $book_ids = array($book_ids); } $book_ids = array_map('absint', $book_ids); $where .= ' AND (book_id in (' . implode(', ', $book_ids) . ') ) '; } // set the where clause switch ($selection) { case 'all': // no change // this is included only so it doesn't fall into the "default" $where .= ''; break; case 'published': $where .= ' AND ( release_date <= CURRENT_DATE() ) '; break; case 'unpublished': $where .= ' AND ( release_date > CURRENT_DATE() OR release_date IS NULL ) '; break; case 'custom': $selection_ids = array_map('absint', $selection_ids); $where .= ' AND (book_id in (' . implode(', ', $selection_ids) . ') ) '; break; case 'publisher': $selection_ids = array_map('esc_sql', $selection_ids); $where .= ' AND ( b.publisher_id in ( "' . implode('", "', $selection_ids) . '" ) ) '; break; default: // anything else is a taxonomy, a type handled by another add-on, or an // invalid input // if it's a taxonomy, add where and join if (in_array($selection, $taxonomies)) { $selection_ids = array_map('absint', $selection_ids); $where .= ' AND ( tt.taxonomy = "mbdb_' . $selection . '" AND tt.term_id in ( ' . implode(', ', $selection_ids) . ' ) AND p.post_type = "mbdb_book" ) '; $join .= ' JOIN ' . $this->prefix() . 'term_relationships AS tr ON tr.object_id = b.book_id JOIN ' . $this->prefix() . 'term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id '; } break; } // add in taxonomy filtering if ($taxonomy != null) { $tax_level = 2; foreach ($taxonomy as $tax => $tax_ids) { switch ($tax) { case 'none': // no additional filtering needed, this is the innermost level break 2; case 'publisher': //if -1 then get books that don't have a publisher if ($tax_ids == -1) { $select .= '"" AS name' . $tax_level . ', '; $where .= ' AND (b.publisher_id IS NULL) '; } else { if (is_array($tax_ids)) { $tax_ids = $tax_ids[0]; } $publisher = mbdb_get_publisher_info($tax_ids); if ($publisher != null && array_key_exists('name', $publisher)) { $select .= '"' . $publisher['name'] . '" as name' . $tax_level . ', '; $where .= ' AND (b.publisher_id ="' . esc_sql($tax_ids) . '") '; } } break; // anything left is a taxonomy // anything left is a taxonomy default: if (in_array($tax, $taxonomies)) { // if -1 then get books that are NOT in any of this taxonomny if ($tax_ids == -1) { $select .= ' "" AS name' . $tax_level . ', '; $where .= ' and b.book_id not in (select book_id from ' . $this->table_name . ' as b join ' . $this->prefix() . 'term_relationships as tr3 on tr3.object_id = b.book_id join ' . $this->prefix() . 'term_taxonomy tt3 on tt3.term_taxonomy_id = tr3.term_taxonomy_id where tt3.taxonomy = "mbdb_' . $tax . '" ) '; } else { if (!is_array($tax_ids)) { $tax_ids = array($tax_ids); } $tax_ids = array_map('absint', $tax_ids); $select .= 't' . $tax_level . '.name AS name' . $tax_level . ', '; $where .= ' AND (tt' . $tax_level . '.taxonomy = "mbdb_' . $tax . '" AND tt' . $tax_level . '.term_id in (' . implode(',', $tax_ids) . ') ) '; $join .= ' JOIN ' . $this->prefix() . 'term_relationships AS tr' . $tax_level . ' ON tr' . $tax_level . '.object_id = b.book_id JOIN ' . $this->prefix() . 'term_taxonomy AS tt' . $tax_level . ' ON tt' . $tax_level . '.term_taxonomy_id = tr' . $tax_level . '.term_taxonomy_id JOIN ' . $this->prefix() . 'terms AS t' . $tax_level . ' ON t' . $tax_level . '.term_id = tt' . $tax_level . '.term_id'; } } break; } $tax_level++; } } // set the order switch ($sort) { case 'release_date': // sort null dates last $orderby .= ' CASE WHEN release_date IS NULL THEN 1 ELSE 0 END, release_date '; break; case 'series_order': // sort null orders last $orderby .= ' CASE WHEN series_order IS NULL THEN 999 ELSE 0 END, series_order '; break; default: $orderby .= ' post_title '; break; } $select = apply_filters('mbdb_book_get_ordered_selection_select', $select); $join = apply_filters('mbdb_book_get_ordered_selection_join', $join); $where = apply_filters('mbdb_book_get_ordered_selection_where', $where, $selection_ids, $selection, $book_ids); $orderby = apply_filters('mbdb_book_get_ordered_selection_orderby', $orderby, $sort, $order); $sql = "{$select} b.book_id, p.post_title, b.cover, b.release_date, b.cover_id FROM {$this->table_name} as b {$join} {$where} {$orderby} {$order} "; $books = $this->run_sql($sql); return apply_filters('mbdb_book_get_ordered_selection', $books, $selection, $selection_ids, $sort, $order, $taxonomy, $book_ids); }