/** * WP_Ajax hook for pb_delete_catalog_logo */ static function deleteLogo() { check_ajax_referer('pb-delete-catalog-logo'); $image_url = $_POST['filename']; $user_id = (int) $_POST['pid']; $book = get_active_blog_for_user($user_id); if (current_user_can_for_blog($book->blog_id, 'upload_files')) { switch_to_blog($book->blog_id); // Delete old images $old_id = \Pressbooks\Image\attachment_id_from_url($image_url); if ($old_id) { wp_delete_attachment($old_id, true); } update_user_meta($user_id, 'pb_catalog_logo', \Pressbooks\Image\default_cover_url()); restore_current_blog(); } // @see http://codex.wordpress.org/AJAX_in_Plugins#Error_Return_Values // Will append 0 to returned json string if we don't die() die; }
/** * Change default book cover from PNG to JPG */ function changeDefaultBookCover() { $post = $this->getMetaPost(); if ($post) { $pb_cover_image = get_post_meta($post->ID, 'pb_cover_image', true); if (preg_match('~assets/images/default-book-cover\\.png$~', $pb_cover_image)) { update_post_meta($post->ID, 'pb_cover_image', \Pressbooks\Image\default_cover_url()); Book::deleteBookObjectCache(); } } }
/** * Returns book information in a useful, string only, format. Data is converted to HTML. * * @return array */ static function getBookInformation($id = '') { // ----------------------------------------------------------------------------- // Is cached? // ----------------------------------------------------------------------------- if (!empty($id) && is_int($id)) { // @codingStandardsIgnoreLine $blog_id = $id; switch_to_blog($blog_id); } else { global $blog_id; } $cache_id = "book-inf-{$blog_id}"; $book_information = wp_cache_get($cache_id, 'pb'); if ($book_information) { return $book_information; } // ---------------------------------------------------------------------------- // Book Information // ---------------------------------------------------------------------------- $expected_array = array('pb_keywords_tags', 'pb_bisac_subject', 'pb_contributing_authors'); $expected_the_content = array('pb_custom_copyright', 'pb_about_unlimited'); $expected_url = array('pb_cover_image'); $book_information = array(); $meta = new Metadata(); $data = $meta->getMetaPostMetadata(); foreach ($data as $key => $val) { // Skip anything not prefixed with pb_ if (!preg_match('/^pb_/', $key)) { continue; } // We only care about strings if (is_array($val)) { if (false !== in_array($key, $expected_array)) { $val = implode(', ', $val); } else { $val = array_values($val); $val = array_pop($val); } } // Skip empty values if (!trim($val)) { continue; } if (false !== in_array($key, $expected_the_content)) { $val = wptexturize($val); $val = wpautop($val); } else { $val = htmlspecialchars($val, ENT_NOQUOTES | ENT_XHTML, 'UTF-8', false); } // Normalize URLs if (in_array($key, $expected_url)) { $val = set_url_scheme($val); } // Remove invisible control characters that break XML $val = \Pressbooks\Sanitize\remove_control_characters($val); $book_information[$key] = $val; } // Return our best guess if no book information has been entered. if (empty($book_information)) { $book_information['pb_title'] = get_bloginfo('name'); if (!function_exists('get_user_by')) { include ABSPATH . 'wp-includes/pluggable.php'; } $author = get_user_by('email', get_bloginfo('admin_email')); $book_information['pb_author'] = isset($author->display_name) ? $author->display_name : ''; $book_information['pb_cover_image'] = \Pressbooks\Image\default_cover_url(); } // ----------------------------------------------------------------------------- // Cache & Return // ----------------------------------------------------------------------------- wp_cache_set($cache_id, $book_information, 'pb', 86400); if (!empty($id) && is_int($id)) { restore_current_blog(); } return $book_information; }
/** * Set up default terms for Front Matter and Back Matter * Insert default part, chapter, front matter, and back matter * Insert default pages (Authors, Cover, TOC, About, Buy, and Access Denied) * Remove content generated by wp_install_defaults * Anything which needs to run on blog activation must go in this function */ private function wpmuActivate() { /** @var $wpdb \wpdb */ global $wpdb; \Pressbooks\Taxonomy::insertTerms(); $posts = array(array('post_title' => __('Main Body', 'pressbooks'), 'post_name' => 'main-body', 'post_type' => 'part', 'menu_order' => 1), array('post_title' => __('Introduction', 'pressbooks'), 'post_name' => 'introduction', 'post_content' => __('This is where you can write your introduction.', 'pressbooks'), 'post_type' => 'front-matter', 'menu_order' => 1), array('post_title' => __('Chapter 1', 'pressbooks'), 'post_name' => 'chapter-1', 'post_content' => __('This is the first chapter in the main body of the text. You can change the text, rename the chapter, add new chapters, and add new parts.', 'pressbooks'), 'post_type' => 'chapter', 'menu_order' => 1), array('post_title' => __('Appendix', 'pressbooks'), 'post_name' => 'appendix', 'post_content' => __('This is where you can add appendices or other back matter.', 'pressbooks'), 'post_type' => 'back-matter', 'menu_order' => 1), array('post_title' => __('Authors', 'pressbooks'), 'post_name' => 'authors', 'post_type' => 'page'), array('post_title' => __('Cover', 'pressbooks'), 'post_name' => 'cover', 'post_type' => 'page'), array('post_title' => __('Table of Contents', 'pressbooks'), 'post_name' => 'table-of-contents', 'post_type' => 'page'), array('post_title' => __('About', 'pressbooks'), 'post_name' => 'about', 'post_type' => 'page'), array('post_title' => __('Buy', 'pressbooks'), 'post_name' => 'buy', 'post_type' => 'page'), array('post_title' => __('Access Denied', 'pressbooks'), 'post_name' => 'access-denied', 'post_content' => __('This book is private, and accessible only to registered users. If you have an account you can login <a href="/wp-login.php">here</a>. You can also set up your own Pressbooks book at: <a href="http://pressbooks.com">Pressbooks.com</a>.', 'pressbooks'), 'post_type' => 'page'), array('post_title' => __('Custom CSS for Ebook', 'pressbooks'), 'post_name' => 'epub', 'post_type' => 'custom-css'), array('post_title' => __('Custom CSS for PDF', 'pressbooks'), 'post_name' => 'prince', 'post_type' => 'custom-css'), array('post_title' => __('Custom CSS for Web', 'pressbooks'), 'post_name' => 'web', 'post_type' => 'custom-css'), array('post_title' => __('Book Information', 'pressbooks'), 'post_name' => 'book-information', 'post_type' => 'metadata')); $post = array('post_status' => 'publish', 'comment_status' => 'open', 'post_author' => $this->user_id); $page = array('post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_content' => '<!-- Here be dragons.-->', 'post_author' => $this->user_id, 'tags_input' => __('Default Data', 'pressbooks')); update_option('blogdescription', __('Simple Book Production', 'pressbooks')); $parent_part = 0; $intro = 0; $appendix = 0; $chapter1 = 0; foreach ($posts as $item) { $exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s AND post_name = %s AND post_status = 'publish' ", array($item['post_title'], $item['post_type'], $item['post_name']))); if (empty($exists)) { if ('page' == $item['post_type']) { $data = array_merge($item, $page); } else { $data = array_merge($item, $post); } $newpost = wp_insert_post($data, true); if (!is_wp_error($newpost)) { switch ($item['post_name']) { case 'cover': $this->opts['page_on_front'] = (int) $newpost; break; case 'table-of-contents': $this->opts['page_for_posts'] = (int) $newpost; break; } if ('part' == $item['post_type']) { $parent_part = $newpost; } elseif ('chapter' == $item['post_type']) { $my_post = array(); $my_post['ID'] = $newpost; $my_post['post_parent'] = $parent_part; wp_update_post($my_post); $chapter1 = $newpost; } elseif ('front-matter' == $item['post_type']) { $intro = $newpost; } elseif ('back-matter' == $item['post_type']) { $appendix = $newpost; } elseif ('metadata' == $item['post_type']) { $metadata_id = $newpost; if (0 !== get_current_user_id()) { $user_info = get_userdata(get_current_user_id()); $name = $user_info->display_name; update_post_meta($metadata_id, 'pb_author', $name); } $locale = get_site_option('WPLANG'); if (!$locale) { $locale = 'en'; } else { $locale = array_search($locale, \Pressbooks\L10n\wplang_codes()); } update_post_meta($metadata_id, 'pb_title', get_option('blogname')); update_post_meta($metadata_id, 'pb_language', $locale); update_post_meta($metadata_id, 'pb_cover_image', \Pressbooks\Image\default_cover_url()); } } else { trigger_error($newpost->get_error_message(), E_USER_ERROR); } } } // Apply 'introduction' front matter type to 'introduction' post wp_set_object_terms($intro, 'introduction', 'front-matter-type'); // Apply 'appendix' front matter type to 'appendix' post wp_set_object_terms($appendix, 'appendix', 'back-matter-type'); // Apply 'standard' chapter type to 'chapter 1' post wp_set_object_terms($chapter1, 'standard', 'chapter-type'); // Remove content generated by wp_install_defaults if (!wp_delete_post(1, true)) { return; } if (!wp_delete_post(2, true)) { return; } if (!wp_delete_comment(1, true)) { return; } $this->opts['pb_activated'] = time(); refresh_blog_details($this->blog_id); }
/** * Render cover image widget * * @param $form_id * @param $cover_pid * @param $image_url * @param $ajax_action * @param $nonce * @param string $description (optional) */ function render_cover_image_box($form_id, $cover_pid, $image_url, $ajax_action, $nonce, $description = '') { ?> <div class="custom-metadata-field image"> <script type="text/javascript"> // <![CDATA[ jQuery.noConflict(); jQuery(document).ready(function($){ jQuery('#delete_cover_button').click(function(e) { if (!confirm('<?php esc_attr_e('Are you sure you want to delete this?', 'pressbooks'); ?> ')) { e.preventDefault(); return false; } var image_file = jQuery(this).attr('name'); var pid = jQuery('#cover_pid').attr('value'); jQuery.ajax({ url: ajaxurl, type: 'POST', data: { action: '<?php echo $ajax_action; ?> ', filename: image_file, pid: pid, _ajax_nonce: '<?php echo $nonce; ?> ' }, success: function(data) { jQuery('#delete_cover_button').remove(); jQuery("#cover_image_preview").fadeOut("slow", function () { jQuery("#cover_image_preview").load(function () { //avoiding blinking, wait until loaded jQuery("#cover_image_preview").fadeIn(); }) .attr('src', '<?php echo \Pressbooks\Image\default_cover_url(); ?> '); }); } }); }); }); // ]]> </script> <div class="<?php echo $form_id; ?> " id="<?php echo $form_id; ?> -1"> <?php if ($image_url && !\Pressbooks\Image\is_default_cover($image_url)) { ?> <p><img id="cover_image_preview" src="<?php echo $image_url; ?> " style="width:auto;height:100px;" alt="cover_image" /><br /> <button id="delete_cover_button" name="<?php echo $image_url; ?> " type="button" class="button-secondary" ><?php _e('Delete', 'pressbooks'); ?> </button></p> <p><input type="file" name="<?php echo $form_id; ?> " value="" id="<?php echo $form_id; ?> " /></p> <input type="hidden" id="cover_pid" name="cover_pid" value="<?php echo $cover_pid; ?> " /> <?php } else { ?> <p><img id="cover_image_preview" src="<?php echo \Pressbooks\Image\default_cover_url(); ?> " style="width:auto;height:100px;" alt="cover_image" /></p> <p><input type="file" name="<?php echo $form_id; ?> " value="<?php echo $image_url; ?> " id="<?php echo $form_id; ?> " /></p> <?php } ?> <?php if ($description) { ?> <span class="description"><?php echo $description; ?> </span><?php } ?> </div> </div> <?php }
/** * WP_Ajax hook for pb_delete_cover_image */ function delete_cover_image() { if (current_user_can_for_blog(get_current_blog_id(), 'upload_files') && check_ajax_referer('pb-delete-cover-image')) { $image_url = $_POST['filename']; $pid = $_POST['pid']; // Delete old images $old_id = \Pressbooks\Image\attachment_id_from_url($image_url); if ($old_id) { wp_delete_attachment($old_id, true); } update_post_meta($pid, 'pb_cover_image', \Pressbooks\Image\default_cover_url()); \Pressbooks\Book::deleteBookObjectCache(); } // @see http://codex.wordpress.org/AJAX_in_Plugins#Error_Return_Values // Will append 0 to returned json string if we don't die() die; }