Skip to content

Commit

Permalink
Version 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
MillerMedia committed Mar 26, 2018
1 parent 8307183 commit e20e2ae
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 70 deletions.
94 changes: 94 additions & 0 deletions _inc/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* @todo Convert this code into a class
*/

/**
* Enqueue admin scripts and stylesheets
*
* @return void
*/

add_action('admin_enqueue_scripts', function () {

wp_enqueue_style('ptc-admin–css', PTC_PLUGIN_URL . 'assets/css/admin.css');
wp_enqueue_script('ptc-admin-js', PTC_PLUGIN_URL . 'assets/js/admin.js');

});

/**
* Handle new admin option to password protect child posts
*
* @return void
*/

add_action('save_post', function ($post_id) {

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;

if (!current_user_can('edit_post', $post_id))
return;

$protect_children = isset($_POST['protect-children']) && $_POST['protect-children'] == "on" ? "on" : "off";

update_post_meta($post_id, '_protect_children', $protect_children);

});

/**
* Add the option to protect child posts
*
* @return void
*/

add_action('post_submitbox_misc_actions', function ($post) {

$post_type = $post->post_type;

if (isPasswordProtected($post)) {
$checked = get_post_meta($post->ID, '_protect_children', true) == "on" ? "checked" : "";
echo "<div id=\"protect-children-div\"><input type=\"checkbox\" " . $checked . " name=\"protect-children\" /><strong>Password Protect</strong> all child posts</div>";
}

});

/**
* On admin page load of a child post, change the 'Visibility' for children post if
* they are protected. There is no hook for that part of the admin section we have
* to edit the outputted HTML.
*
* @param string $buffer The outputted HTML of the edit post page
* @return string $buffer Original or modified HTML
*/

add_action('admin_init', function () {
if (!defined('DOING_AJAX') || !DOING_AJAX)
global $pagenow;

ob_start(function ($buffer) use ($pagenow) {

if ('post.php' === $pagenow && isset($_GET['post'])) {
$post = get_post($_GET['post']);

// Check if it is a child post and if the parent post has a password set
if ($parent_id = wp_get_post_parent_id($post) and protectTheChildrenEnabled($parent_id)) {

// Change the wording to 'Password Protected' if the post is protected
$buffer = preg_replace('/(<span id="post-visibility-display">)(.*)(<\/span>)/i', '$1Password protected$3', $buffer);

// Remove Edit button post visibility (post needs to be updated from parent post)
$buffer = preg_replace('/<a href="#visibility".*?><\/a>/i', '', $buffer);

// Add 'Password protect by parent post' notice under visibility section
$regex_pattern = '/(<\/div>)(\n*|.*)(<\!-- \.misc-pub-section -->)(\n*|.*)(<div class="misc-pub-section curtime misc-pub-curtime">)/i';
$admin_edit_link = sprintf(admin_url('post.php?post=%d&action=edit'), $post_parent);
$update_pattern = sprintf('<br><span class="wp-media-buttons-icon password-protect-admin-notice">Password protected by <a href="%s">parent post</a></span>$1$2$3$4$5', $admin_edit_link);
$buffer = preg_replace($regex_pattern, $update_pattern, $buffer);
}
}

return $buffer;
});
});
30 changes: 29 additions & 1 deletion _inc/helpers.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
<?php

/**
* Check if post is password protected
* @todo Convert this code into a class
*/

/**
* Checks if post is password protected
*
* @param int|object $post
* @return boolean
*/

function isPasswordProtected($post)
{
if (is_int($post) or !is_object($post))
$post = get_post($post);

return 'private' != $post->post_status && !empty($post->post_password);

}

/**
* Check if password protected is on and if the Protect the Children option is enabled.
*
* @param $post Post ID or post object
* @return bool|WP_Post
*/

function protectTheChildrenEnabled($post)
{
if (is_int($post) or !is_object($post))
$post = get_post($post);

if (!isPasswordProtected($post))
return false;

if ( get_post_meta($post->ID, '_protect_children', true) == "on" )
return $post;

return false;
}
14 changes: 14 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ div#protect-children-div {
vertical-align: top;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

.password-protect-admin-notice{
font-size:12px;
padding-left:20px;
}

.password-protect-admin-notice:before{
font: 400 14px/1 dashicons;
padding-right:5px;
}

