Skip to content

Commit f1a0f75

Browse files
committed
Merge branch 'release/2.5.0'
2 parents d88a5f9 + eda9b7e commit f1a0f75

File tree

13 files changed

+397
-64
lines changed

13 files changed

+397
-64
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## Unreleased
44

5+
## v2.5.0 - 08/18/2024
6+
7+
* Improvement: Change rule processing to be more explicit for each context/rule type, improving performance & reliability of how rules are handled in some edge cases.
8+
* Improvement: Update `content_control_known_blockTypes` option to not be autoloaded.
9+
* Improvement: Update QueryMonitor output to differentiate between terms & posts.
10+
* Fix: Error due to invalid return value variable name.
11+
512
## v2.4.0 - 06/21/2024
613

714
* Improvement: Optimized the order we determine if we can skip checking restrictions for any givem content type. Thanks to PolyLang team for the suggestion.

classes/Controllers/Frontend/Restrictions/PostContent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ public function filter_the_excerpt_if_restricted( $post_excerpt, $post = null )
157157

158158
$restriction = get_applicable_restriction();
159159

160+
if ( false === $restriction ) {
161+
return $post_excerpt;
162+
}
163+
160164
/**
161165
* Filter the excerpt to display when a post is restricted.
162166
*

classes/Controllers/RestAPI.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
use ContentControl\Base\Controller;
1414

15+
use function ContentControl\check_referrer_is_admin;
16+
use function ContentControl\is_rest;
17+
1518
/**
1619
* RestAPI function initialization.
1720
*/
@@ -21,6 +24,10 @@ class RestAPI extends Controller {
2124
*/
2225
public function init() {
2326
add_action( 'rest_api_init', [ $this, 'register_routes' ] );
27+
28+
// Handle CPT & Taxonomy that are not registered with the `show_in_rest` arg when searching from our settings pages.
29+
add_filter( 'register_post_type_args', [ $this, 'modify_post_type_show_in_rest' ], 10, 2 );
30+
add_filter( 'register_taxonomy_args', [ $this, 'modify_taxonomy_show_in_rest' ], 10, 2 );
2431
}
2532

2633
/**
@@ -34,4 +41,107 @@ public function register_routes() {
3441
( new \ContentControl\RestAPI\ObjectSearch() )->register_routes();
3542
( new \ContentControl\RestAPI\Settings() )->register_routes();
3643
}
44+
45+
/**
46+
* Modify show_in_rest for post types.
47+
*
48+
* @param array<string,mixed> $args Post type args.
49+
*
50+
* @return array<string,mixed>
51+
*/
52+
public function modify_post_type_show_in_rest( $args ) {
53+
$include_private = $this->container->get_option( 'includePrivatePostTypes', true );
54+
return $this->modify_type_force_show_in_rest( $args, $include_private );
55+
}
56+
57+
/**
58+
* Modify show_in_rest for taxonomies.
59+
*
60+
* @param array<string,mixed> $args Taxonomy args.
61+
*
62+
* @return array<string,mixed>
63+
*/
64+
public function modify_taxonomy_show_in_rest( $args ) {
65+
$include_private = $this->container->get_option( 'includePrivateTaxonomies', true );
66+
return $this->modify_type_force_show_in_rest( $args, $include_private );
67+
}
68+
69+
/**
70+
* Modify show_in_rest for post types.
71+
*
72+
* @param array<string,mixed> $args Post type args.
73+
* @param boolean $include_private Whether to include private post types.
74+
*
75+
* @return array<string,mixed>
76+
*/
77+
public function modify_type_force_show_in_rest( $args, $include_private = false ) {
78+
if ( ! $this->force_show_in_rest() ) {
79+
return $args;
80+
}
81+
82+
// If show_in_rest is already false, return early.
83+
if ( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) {
84+
return $args;
85+
}
86+
87+
// Check if this is a private taxonomy.
88+
if ( true !== $args['public'] ) {
89+
if ( $include_private ) {
90+
$args['show_in_rest'] = true; // Enable REST API.
91+
}
92+
} else {
93+
$args['show_in_rest'] = true; // Enable REST API.
94+
}
95+
96+
return $args;
97+
}
98+
99+
/**
100+
* Force show_in_rest to true.
101+
*
102+
* @return boolean
103+
*/
104+
public function force_show_in_rest() {
105+
static $force_show_in_rest;
106+
107+
if ( isset( $force_show_in_rest ) ) {
108+
return $force_show_in_rest;
109+
}
110+
111+
$force_show_in_rest = false;
112+
113+
if ( ! is_rest() ) {
114+
return false;
115+
}
116+
117+
// Return false if referrer is not our settings page. /wp-admin/options-general.php?page=content-control-settings.
118+
if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) {
119+
return false;
120+
}
121+
122+
if ( ! check_referrer_is_admin() ) {
123+
return false;
124+
}
125+
126+
$referrer = sanitize_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
127+
128+
// Check if referrer is our settings page and contains the page=content-control-settings query param.
129+
if ( strpos( $referrer, 'options-general.php' ) === false ) {
130+
return false;
131+
}
132+
133+
// Check if referrer is our settings page and contains the page=content-control-settings query param.
134+
if ( strpos( $referrer, 'page=content-control-settings' ) === false ) {
135+
return false;
136+
}
137+
138+
// Check if current user has permission to make plugin settings changes.
139+
if ( ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) {
140+
return false;
141+
}
142+
143+
$force_show_in_rest = true;
144+
145+
return true;
146+
}
37147
}

