function mbdb_activate()
{
    MBDB()->books->create_table();
    mbdb_set_up_roles();
    // insert defaults
    $mbdb_options = get_option('mbdb_options');
    mbdb_insert_default_retailers($mbdb_options);
    mbdb_insert_default_formats($mbdb_options);
    mbdb_insert_default_edition_formats($mbdb_options);
    mbdb_insert_default_social_media($mbdb_options);
    mbdb_insert_image('coming-soon', 'coming_soon_blue.jpg', $mbdb_options);
    mbdb_insert_image('goodreads', 'goodreads.png', $mbdb_options);
    update_option('mbdb_options', $mbdb_options);
    // SET DEFAULT OPTIONS FOR GRID SLUGS
    mbdb_set_default_tax_grid_slugs();
    mbdb_init();
    flush_rewrite_rules();
}
// End if class_exists check
/**
 * The main function responsible for returning the one true Mooberry Book Manager
 * Instance to functions everywhere.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: <?php $mbdb = MBDB(); ?>
 *
 * @since 3.0
 * @return object The one true Mooberry_Book_Manager Instance
 */
function MBDB()
{
    return Mooberry_Book_Manager::instance();
}
// Get MBDB Running
MBDB();
/*
add_filter('tc_include_cpt_in_archives' , 'mbdb_tc_archives');
function mbdb_tc_archives($include) {
	error_log('inclue cpt in archives');
	error_log(get_post_type());
	if (get_post_type() == 'mbdb_book' || get_post_type() == 'mbdb_tax_grid') {
		return false;
	} else {
		return $include;
	}
}
*/
 function migrate_data()
 {
     $import_books = get_option('mbdb_import_books');
     if ($import_books) {
         echo '<h4>' . __('Data already migrated. Mooberry Book Manager 3.0 is ready to use!', 'mooberry-book-manager') . '</h4>';
         return;
     }
     echo '<h4>' . __('Migrating Data...', 'mooberry-book-manager') . '</h4>';
     $success = MBDB()->books->import();
     if ($success === true) {
         echo '<h4>' . __('Success!', 'mooberry-book-manager') . '</h4>';
         update_option('mbdb_import_books', true);
     } else {
         global $wpdb;
         echo '<h4>' . sprintf(__('An error occured while migrating data to version 3.0. Please contact Mooberry Dreams at %s with the following error message.', 'mooberry-book-manager'), '<A HREF="mailto:bookmanager@mooberrydreams.com">bookmanager@mooberrydreams.com</A>') . '</h4><p><h4><b>' . __('Error:', 'mooberry-book-manager') . '</b></h3> <h3>' . $wpdb->last_error . '</h4></p>';
     }
 }
function mbdb_upgrade_to_3_0()
{
    // 0. CREATE TABLE
    MBDB()->books->create_table();
    /*
    // 1. IMPORT BOOK DATA
    // this needs to run just one time
    $import_books = get_option('mbdb_import_books');
    if (!$import_books || $import_books == null) {
    	$success = MBDB()->books->import();
    	if ($success === true) {
    		update_option('mbdb_import_books', true);
    	} else {
    		global $wpdb;
    		update_option('mbdb_error', $wpdb->last_error);
    		add_action( 'admin_notices', 'mbdb_admin_notice_db_error' );
    		
    	}
    }
    */
    // 2. UPDATE GRID OPTIONS
    // loop through all the pages with a book grid
    $grid_pages = get_posts(array('posts_per_page' => -1, 'post_type' => 'page', 'meta_query' => array(array('key' => '_mbdb_book_grid_display', 'value' => 'yes', 'compare' => '='))));
    foreach ($grid_pages as $page) {
        // group_by => level 1
        $level1 = get_post_meta($page->ID, '_mbdb_book_grid_group_by', true);
        if (!$level1) {
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_1', 'none');
        } else {
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_1', $level1);
        }
        // genre_group_by => level 2
        // tag_group_by => level 2
        // else level 2 => none
        if ($level1 == 'tag' || $level1 == 'genre') {
            $level2 = get_post_meta($page->ID, '_mbdb_book_grid_' . $level1 . '_group_by', true);
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_2', $level2);
        } else {
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_2', 'none');
            $level2 = null;
        }
        // genre_tag_group_by => level 3
        // tag_genre_group_by => level 3
        // level 4 => none
        // else level 3 => none
        if ($level2 == 'tag' || $level2 == 'genre') {
            $level3 = get_post_meta($page->ID, '_mbdb_book_grid_' . $level1 . '_' . $level2 . '_group_by', true);
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_3', $level3);
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_4', 'none');
        } else {
            update_post_meta($page->ID, '_mbdb_book_grid_group_by_level_3', 'none');
        }
    }
    // 3. SET DEFAULT OPTIONS FOR GRID SLUGS
    mbdb_set_default_tax_grid_slugs();
    // 4. update all books to have book short code
    // get all posts of type mbdb_book
    $books = get_posts(array('posts_per_page' => -1, 'post_type' => 'mbdb_book'));
    // unhook this function because wp_update_post will call it
    remove_action('save_post_mbdb_book', 'mbdb_save_excerpt');
    foreach ($books as $book) {
        // update the post, which calls save_post again
        wp_update_post(array('ID' => $book->ID, 'post_content' => '[mbdb_book]'));
    }
    // re-hook this function
    add_action('save_post_mbdb_book', 'mbdb_save_excerpt');
    // 5. INSERT DEFAULT SOCIAL MEDIA SITES
    $mbdb_options = get_option('mbdb_options');
    mbdb_insert_default_social_media($mbdb_options);
    update_option('mbdb_options', $mbdb_options);
    flush_rewrite_rules();
    wp_reset_postdata();
}
/**
 *  Return one group of books for the grid
 *  This is called recursively until the group "none" is found
 *  
 *  @since 1.0
 *  @since 3.0 re-factored
 *  
 *  @param [int] $level         the nested level of the grid we're currently one
 *  @param [array] $groups       the groups in grid
 *  @param [array] $current_group  the id of the current group. Could be if of a
 *  								 series, genre, publisher, illustrator, etc.
 *  @param [string] $selection     what selection of books for the grid ('all',
 *  								 'unpublished', 'series', etc.)
 *  @param [array] $selected_ids  ids of the selection
 *  @param [string] $sort          represents sort, ie 'titleA', 'titleD', 
 *  								'series, etc.
 *  @param [array] $book_ids      optional list of book_ids to filter by, useful
 *  								for add-on plugins to add on to grid (ie MA)
 *  
 *  @return array of books for this group
 *  
 *  @access public
 */
