Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Last active July 15, 2018 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellofromtonya/a1aeffe4cd7025693e24ac7200599756 to your computer and use it in GitHub Desktop.
Save hellofromtonya/a1aeffe4cd7025693e24ac7200599756 to your computer and use it in GitHub Desktop.
Remove Page Attributes from Specific Page.
<?php
class WP_Offline_Page {
/**
* Initializes the instance.
*/
public function init() {
add_action( 'current_screen', array( $this, 'remove_page_attributes' ) );
// other code removed.
}
/**
* Remove the page attributes for the Offline Page.
*
* @return bool Returns true on success.
*/
public function remove_page_attributes() {
// Bail out if this is not the right page.
$screen = get_current_screen();
if ( ! $screen instanceof WP_Screen || 'page' !== $screen->post_type ) {
return false;
}
// When on the edit page, enqueue the inline edit script.
if ( 'edit-page' === $screen->id ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_remove_page_attributes_script' ) );
return true;
}
// When on the Offline Page's page UI, remove the page attributes support.
if ( 'page' === $screen->id && isset( $_GET['post'] ) && (int) $_GET['post'] === $this->get_offline_page_id() ) { // WPCS: CSRF ok.
remove_post_type_support( 'page', 'page-attributes' );
return true;
}
return false;
}
/**
* Enqueue the script to remove the page attributes fields from the Quick Edit UI.
*/
public function enqueue_remove_page_attributes_script() {
if ( $this->get_offline_page_id() < 1 ) {
return;
}
wp_enqueue_script(
'pwa-offline-page-inline-edit',
plugins_url( '/offline-page/js/inline-edit-offline-page.js', __FILE__ ),
array( 'jquery' ),
PWA_VERSION,
true
);
wp_localize_script( 'pwa-offline-page-inline-edit', 'offlinePageID', array(
'id' => $this->offline_page_id,
) );
}
}
/* global offlinePageID */
/**
* Offline Page's inline edit handler to remove the page attribute fields from the quick edit UI.
*
* Note: This script uses MutationObserver, which is not supported by less modern browsers, such as
* less than IE11.
*/
( function( $, window, document ) {
'use strict';
/**
* Offline Page's Inline Edit Handler.
*
* @class
*/
class InlineEditOfflinePage {
/**
* Creates the handler.
*
* @constructor
* @param {Number} pageID
*/
constructor(pageID) {
this.pageID = pageID;
this.editID = 'edit-' + this.pageID;
this.listEl = document.getElementById('the-list');
this.observer = new MutationObserver(this.mutationHandler.bind(this));
}
/**
* Initializes the observer.
*
* @returns {boolean}
*/
init() {
this.observer.observe(this.listEl, {
childList: true,
subtree: true
});
}
/**
* Callback for the observer's mutations. Iterates through the list, searching
* for an added Offline Page node. When found, it calls to remove its page attributes
* fields from the Quick Edit UI.
*
* @param mutationsList
*/
mutationHandler(mutationsList) {
for (let mutation of mutationsList) {
if (this.isOfflinePageNode(mutation)) {
this.removePageAttributes();
break;
}
}
}
/**
* Checks if this mutation is an offline page node.
*
* @param {Object} mutation Mutation Record.
* @returns {boolean}
*/
isOfflinePageNode(mutation) {
if (mutation.addedNodes.length === 0) {
return false;
}
// Search to find the right node. If found, return true.
for (let nodeItem of mutation.addedNodes) {
return this.editID === nodeItem.id;
}
return false;
}
/**
* Removes the page attributes from the quick edits.
*/
removePageAttributes() {
$('#post_parent').parent('label').remove();
$('.inline-edit-menu-order-input').closest('label').remove();
}
}
$( document ).ready( function() {
// Bail out if something goes wrong.
if ( ! 'MutationObserver' in window || typeof offlinePageID === 'undefined' ) {
return;
}
// If this list does not include the offline page, there's nothing to do. Bail out.
if ( null === $( '#post-' + offlinePageID.id ) ) {
return;
}
const inlineEdit = new InlineEditOfflinePage(offlinePageID.id);
inlineEdit.init();
});
})( jQuery, window, document );
<?php
/**
* Tests for class WP_Offline_Page.
*/
class Test_WP_Offline_Page extends WP_UnitTestCase {
/**
* Tested instance.
*
* @var WP_Offline_Page
*/
public $instance;
/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
$this->instance = new WP_Offline_Page();
}
/**
* Test init.
*
* @covers WP_Offline_Page::init()
*/
public function test_init() {
$this->instance->init();
$this->assertEquals( 10, has_action( 'current_screen', array( $this->instance, 'remove_page_attributes' ) ) );
}
/**
* Test remove_page_attributes.
*
* @covers WP_Offline_Page::remove_page_attributes()
*/
public function test_remove_page_attributes() {
$this->assertArrayNotHasKey( 'post', $_GET ); // WPCS: CSRF ok.
$this->assertFalse( $this->instance->remove_page_attributes() );
set_current_screen( 'page' );
$page_id = $this->factory()->post->create( array( 'post_type' => 'page' ) );
$_GET['post'] = $page_id; // WPCS: CSRF ok.
// Check that false returns when the page is not the Offline Page.
$this->assertFalse( $this->instance->remove_page_attributes() );
add_option( WP_Offline_Page::OPTION_NAME, $page_id + 99 );
$this->instance->get_offline_page_id( true );
$this->assertFalse( $this->instance->remove_page_attributes() );
// Check that the page attributes were removed for the Offline Page.
update_option( WP_Offline_Page::OPTION_NAME, $page_id );
$this->instance->get_offline_page_id( true );
$this->assertArrayHasKey( 'page-attributes', get_all_post_type_supports( 'page' ) );
$this->assertTrue( $this->instance->remove_page_attributes() );
$this->assertArrayNotHasKey( 'page-attributes', get_all_post_type_supports( 'page' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment