Skip to content

Commit

Permalink
Delete book (fixes #845) (#864)
Browse files Browse the repository at this point in the history
* Re-enable ms-delete-site.php

* Change text.

* Add tests

* Make ::init() consistent in PB code - different than ::getInstance()
  • Loading branch information
dac514 authored and Ned Zimmerman committed Jul 26, 2017
1 parent ad968f9 commit 42c3eb2
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 79 deletions.
1 change: 1 addition & 0 deletions hooks-admin.php
Expand Up @@ -55,6 +55,7 @@
add_action( 'init', [ '\Pressbooks\Modules\ThemeOptions\ThemeOptions', 'init' ] );
add_action( 'admin_init', '\Pressbooks\Admin\Laf\redirect_away_from_bad_urls' );
add_action( 'admin_menu', '\Pressbooks\Admin\Laf\replace_book_admin_menu', 1 );
add_action( 'admin_menu', [ '\Pressbooks\Admin\Delete\Book', 'init' ] );
add_filter( 'parent_file', '\Pressbooks\Admin\Laf\fix_parent_file' );
add_filter( 'submenu_file', '\Pressbooks\Admin\Laf\fix_submenu_file', 10, 2 );
add_action( 'wp_dashboard_setup', '\Pressbooks\Admin\Dashboard\replace_dashboard_widgets' );
Expand Down
6 changes: 3 additions & 3 deletions hooks.php
Expand Up @@ -197,9 +197,9 @@
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12 ); // execute wpautop after shortcode processing

$_ = \Pressbooks\Shortcodes\Footnotes\Footnotes::getInstance();
$_ = \Pressbooks\Shortcodes\Generics\Generics::getInstance();
$_ = \Pressbooks\Shortcodes\WikiPublisher\Glyphs::getInstance();
$_ = \Pressbooks\Shortcodes\Footnotes\Footnotes::init();
$_ = \Pressbooks\Shortcodes\Generics\Generics::init();
$_ = \Pressbooks\Shortcodes\WikiPublisher\Glyphs::init();