function mbdb_get_group($level, $groups, $current_group, $selection, $selected_ids, $sort, $book_ids)
{
    do_action('mbdb_book_grid_pre+get_group', $level, $groups, $current_group, $selection, $selected_ids, $sort, $book_ids);
    $books = array();
    $taxonomies = get_object_taxonomies('mbdb_book', 'objects');
    $tax_names = array_keys($taxonomies);
    switch ($groups[$level]) {
        // break the recursion by actually getting the books
        case 'none':
            $books = MBDB()->books->get_ordered_selection($selection, $selected_ids, $sort, $book_ids, $current_group);
            break;
        case 'publisher':
            $books = mbdb_get_books_by_publisher($level, $groups, $current_group, $selection, $selected_ids, $sort, $book_ids);
            break;
        default:
            // see if it's a taxonomy
            // don't just assume it's a taxonomy because it could be
            // that there's an add-on plugin (ie MA) that's added
            // a new group
            if (in_array('mbdb_' . $groups[$level], $tax_names)) {
                $books = mbdb_get_books_by_taxonomy($level, $groups, $current_group, $selection, $selected_ids, $sort, $book_ids);
            }
    }
    do_action('mbdb_book_grid_post_get_group', $level, $groups, $current_group, $selection, $selected_ids, $sort, $book_ids);
    return apply_filters('mbdb_book_grid_get_group_books', $books, $level, $groups, $current_group, $selection, $selected_ids, $sort, $book_ids);
}
Ejemplo n.º 6
0
function mbdb_get_meta_data($override, $object_id, $a)
{
    // if not a book post type, return what we got in
    if (get_post_type() != 'mbdb_book') {
        return $override;
    }
    // returns false if $object_id is not a valid book or field is not column
    // in custom table
    // otherwise returns book field data
    $book_data = MBDB()->books->get_data_by_post_meta($a['field_id'], $object_id);
    // only override the fields in the table
    if ($book_data !== false) {
        return $book_data;
    }
    // if doesn't match one of the columsn, return what we got in
    // so that CMB2 will retrieve the value from post meta
    return $override;
}
/**
 *  Outputs list of books in series with links to individual books
 *  except for the current book
 *  
 *  
 *  @since 1.0
 *  
 *  @param [string] $delim  list or comma
 *  @param [string] $series series to print out (term)
 *  @param [int] $bookID current bookid
 *  
 *  @return html output
 *  
 *  @access public
 */
