Exemple #1
0
 /**
  * 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;
 }
 /**
  * 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)) {
         $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');
     $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);
         }
         // 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'] = $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;
 }
Exemple #3
0
/**
* 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();
								});
								jQuery('#cover_image_preview').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 
}
Exemple #4
0
/**
 * 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;
}
 /**
  * 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();
         }
     }
 }