diff --git a/wp-content/mu-plugins/markdown-sync.php b/wp-content/mu-plugins/markdown-sync.php index 46644d6..ee67de8 100644 --- a/wp-content/mu-plugins/markdown-sync.php +++ b/wp-content/mu-plugins/markdown-sync.php @@ -1,10 +1,12 @@ 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( + '

%s

%s

', + '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( + '#