.password-protect-admin-notice:before{
content: "\f348";
}
78 changes: 13 additions & 65 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,23 @@
* Author URI: www.millermedia.io
*/

if ( version_compare( PHP_VERSION, '5.6', '<' ) ) {
add_action( 'admin_notices', function(){
echo "<div class=\"error\"><p>".__('Protect the Children requires PHP 5.6 and greater to function properly. Please upgrade PHP or deactivate Protect the Children.', 'protect-the-children') ."</p></div>";
} );
if (version_compare(PHP_VERSION, '5.6', '<')) {
add_action('admin_notices', function () {
echo "<div class=\"error\"><p>" . __('Protect the Children requires PHP 5.6 and greater to function properly. Please upgrade PHP or deactivate Protect the Children.', 'protect-the-children') . "</p></div>";
});
return;
}

define('PTC_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('PTC_PLUGIN_URL', plugin_dir_url(__FILE__));

require_once(PTC_PLUGIN_PATH . '_inc/helpers.php');
require_once(PTC_PLUGIN_PATH . '_inc/admin.php');

/**
* Enqueue admin scripts and stylesheets
*
* @return void
*/

add_action('admin_enqueue_scripts', function () {

wp_enqueue_style('ptc-admin–css', PTC_PLUGIN_URL . 'assets/css/admin.css');
wp_enqueue_script('ptc-admin-js', PTC_PLUGIN_URL . 'assets/js/admin.js');

});

/**
* Handle new admin option to password protect child posts
* On front-end page load, check the post's parent ID
*
* @return void
*/

add_action('save_post', function ($post_id) {

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;

if (!current_user_can('edit_post', $post_id))
return;

$protect_children = isset($_POST['protect-children']) && $_POST['protect-children'] == "on" ? "on" : "off";

update_post_meta($post_id, '_protect_children', $protect_children);

});

/**
* Add the option to protect child posts
*
* @return void
*/

add_action('post_submitbox_misc_actions', function ($post) {

$post_type = $post->post_type;

if (isPasswordProtected($post)) {
$checked = get_post_meta($post->ID, '_protect_children', true) == "on" ? "checked" : "";
echo "<div id=\"protect-children-div\"><input type=\"checkbox\" " . $checked . " name=\"protect-children\" /><strong>Password Protect</strong> all child posts</div>";
}

});

/**
* On page load, check the post's parent ID
*
* @return int
* @return bool
*/

add_action('template_redirect', function () {
Expand All @@ -82,24 +33,21 @@
if (!$parent_id)
return;

// See if the parent post is password protected
if (!isPasswordProtected($parent_post = get_post($parent_id)))
return;
$parent_post = protectTheChildrenEnabled($parent_id);

// See if the parent post has the protect child option enabled
if (empty(get_post_meta($parent_id, '_protect_children', true)) || get_post_meta($parent_id, '_protect_children', true) == "off")
if (!$parent_post)
return;

$parent_password = $parent_post->post_password;

// Check the cookie (hashed password)
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash( 8, true );
$hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
$required = ! $hasher->CheckPassword( $parent_password, $hash );
$hasher = new PasswordHash(8, true);
$hash = wp_unslash($_COOKIE['wp-postpass_' . COOKIEHASH]);
$required = !$hasher->CheckPassword($parent_password, $hash);

// If password has already been entered on the parent post, continue to page
if( !$required )
if (!$required)
return;

add_filter('post_password_required', function () {
Expand Down
15 changes: 11 additions & 4 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: millermedianow
Tags: password protect, password, protected, protect, password, child, parent, edit, visibility
Tested up to: 4.9.4
Stable tag: 1.0.1
Stable tag: 1.2
Requires PHP: 5.6
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -30,10 +30,17 @@ PROTECT THE CHILDREN!

== Changelog ==

= 1.0 =
* The initial public release!
= 1.2 = (3/25/18)
* Child post displays password protected status in admin when its parent is protecting children.
* Misc code cleanup

= 1.1 =
* All posts (parent and children) are now unlocked by entering the password on one of those protected pages.

= 1.0.1 =
* Tested and confirmed compatibility for PHP 7.2
* Tested and confirmed compatbility with WordPress 4.9.4
* Removed support for PHP versions lower than 5.6
* Removed support for PHP versions lower than 5.6

= 1.0 =
* The initial public release!

0 comments on commit e20e2ae

Please sign in to comment.