classes/Plugin/Core.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ protected function check_version() {
9292
*
9393
* Note: Old version is still available in options.
9494
*
95-
* @param string $version The new version.
95+
* @param string $old_version The old version.
96+
* @param string $new_version The new version.
9697
*/
97-
do_action( "{$this->get( 'option_prefix' )}/update_version", $data['version'] );
98+
do_action( "{$this->get( 'option_prefix' )}/update_version", $data['version'], $version );
9899

99100
// Save Upgraded From option.
100101
$data['upgraded_from'] = $data['version'];

classes/QueryMonitor/Output.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,26 @@ public function output_post_restrictions() {
209209
if ( 'main' === $post_id ) {
210210
continue;
211211
}
212-
$post = get_post( $post_id );
212+
213+
if ( strpos( $post_data['context'], 'terms' ) !== false ) {
214+
$term = get_term( $post_id );
215+
$name = $term->name;
216+
$type = 'TAX: ' . $term->taxonomy;
217+
} elseif ( strpos( $post_data['context'], 'posts' ) !== false ) {
218+
$post = get_post( $post_id );
219+
$name = $post->post_title;
220+
$type = 'PT: ' . $post->post_type;
221+
} else {
222+
continue;
223+
}
213224

214225
$user_can_view = $post_data['user_can_view'] ? 'Yes' : 'No';
215226
$restrictions = $post_data['restrictions'] ? $post_data['restrictions'] : [];
216227

217228
echo '<tr>';
218229
echo '<td>' . esc_html( $post_id ) . '</td>';
219-
echo '<td>' . esc_html( $post->post_title ) . '</td>';
220-
echo '<td>' . esc_html( $post->post_type ) . '</td>';
230+
echo '<td>' . esc_html( $name ) . '</td>';
231+
echo '<td>' . esc_html( $type ) . '</td>';
221232
echo '<td>' . esc_html( $post_data['context'] ) . '</td>';
222233
echo '<td>' . esc_html( $user_can_view ) . '</td>';
223234

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "code-atlantic/content-control",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"type": "wordpress-plugin",
55
"license": "GPL-2.0-or-later",
66
"minimum-stability": "dev",

content-control.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Content Control
44
* Plugin URI: https://contentcontrolplugin.com/?utm_campaign=plugin-info&utm_source=php-file-header&utm_medium=plugin-ui&utm_content=plugin-uri
55
* Description: Restrict content to logged in/out users or specific user roles. Restrict access to certain parts of a page/post. Control the visibility of widgets.
6-
* Version: 2.4.0
6+
* Version: 2.5.0
77
* Author: Code Atlantic
88
* Author URI: https://code-atlantic.com/?utm_campaign=plugin-info&utm_source=php-file-header&utm_medium=plugin-ui&utm_content=author-uri
99
* Donate link: https://code-atlantic.com/donate/?utm_campaign=donations&utm_source=php-file-header&utm_medium=plugin-ui&utm_content=donate-link
@@ -30,7 +30,7 @@ function get_plugin_config() {
3030
return [
3131
'name' => \__( 'Content Control', 'content-control' ),
3232
'slug' => 'content-control',
33-
'version' => '2.4.0',
33+
'version' => '2.5.0',
3434
'option_prefix' => 'content_control',
3535
// Maybe remove this and simply prefix `name` with `'Popup Maker'`.
3636
'text_domain' => 'content-control',

inc/functions/options.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ function get_block_types() {
7777
return \get_option( 'content_control_known_blockTypes', [] );
7878
}
7979

80+
/**
81+
* Delete block types cache.
82+
*
83+
* @return void
84+
*/
85+
function delete_block_types_cache() {
86+
\delete_option( 'content_control_known_blockTypes' );
87+
}
88+
89+
/**
90+
* Purge block types cache on update.
91+
*
92+
* @param string $old_version The old version.
93+
* @param string $new_version The new version.
94+
*
95+
* @return void
96+
*/
97+
function purge_block_types_cache_on_update( $old_version, $new_version ) {
98+
if ( version_compare( '2.5.0', $new_version, '>=' ) && version_compare( $old_version, '2.5.0', '<' ) ) {
99+
delete_block_types_cache();
100+
}
101+
}
102+
103+
add_action( 'content_control/update_version', __NAMESPACE__ . '\\purge_block_types_cache_on_update', 10, 2 );
104+
80105
/**
81106
* Sanitize expetced block type data.
82107
*
@@ -117,7 +142,7 @@ function update_block_types( $incoming_block_types = [] ) {
117142
// Flatten values to a simple array for storage.
118143
$block_types = array_values( $block_types );
119144

120-
\update_option( 'content_control_known_blockTypes', $block_types );
145+
\update_option( 'content_control_known_blockTypes', $block_types, false );
121146
}
122147

123148
/**

0 commit comments

Comments
 (0)