Skip to content

Commit

Permalink
Disable more editing features
Browse files Browse the repository at this point in the history
Since we're managing the content, title, and slug elsewhere, we should
disable editing these fields in the dashboard.
  • Loading branch information
nylen committed Feb 25, 2020
1 parent ca4109b commit 5c8810d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
4 changes: 3 additions & 1 deletion wp-content/mu-plugins/markdown-sync.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

require_once __DIR__ . '/markdown-sync/plugin.php';
require_once __DIR__ . '/markdown-sync/class-cpdocs-editor.php';
require_once __DIR__ . '/markdown-sync/class-cpdocs-importer.php';

$cpnet_docs_importer = new CPDocs_Importer();
$cpnet_docs_editor = new WordPressdotorg\Markdown\Editor( $cpnet_docs_importer );
$cpnet_docs_editor = new CPDocs_Editor( $cpnet_docs_importer );
add_action( 'init', [ $cpnet_docs_editor, 'init' ] );

if ( class_exists( 'WP_CLI' ) ) {
WP_CLI::add_command(
Expand Down
137 changes: 137 additions & 0 deletions wp-content/mu-plugins/markdown-sync/class-cpdocs-editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

use WordPressdotorg\Markdown\Editor;

class CPDocs_Editor extends Editor {
/**
* Override for main initialization function - we will modify some of the
* parent class' behavior.
*/
public function init() {
parent::init();
// Disable some features of the post editor.
add_filter( 'replace_editor', [ $this, 'maybe_disable_edits' ], 10, 2 );
}

/**
* New function to determine whether this particular post is handled by the
* markdown sync system.
*/
public function is_markdown_post( WP_Post $post ) {
if ( $post->post_type !== $this->importer->get_post_type() ) {
return false;
}

$source = $this->importer->get_markdown_source( $post->ID );
if ( is_wp_error( $source ) || empty( $source ) ) {
return false;
}

return true;
}

/**
* Override for rendering an editor warning: we shouldn't do this for all
* pages, only the ones where we actually have Markdown content.
*
* We also modify the text of this warning slightly.
*
* @param WP_Post $post Post being edited.
*/
public function render_editor_warning( WP_Post $post ) {
if ( ! $this->is_markdown_post( $post ) ) {
return;
}

printf(
'<div class="notice notice-warning"><p>%s</p><p><a href="%s">%s</a></p></div>',
'This page is maintained on GitHub, please make any changes to the content, title, and slug there instead.',
$this->get_markdown_edit_link( $post->ID ),
'Edit on GitHub'
);
}

/**
* New function to disable editing content, title, and slug for posts that
* are edited on GitHub.
*/
public function maybe_disable_edits( $replace, $post ) {
if ( $this->is_markdown_post( $post ) ) {
// Don't return true because that will replace the entire editor
// interface. Instead use this hack to disable the content box
// just in time.
remove_post_type_support( $post->post_type, 'editor' );
// Disable editing the title and the slug. This just disables the
// UI controls (doesn't actually disable the related POST request)
// but that is good enough for our use-case.
add_action(
'edit_form_top',
[ $this, 'disable_edits_step1' ]
);
}

return $replace;
}

/**
* New function to disable editing the title and the permalink (slug).
*
* Step 1: Setup - enable these hacks just in time.
*/
public function disable_edits_step1( $post ) {
ob_start(); // Catch the HTML for the title edit box.
add_filter(
'page_link',
[ $this, 'disable_edits_step2' ],
10,
3
);
add_action(
'edit_form_after_title',
[ $this, 'disable_edits_step3' ]
);
}

/**
* New function to disable editing the title and the permalink (slug).
*
* Step 2: Disable editing the slug by changing the sample permalink HTML.
*
* See: https://github.com/ClassicPress/ClassicPress/blob/1.1.2+dev/src/wp-admin/includes/post.php#L1355-L1385
*/
public function disable_edits_step2( $link, $post_id, $sample ) {
if ( ! $sample ) {
return $link;
}

return str_replace(
'%pagename%',
get_post( $post_id )->post_name,
$link
);
}

/**
* New function to disable editing the title and the permalink (slug).
*
* Step 3: Make the title edit box read-only, and unhook temporary filters.
*
* See: https://github.com/ClassicPress/ClassicPress/blob/1.1.2+dev/src/wp-admin/edit-form-advanced.php#L574
*/
public function disable_edits_step3() {
// Get the currently buffered HTML including the title edit box.
$html = ob_get_clean();
// Add a readonly attribute. input[readonly] has background #eee but
// this is overridden by the CSS rules for the title edit box.
echo preg_replace(
'#<input type="text" name="post_title"#',
'<input type="text" name="post_title" readonly style="background:#eee"',
$html
);
// Remove the temporary filter we used to disable editing the slug.
remove_filter(
'page_link',
[ $this, 'disable_edits_step2' ]
);
}
}

0 comments on commit 5c8810d

Please sign in to comment.