function mbdb_series_list($delim, $series, $bookID)
{
    $classname = 'mbm-book-serieslist';
    $books = MBDB()->books->get_books_by_taxonomy(null, 'series', $series, 'series_order', 'ASC');
    if ($delim == 'list') {
        $list = '<ul class="' . $classname . '-list">';
    } else {
        $list = '';
    }
    foreach ($books as $book) {
        if ($delim == 'list') {
            $list .= '<li class="' . $classname . '-listitem">';
        }
        if ($book->book_id != $bookID) {
            $list .= '<A class="' . $classname . '-listitem-link" HREF="' . get_permalink($book->book_id) . '">';
        }
        $list .= '<span class="' . $classname . '-listitem-text">' . esc_html($book->post_title) . '</span>';
        if ($book->book_id != $bookID) {
            $list .= '</a>';
        }
        if ($delim == 'list') {
            $list .= '</li>';
        } else {
            $list .= ', ';
        }
    }
    if ($delim == 'list') {
        $list .= '</ul>';
    } else {
        // trim off the last space and comma
        $list = substr($list, 0, -2);
    }
    return $list;
}
 function widget($args, $instance)
 {
     extract($args);
     $mbdb_bookID = $instance['mbdb_bookID'];
     $mbdb_widget_type = apply_filters('mbdb_widget_type', $instance['mbdb_widget_type']);
     $book = null;
     do_action('mbdb_widget_pre_get_books', $instance);
     switch ($mbdb_widget_type) {
         case 'random':
             // get book ID of a random book
             $book = apply_filters('mbdb_widget_random_book_list', MBDB()->books->get_random_book(), $instance);
             break;
         case "newest":
             // get book ID of most recent book
             $book = apply_filters('mbdb_widget_newest_book_list', MBDB()->books->get_most_recent_book(), $instance);
             break;
         case "coming-soon":
             // get books with future or blank release dates
             $book = apply_filters('mbdb_widget_coming_soon_book_list', MBDB()->books->get_upcoming_book(), $instance);
             break;
         case "specific":
             // make sure seected book is still a valid book
             $book = apply_filters('mbdb_widget_specific_book_list', MBDB()->books->get($mbdb_bookID), $instance);
             break;
     }
     $book = apply_filters('mbdb_widget_book', $book, $instance);
     do_action('mbdb_widget_post_get_books', $instance, $book);
     //output
     if ($book == null) {
         $mbdb_bookID = 0;
         $mbdb_book_title = '';
     } else {
         $mbdb_bookID = $book->book_id;
         $mbdb_book_title = get_the_title($mbdb_bookID);
     }
     $mbdb_widget_title = apply_filters('mbdb_widget_title', $instance['mbdb_widget_title']);
     $mbdb_widget_show_title = apply_filters('mbdb_widget_show_title', $instance['mbdb_widget_show_title']);
     $mbdb_cover_size = apply_filters('mbdb_widget_cover_size', $instance['mbdb_widget_cover_size']);
     $mbdb_bookID = apply_filters('mbdb_widget_bookID', $mbdb_bookID);
     $mbdb_book_title = apply_filters('mbdb_widget_book_title', $mbdb_book_title);
     do_action('mbdb_widget_pre_display');
     echo $before_widget;
     echo $before_title . esc_html($mbdb_widget_title) . $after_title;
     if ($mbdb_bookID == 0) {
         echo apply_filters('mbdb_widget_no_books_found', '<em>' . __('No books found', 'mooberry-book-manager') . '</em>');
     } else {
         $image_src = $book->cover;
         $image_id = $book->cover_id;
         //$image_src = get_post_meta( $mbdb_bookID, '_mbdb_cover', true );
         $book_link = get_permalink($mbdb_bookID);
         if ($book_link != '') {
             do_action('mbdb_widget_pre_link', $book_link);
             echo '<A class="mbm-widget-link" HREF="' . esc_url($book_link) . '"> ';
         }
         if (!$image_src || $image_src == '') {
             // v3.0 check for placeholder image setting
             $show_placeholder_cover = mbdb_get_option('show_placeholder_cover');
             if (is_array($show_placeholder_cover)) {
                 if (in_array('widget', $show_placeholder_cover)) {
                     $image_src = mbdb_get_option('coming-soon');
                 }
             }
         }
         if ($image_src && $image_src !== '') {
             do_action('mbdb_widget_pre_image', $image_src);
             $alt = mbdb_get_alt_text($image_id, __('Book Cover:', 'mooberry-book-manager') . ' ' . $mbdb_book_title);
             echo '<img class="mbm-widget-cover" style="width:' . esc_attr($mbdb_cover_size) . 'px;padding-top:10px;" src="' . esc_url($image_src) . '" ' . $alt . '  /> ';
             do_action('mbdb_widget_post_image', $image_src);
         }
         if ($mbdb_widget_show_title == 'yes') {
             if ($mbdb_book_title != '') {
                 do_action('mbdb_widget_pre_book_title', $mbdb_book_title);
                 echo '<P><span class="mbm-widget-title">' . esc_html($mbdb_book_title) . '</span></P>';
                 do_action('mbdb_widget_post_book_title', $mbdb_book_title);
             }
         }
         if ($book_link != '') {
             echo '</A>';
             do_action('mbdb_widget_post_link');
         }
     }
     //echo '</div>' .
     echo $after_widget;
     do_action('mbdb_widget_post_display');
 }