Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpstan level 7 changes #242

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 6
level: 7
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
stubFiles:
Expand Down
49 changes: 34 additions & 15 deletions src/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function validate_before_save_cb( $data, $post ) {
$existing_post = get_post( $post['ID'] );

// Overwrite new/invalid query with previous working query, or empty
$data['post_content'] = $existing_post->post_content;
if ( $existing_post ) {
$data['post_content'] = $existing_post->post_content;
}

AdminErrors::add_message( $e->getMessage() );
}
Expand Down Expand Up @@ -170,18 +172,21 @@ public static function grant_input_box_cb( $post ) {
checked( $value, Grant::USE_DEFAULT, false )
);
$html .= '<label for="graphql_query_grant_default">Use global default</label><br >';

/** @var array[] */
$allowed_html = [
'input' => [
'type' => true,
'id' => true,
'name' => true,
'value' => true,
'checked' => true,
],
'br' => true,
];
echo wp_kses(
$html,
[
'input' => [
'type' => true,
'id' => true,
'name' => true,
'value' => true,
'checked' => true,
],
'br' => true,
]
$allowed_html
);
}

Expand All @@ -196,9 +201,21 @@ public static function maxage_input_box_cb( $post ) {

$max_age = new MaxAge();
$value = $max_age->get( $post->ID );
$value = absint( $value ) ? $value : 0;
$html = sprintf( '<input type="text" id="graphql_query_maxage" name="graphql_query_maxage" value="%s" />', $value );
$html .= '<br><label for="graphql_query_maxage">Max-Age HTTP header. Integer value.</label>';

if ( is_wp_error( $value ) ) {
AdminErrors::add_message(
sprintf(
__( 'Invalid max age %s.', 'wp-graphql-smart-cache' ),
$value->get_error_message()
)
);
$value = 0;
} else {
$value = absint( $value ) ? $value : 0;
}

$html = sprintf( '<input type="text" id="graphql_query_maxage" name="graphql_query_maxage" value="%s" />', $value );
$html .= '<br><label for="graphql_query_maxage">Max-Age HTTP header. Integer value.</label>';
echo wp_kses(
$html,
[
Expand Down Expand Up @@ -271,7 +288,9 @@ public function make_excerpt_column_sortable_in_admin_cb( $columns ) {
* @return array
*/
public function wp_editor_settings( $settings, $editor_id ) {
if ( 'content' === $editor_id && Document::TYPE_NAME === get_current_screen()->post_type ) {
$screen = get_current_screen();

if ( $screen && 'content' === $editor_id && Document::TYPE_NAME === $screen->post_type ) {
$settings['tinymce'] = false;
$settings['quicktags'] = false;
$settings['media_buttons'] = false;
Expand Down
18 changes: 11 additions & 7 deletions src/AdminErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ public function display_validation_messages() {

foreach ( $error_messages as $message ) {
$html = sprintf( '<div id="plugin-message" class="error below-h2"><p>%s</p></div>', $message );

/** @var array[] */
$allowed_html = [
'div' => [
'id' => true,
'class' => true,
],
'p' => true,
];

echo wp_kses(
$html,
[
'div' => [
'id' => true,
'class' => true,
],
'p' => true,
]
$allowed_html
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Cache/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function save_query_mapping_cb(

$request_key = $this->build_key( $query_id, $query, $variables, $operation );

if ( false === $request_key ) {
return;
}

// get the runtime nodes from the query analyzer
$runtime_nodes = $request->get_query_analyzer()->get_runtime_nodes() ?: [];
$list_types = $request->get_query_analyzer()->get_list_types() ?: [];
Expand Down
29 changes: 23 additions & 6 deletions src/Cache/Invalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function init() {
/**
* Return a list of ignored meta keys
*
* @return array|null
* @return array
*/
public static function get_ignored_meta_keys() {
if ( null !== self::$ignored_meta_keys ) {
Expand Down Expand Up @@ -449,6 +449,10 @@ public function on_saved_term_cb( $term_id, $tt_id, $taxonomy, $update, $args =

$tax_object = get_taxonomy( $taxonomy );

if ( false === $tax_object ) {
return;
}

// Delete the cached results associated with this post/key
$this->purge_nodes( 'term', $term->term_id, 'term_saved' );

Expand Down Expand Up @@ -479,6 +483,10 @@ public function on_deleted_term_relationship_cb( $object_id, $tt_id, $taxonomy )

$tax_object = get_taxonomy( $taxonomy );

if ( false === $tax_object ) {
return;
}

// Delete the cached results associated with this post/key
$this->purge_nodes( 'term', $term->term_id, 'term_relationship_deleted' );
$type_name = strtolower( $tax_object->graphql_single_name );
Expand Down Expand Up @@ -534,6 +542,10 @@ public function on_transition_post_status_cb( $new_status, $old_status, WP_Post

$post_type_object = get_post_type_object( $post->post_type );

if ( ! $post_type_object instanceof \WP_Post_Type ) {
return;
}

// If the post type is not public and not publicly queryable
// don't track it
if ( false === $post_type_object->public && false === $post_type_object->publicly_queryable ) {
Expand Down Expand Up @@ -570,8 +582,7 @@ public function on_transition_post_status_cb( $new_status, $old_status, WP_Post
$action_type = 'CREATE';
}

$post_type_object = get_post_type_object( $post->post_type );
$type_name = $post_type_object instanceof \WP_Post_Type ? strtolower( $post_type_object->graphql_single_name ) : $post_type_object;
$type_name = strtolower( $post_type_object->graphql_single_name );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with changes like this we really need to be careful that we're only executing code when WPGraphQL is active.

If WPGraphQL Smart Cache were active but WPGraphQL was not active, and the invalidation callbacks were fired, I think this would be some fatals.

So, we should check to ensure we're only executing code if WPGraphQL is already active. 👀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// if we create a post
// we need to purge lists of the type
Expand All @@ -584,7 +595,7 @@ public function on_transition_post_status_cb( $new_status, $old_status, WP_Post
// Purge the terms associated with the node
$terms = wp_get_object_terms( $post->ID, \WPGraphQL::get_allowed_taxonomies() );

if ( ! empty( $terms ) ) {
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
array_map(
function ( $term ) use ( $post ) {
if ( ! $term instanceof WP_Term ) {
Expand All @@ -608,7 +619,7 @@ function ( $term ) use ( $post ) {
if ( 'DELETE' === $action_type ) {
$terms = wp_get_object_terms( $post->ID, \WPGraphQL::get_allowed_taxonomies() );

if ( ! empty( $terms ) ) {
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
array_map(
function ( $term ) use ( $post ) {
if ( ! $term instanceof WP_Term ) {
Expand Down Expand Up @@ -723,6 +734,10 @@ public function on_postmeta_change_cb( $meta_id, $post_id, $meta_key, $meta_valu

$post_type_object = get_post_type_object( $post->post_type );

if ( ! $post_type_object instanceof \WP_Post_Type ) {
return;
}

// If the post type is not public and not publicly queryable
// don't track it
if ( false === $post_type_object->public && false === $post_type_object->publicly_queryable ) {
Expand Down Expand Up @@ -813,7 +828,9 @@ public function on_update_nav_menu_cb( $menu_id ) {
$menu = get_term_by( 'id', absint( $menu_id ), 'nav_menu' );

// menus have a term:id relay global ID, as they use the term loader
$this->purge_nodes( 'term', $menu->term_id, 'updated_nav_menu' );
if ( $menu instanceof WP_Term ) {
$this->purge_nodes( 'term', $menu->term_id, 'updated_nav_menu' );
}
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Cache/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WPGraphQL\SmartCache\Document;
use WPGraphQL\SmartCache\Storage\Transient;
use WPGraphQL\SmartCache\Storage\WpCache;
use WPGraphQL\SmartCache\Storage\Ephemeral;

class Query {

Expand All @@ -17,7 +18,7 @@ class Query {
/**
* The storage object for the actual system of choice transient, database, object, memory, etc
*
* @var object
* @var WpCache|Transient|Ephemeral
**/
public static $storage = null;

Expand Down Expand Up @@ -69,7 +70,13 @@ public function build_key( $query_id, $query, $variables = null, $operation = nu
'user' => $user->ID,
];

return hash( 'sha256', wp_json_encode( $parts ) );
$parts_string = wp_json_encode( $parts );

if ( false === $parts_string ) {
return false;
}

return hash( 'sha256', $parts_string );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function add_cache_key_to_response_extensions(

if ( is_array( $response ) ) {
$response['extensions']['graphqlSmartCache']['graphqlObjectCache'] = $message;
} if ( is_object( $response ) ) {
} if ( is_object( $response ) && property_exists( $response, 'extensions' ) ) {
$response->extensions['graphqlSmartCache']['graphqlObjectCache'] = $message;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function graphql_mutation_insert( $post_object, $filtered_input, $input,

// Remove the existing/old alias terms before update
$terms = wp_get_post_terms( $post_object['postObjectId'], self::ALIAS_TAXONOMY_NAME );
if ( $terms ) {
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
wp_remove_object_terms( $post_object['postObjectId'], $term->term_id, self::ALIAS_TAXONOMY_NAME );
wp_delete_term( $term->term_id, self::ALIAS_TAXONOMY_NAME );
Expand Down Expand Up @@ -353,7 +353,7 @@ public function after_updated_cb( $post_ID, $post_after, $post_before ) {

// If the old query string hash is assigned to this post, delete it
$terms = wp_get_post_terms( $post_ID, self::ALIAS_TAXONOMY_NAME );
if ( $terms ) {
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
if ( $old_query_id === $term->name ) {
wp_remove_object_terms( $post_ID, $term->term_id, self::ALIAS_TAXONOMY_NAME );
Expand Down Expand Up @@ -469,7 +469,7 @@ public function delete_post_cb( $post_id ) {
*/
public function delete_term( $post_id ) {
$terms = wp_get_object_terms( $post_id, self::ALIAS_TAXONOMY_NAME );
if ( $terms ) {
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, self::ALIAS_TAXONOMY_NAME );
}
Expand Down