function mbdb_register_script() { $current_screen = get_current_screen(); if (!$current_screen) { return; } $parent_base = $current_screen->parent_base; $post_type = $current_screen->post_type; $base = $current_screen->base; if ($parent_base == 'edit' && $post_type == 'page' && $base == 'post') { // admin-book-grid $group_by_options = mbdb_book_grid_group_by_options(); $text_to_translate = array('label1' => __('Group Books Within', 'mooberry-book-manager'), 'label2' => __('By', 'mooberry-book-manager'), 'groupby' => $group_by_options); wp_register_script('mbdb-admin-book-grid', MBDB_PLUGIN_URL . 'includes/js/admin-book-grid.js', array('jquery'), mbdb_get_enqueue_version()); wp_localize_script('mbdb-admin-book-grid', 'text_to_translate', $text_to_translate); wp_enqueue_script('mbdb-admin-book-grid'); } if ($base == 'widgets') { // admin-widget wp_enqueue_script('mbdb-admin-widget', MBDB_PLUGIN_URL . 'includes/js/admin-widget.js', '', mbdb_get_enqueue_version()); } if ($post_type == 'mbdb_book' && $base == 'post') { // admin-book wp_register_script('mbdb-admin-book', MBDB_PLUGIN_URL . 'includes/js/admin-book.js', '', mbdb_get_enqueue_version()); wp_localize_script('mbdb-admin-book', 'display_editions', 'no'); wp_enqueue_script('mbdb-admin-book'); } if ($parent_base == 'mbdb_options') { // admin-settings wp_enqueue_script('mbdb-admin-options', MBDB_PLUGIN_URL . 'includes/js/admin-options.js', '', mbdb_get_enqueue_version()); wp_localize_script('mbdb-admin-options', 'mbdb_admin_options_ajax', array('translation' => __('Are you sure you want to reset the Book Edit page?', 'mooberry-book-manager'), 'ajax_url' => admin_url('admin-ajax.php'), 'ajax_nonce' => wp_create_nonce('mbdb_admin_options_ajax_nonce'))); } }
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); }
/** * Get the data and generate output content for the book grid * * * * @since 1.0 * * @return content to be displayed * * @access public */ function mbdb_bookgrid_content() { global $post; $content = ''; $mbdb_book_grid_meta_data = get_post_meta($post->ID); // VALIDATE THE INPUTS // loop through the group by levels // set up the group arrays // stop at the first one that is none // if there is a series, add none after that and stop $groups = array(); $current_group = array(); $group_by_levels = mbdb_book_grid_group_by_options(); for ($x = 1; $x < count($group_by_levels); $x++) { if (!array_key_exists('_mbdb_book_grid_group_by_level_' . $x, $mbdb_book_grid_meta_data)) { $groups[$x] = 'none'; $current_group['none'] = 0; break; } $group_by_dropdown = $mbdb_book_grid_meta_data['_mbdb_book_grid_group_by_level_' . $x][0]; $groups[$x] = $group_by_dropdown; $current_group[$group_by_dropdown] = 0; if ($group_by_dropdown == 'none') { break; } if ($group_by_dropdown == 'series') { $groups[$x + 1] = 'none'; $current_group['none'] = 0; break; } } // set the sort if (array_key_exists('_mbdb_book_grid_order', $mbdb_book_grid_meta_data)) { $sort = mbdb_set_sort($groups, $mbdb_book_grid_meta_data['_mbdb_book_grid_order'][0]); } else { $sort = mbdb_set_sort($groups, 'titleA'); } // if selection isn't set, default to "all" if (array_key_exists('_mbdb_book_grid_books', $mbdb_book_grid_meta_data)) { $selection = $mbdb_book_grid_meta_data['_mbdb_book_grid_books'][0]; } else { $selection = 'all'; } // turn selected_ids into an array // or null if there aren't any if (array_key_exists('_mbdb_book_grid_' . $selection, $mbdb_book_grid_meta_data)) { $selected_ids = unserialize($mbdb_book_grid_meta_data['_mbdb_book_grid_' . $selection][0]); } else { $selected_ids = null; } // start off the recursion by getting the first group $level = 1; $books = mbdb_get_group($level, $groups, $current_group, $selection, $selected_ids, $sort, null); // $books now contains the complete array of books to display in the grid // get the display output content $content = mbdb_display_grid($books, 0); // find all the book grid's postmeta so we can display it in comments for debugging purposes $grid_values = array(); foreach ($mbdb_book_grid_meta_data as $key => $data) { if (substr($key, 0, 5) == '_mbdb') { $grid_values[$key] = $data[0]; } } $content = '<!-- Grid Parameters: ' . print_r($grid_values, true) . ' -->' . $content; // add on bottom text if (array_key_exists('_mbdb_book_grid_description_bottom', $mbdb_book_grid_meta_data)) { $book_grid_description_bottom = $mbdb_book_grid_meta_data['_mbdb_book_grid_description_bottom'][0]; } else { $book_grid_description_bottom = ''; } return $content . $book_grid_description_bottom; }