// Theme Lock
if ( $is_book && Lock::isLocked() ) {
Expand Down
81 changes: 81 additions & 0 deletions inc/admin/delete/class-book.php
@@ -0,0 +1,81 @@
<?php
/**
* @author Pressbooks <code@pressbooks.com>
* @license GPLv2 (or any later version)
*/

namespace Pressbooks\Admin\Delete;

class Book {

/**
* @var \Pressbooks\Admin\Delete\Book
*/
private static $instance = null;

/**
* @return \Pressbooks\Admin\Delete\Book
*/
static function init() {
if ( is_null( self::$instance ) ) {
$self = new self;
// Hide from side menu
remove_submenu_page( 'tools.php', 'ms-delete-site.php' );
// Add to top menu
if ( current_user_can( 'delete_site' ) ) {
add_action( 'admin_bar_menu', [ $self, 'addMenu' ], 31 );
}
// Override delete site email
add_filter( 'delete_site_email_content', [ $self, 'deleteBookEmailContent' ] );
$self::$instance = $self;
}
return self::$instance;
}

/**
*
*/
public function __construct() {
}

/**
* @param \WP_Admin_Bar $wp_admin_bar
*/
public function addMenu( $wp_admin_bar ) {
$wp_admin_bar->add_node(
[
'parent' => 'site-name',
'id' => 'delete-book',
'title' => __( 'Delete Book', 'pressbooks' ),
'href' => admin_url( 'ms-delete-site.php' ),
]
);
}

/**
* @param string $content
*
* @return string
*/
public function deleteBookEmailContent( $content ) {

/* translators: Do not translate USERNAME, URL_DELETE, SITE_NAME: those are placeholders. */
$content = __( "Howdy ###USERNAME###,
You recently clicked the 'Delete Book' link on your book and filled in a
form on that page.
If you really want to delete your book, click the link below. You will not
be asked to confirm again so only click this link if you are absolutely certain:
###URL_DELETE###
If you delete your book, please consider starting a new book project with us
some time in the future! (But remember your current book
is gone forever.)
Thanks for using Pressbooks,
###SITE_NAME###", 'pressbooks' );

return $content;
}
}
1 change: 0 additions & 1 deletion inc/admin/laf/namespace.php
Expand Up @@ -90,7 +90,6 @@ function replace_book_admin_menu() {
remove_submenu_page( 'tools.php', 'tools.php' );
remove_submenu_page( 'tools.php', 'import.php' );
remove_submenu_page( 'tools.php', 'export.php' );
remove_submenu_page( 'tools.php', 'ms-delete-site.php' );

remove_submenu_page( 'edit.php?post_type=chapter', 'edit.php?post_type=chapter' );

Expand Down
19 changes: 10 additions & 9 deletions inc/modules/searchandreplace/class-searchandreplace.php
Expand Up @@ -23,7 +23,10 @@ class SearchAndReplace {
static function init() {
if ( is_admin() ) {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
$self = new self;
add_filter( 'admin_menu', [ $self, 'adminMenu' ] );
add_action( 'load-tools_page_pressbooks-search-and-replace', [ $self, 'searchHead' ] );
self::$instance = $self;
}
return self::$instance;
}
Expand All @@ -33,15 +36,13 @@ static function init() {
/**
* Constructor
*/
function __construct() {
add_filter( 'admin_menu', [ $this, 'adminMenu' ] );
add_action( 'load-tools_page_pressbooks-search-and-replace', [ $this, 'searchHead' ] );
public function __construct() {
}

/**
* Load styles and scripts
*/
function searchHead() {
public function searchHead() {
$assets = new Assets( 'pressbooks', 'plugin' );
wp_enqueue_style( 'search-and-replace', $assets->getPath( 'styles/search-and-replace.css' ) );
wp_register_script( 'search-and-replace', $assets->getPath( 'scripts/search-and-replace.js' ) );
Expand All @@ -52,7 +53,7 @@ function searchHead() {
/**
* @return array
*/
function getL10n() {
public function getL10n() {
return [
'warning_text' => __( 'Once you&rsquo;ve pressed &lsquo;Replace & Save&rsquo; there is no going back! Have you checked &lsquo;Preview Replacements&rsquo; to make sure this will do what you want it to do?', 'pressbooks' ),
];
Expand All @@ -61,7 +62,7 @@ function getL10n() {
/**
* Add admin page
*/
function adminMenu() {
public function adminMenu() {
if ( current_user_can( 'administrator' ) ) {
add_management_page(
__( 'Search & Replace', 'pressbooks' ),
Expand All @@ -76,7 +77,7 @@ function adminMenu() {
/**
* Callable for add_management_page
*/
function adminScreen() {
public function adminScreen() {
$searches = Search::getSearches();

if ( isset( $_POST['search_pattern'] ) && ! wp_verify_nonce( $_POST['pressbooks-search-and-replace-nonce'], 'search' ) ) {
Expand Down Expand Up @@ -157,7 +158,7 @@ private function render( $template, $template_vars = [] ) {
}
}

function renderError( $message ) {
public function renderError( $message ) {
?>
<div class="fade error" id="message">
<p><?php echo $message ?></p>
Expand Down
43 changes: 18 additions & 25 deletions inc/shortcodes/footnotes/class-footnotes.php
Expand Up @@ -25,41 +25,34 @@ class Footnotes {
*/
var $numbered = [];

/**
* This is our constructor, which is private to force the use of getInstance()
*/
private function __construct() {

add_shortcode( 'footnote', [ $this, 'shortcodeHandler' ] );
add_filter(
'no_texturize_shortcodes', function ( $excluded_shortcodes ) {
$excluded_shortcodes[] = 'footnote';
return $excluded_shortcodes;
}
);

// do_shortcode() is registered as a default filter on 'the_content' with a priority of 11.
// We need to run $this->footNoteContent() after this, set to 12
add_filter( 'the_content', [ $this, 'footnoteContent' ], 12 );

add_action( 'init', [ $this, 'footnoteButton' ] ); // TinyMCE button
add_action( 'admin_enqueue_scripts', [ $this, 'myCustomQuicktags' ] ); // Quicktag button
}


/**
* Function to instantiate our class and make it a singleton
*
* @return Footnotes
*/
public static function getInstance() {
public static function init() {
if ( ! self::$instance ) {
self::$instance = new self;
$self = new self;
add_shortcode( 'footnote', [ $self, 'shortcodeHandler' ] );
add_filter( 'no_texturize_shortcodes', function ( $excluded_shortcodes ) {
$excluded_shortcodes[] = 'footnote';
return $excluded_shortcodes;
} );
// do_shortcode() is registered as a default filter on 'the_content' with a priority of 11.
// We need to run $this->footNoteContent() after this, set to 12
add_filter( 'the_content', [ $self, 'footnoteContent' ], 12 );
add_action( 'init', [ $self, 'footnoteButton' ] ); // TinyMCE button
add_action( 'admin_enqueue_scripts', [ $self, 'myCustomQuicktags' ] ); // Quicktag button
self::$instance = $self;
}

return self::$instance;
}

/**
*
*/
public function __construct() {
}

/**
* Pre-process footnote shortcode
Expand Down
33 changes: 16 additions & 17 deletions inc/shortcodes/generics/class-generics.php
Expand Up @@ -28,10 +28,24 @@ class Generics {
'textbox' => [ 'div', 'textbox' ],
];

function __construct() {
/**
* Adds shortcodes based on $self->generics.
*/
public static function init() {
if ( ! self::$instance ) {
$self = new self;
foreach ( $self->shortcodes as $shortcode => $tag ) {
add_shortcode( $shortcode, [ $self, 'shortcodeHandler' ] );
}
self::$instance = $self;
}
return self::$instance;
}

function shortcodeHandler( $atts, $content = '', $shortcode ) {
public function __construct() {
}

public function shortcodeHandler( $atts, $content = '', $shortcode ) {
$tag = $this->shortcodes[ $shortcode ];

if ( ! $content ) {
Expand All @@ -52,19 +66,4 @@ function shortcodeHandler( $atts, $content = '', $shortcode ) {
$content = wpautop( trim( $content ) );
return '<' . $tag . $class . '>' . do_shortcode( $content ) . '</' . $tag . '>';
}

/**
* Adds shortcodes based on $self->generics.
*/
static function getInstance() {
if ( ! self::$instance ) {
self::$instance = new self;
}

foreach ( self::$instance->shortcodes as $shortcode => $tag ) {
add_shortcode( $shortcode, [ self::$instance, 'shortcodeHandler' ] );
}

return self::$instance;
}
}
35 changes: 13 additions & 22 deletions inc/shortcodes/wikipublisher/class-glyphs.php
Expand Up @@ -27,47 +27,38 @@ class Glyphs {
'hbo', // Ancient Hebrew
];


/**
* Setup the [pb_language] shortcode, which is private to force the use of getInstance()
* Function to instantiate our class and make it a singleton
*
* @deprecated
* @return Glyphs
*/
private function __construct() {

add_shortcode( 'pb_language', [ $this, 'langShortcode' ] );
add_filter(
'no_texturize_shortcodes', function ( $excluded_shortcodes ) {
public static function init() {
if ( ! self::$instance ) {
$self = new self;
add_shortcode( 'pb_language', [ $self, 'langShortcode' ] );
add_filter( 'no_texturize_shortcodes', function ( $excluded_shortcodes ) {
$excluded_shortcodes[] = 'pb_language';
return $excluded_shortcodes;
}
);

} );
self::$instance = $self;
}
return self::$instance;
}


/**
* Function to instantiate our class and make it a singleton
*
* @deprecated
* @return Glyphs
*/
public static function getInstance() {
if ( ! self::$instance ) {
self::$instance = new self;
}

return self::$instance;
public function __construct() {
}


/**
* @param $atts
* @param null $content
*
* @return string
*/
function langShortcode( $atts, $content = null ) {
public function langShortcode( $atts, $content = null ) {

$a = shortcode_atts(
[
Expand Down
8 changes: 8 additions & 0 deletions languages/core-en-us.php
Expand Up @@ -16,6 +16,14 @@
$overrides['Visit Site'] = 'Visit Book';
$overrides['Edit Site'] = 'Edit Book';
$overrides['You have used your space quota. Please delete files before uploading.'] = 'Sorry, you have used all of your storage quota. Want more space? Please upgrade your book.';
$overrides['Delete Site'] = 'Delete Book';
$overrides['Delete My Site'] = 'Delete My Book';
$overrides['Delete My Site Permanently'] = 'Delete My Book Permanently';
$overrides["I'm sure I want to permanently disable my site, and I am aware I can never get it back or use %s again."] = "I'm sure I want to permanently disable my book, and I am aware I can never get it back or use %s again.";
$overrides['If you do not want to use your %s site any more, you can delete it using the form below. When you click <strong>Delete My Site Permanently</strong> you will be sent an email with a link in it. Click on this link to delete your site.'] = 'If you do not want to use your %s book any more, you can delete it using the form below. When you click <strong>Delete My Book Permanently</strong> you will be sent an email with a link in it. Click on this link to delete your book.';
$overrides['Remember, once deleted your site cannot be restored.'] = 'Remember, once deleted, your book cannot be restored.';
$overrides['Thank you. Please check your email for a link to confirm your action. Your site will not be deleted until this link is clicked.'] = 'Thank you. Please check your email for a link to confirm your action. Your book will not be deleted until this link is clicked.';
$overrides['Thank you for using %s, your site has been deleted. Happy trails to you until we meet again.' ] = 'Thank you for using %s, your book has been deleted.';
}

return $overrides;
22 changes: 22 additions & 0 deletions tests/test-admin-delete-book.php
@@ -0,0 +1,22 @@
<?php

class Admin_DeleteBookTest extends \WP_UnitTestCase {

public function test_deleteBookEmailContent() {
$delete_book = new \Pressbooks\Admin\Delete\Book();
$content = $delete_book->deleteBookEmailContent( 'WordPress' );
$this->assertNotContains( 'WordPress,', $content );
$this->assertContains( 'Pressbooks', $content );
}

public function test_addMenu() {
$delete_book = new \Pressbooks\Admin\Delete\Book();
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';
$admin_bar = new \WP_Admin_Bar();
$delete_book->addMenu( $admin_bar );
$node = $admin_bar->get_node( 'delete-book' );
$this->assertTrue( is_object( $node ) );
$this->assertEquals( $node->id, 'delete-book' );
$this->assertEquals( $node->parent, 'site-name' );
}
}
5 changes: 4 additions & 1 deletion tests/test-shortcodes-footnotes-footnotes.php
Expand Up @@ -25,9 +25,12 @@ public function setUp() {

public function test_getInstance() {

$val = $this->fn->getInstance();
$val = $this->fn->init();

$this->assertTrue( $val instanceof \Pressbooks\Shortcodes\Footnotes\Footnotes );

global $shortcode_tags;
$this->assertArrayHasKey( 'footnote', $shortcode_tags );
}

public function test_shortcodeHandler_numbered() {
Expand Down

0 comments on commit 42c3eb2

Please sign in to comment.