diff --git a/_inc/admin.php b/_inc/admin.php new file mode 100644 index 0000000..113811f --- /dev/null +++ b/_inc/admin.php @@ -0,0 +1,94 @@ +post_type; + + if (isPasswordProtected($post)) { + $checked = get_post_meta($post->ID, '_protect_children', true) == "on" ? "checked" : ""; + echo "
Password Protect all child posts
"; + } + +}); + +/** + * 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>)/i', '$1Password protected$3', $buffer); + + // Remove Edit button post visibility (post needs to be updated from parent post) + $buffer = preg_replace('/<\/a>/i', '', $buffer); + + // Add 'Password protect by parent post' notice under visibility section + $regex_pattern = '/(<\/div>)(\n*|.*)(<\!-- \.misc-pub-section -->)(\n*|.*)(
)/i'; + $admin_edit_link = sprintf(admin_url('post.php?post=%d&action=edit'), $post_parent); + $update_pattern = sprintf('
Password protected by
parent post$1$2$3$4$5', $admin_edit_link); + $buffer = preg_replace($regex_pattern, $update_pattern, $buffer); + } + } + + return $buffer; + }); +}); \ No newline at end of file diff --git a/_inc/helpers.php b/_inc/helpers.php index 3e51cb8..2a4d322 100644 --- a/_inc/helpers.php +++ b/_inc/helpers.php @@ -1,14 +1,42 @@ 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; } \ No newline at end of file diff --git a/assets/css/admin.css b/assets/css/admin.css index 6a8ea14..e78be25 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -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"; } \ No newline at end of file diff --git a/index.php b/index.php index 6fba36b..08c9b7d 100644 --- a/index.php +++ b/index.php @@ -7,10 +7,10 @@ * Author URI: www.millermedia.io */ -if ( version_compare( PHP_VERSION, '5.6', '<' ) ) { - add_action( 'admin_notices', function(){ - echo "

".__('Protect the Children requires PHP 5.6 and greater to function properly. Please upgrade PHP or deactivate Protect the Children.', 'protect-the-children') ."

"; - } ); +if (version_compare(PHP_VERSION, '5.6', '<')) { + add_action('admin_notices', function () { + echo "

" . __('Protect the Children requires PHP 5.6 and greater to function properly. Please upgrade PHP or deactivate Protect the Children.', 'protect-the-children') . "

"; + }); return; } @@ -18,61 +18,12 @@ 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 "
Password Protect all child posts
"; - } - -}); - -/** - * On page load, check the post's parent ID - * - * @return int + * @return bool */ add_action('template_redirect', function () { @@ -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 () { diff --git a/readme.txt b/readme.txt index 881ec82..68aace5 100644 --- a/readme.txt +++ b/readme.txt @@ -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 @@ -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 \ No newline at end of file +* Removed support for PHP versions lower than 5.6 + += 1.0 = +* The initial public release! \ No newline at end of file