Skip to content

Commit

Permalink
Include jQuery to any module that needs it, further booletproof the R…
Browse files Browse the repository at this point in the history
…EST API endpoint args with validation and sanitization callbacks and fix an issue with the WP CLI command generating a PHP fatal error.
  • Loading branch information
kmgalanakis committed Jun 16, 2023
1 parent 1820101 commit 7226db7
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
env: { jquery: true },
extends: ['@10up/eslint-config/wordpress'],
settings: {
'import/core-modules': [ 'jquery' ]
},
rules: {
'react/no-array-index-key': 'off',
'jsdoc/newline-after-description': 'off',
Expand Down
110 changes: 66 additions & 44 deletions dist/js/gutenberg.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions includes/classes/class-fixinsecurecontent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FixInsecureContent {
*
* @var array
*/
private array $fixed_post_count;
private array $fixed_post_count = [];

/**
* Array of warning messages.
Expand Down Expand Up @@ -155,9 +155,14 @@ public function fix( $include, $all, $post_type, $posts_per_page, $post_offset,
// translators: Message to show when the fixing of insecure content is completed.
$message = PHP_EOL . sprintf( __( 'Total posts checked for insecure URL(s): %s', 'insecure-content-warning' ), $this->total_post_count ) . PHP_EOL;
WP_CLI::log( WP_CLI::colorize( "%c{$message}%n " ) );
Utils\format_items( 'table', $this->fixed_post_count, array( 'URL(s) fixed summary' ) );

if ( empty( $this->fixed_post_count ) ) {
WP_CLI::log( WP_CLI::colorize( '%c' . __( 'No post(s) found', 'insecure-content-warning' ) . '%n' ) );
} else {
Utils\format_items( 'table', $this->fixed_post_count, array( 'URL(s) fixed summary' ) );
}
} else {
if ( null === $this->fixed_post_count ) {
if ( empty( $this->fixed_post_count ) ) {
return __( 'No post(s) found', 'insecure-content-warning' );
}

Expand Down Expand Up @@ -278,7 +283,7 @@ protected function does_secure_content_exist( $url ) {
// Check if a https version of the URL exists.
$secure_version_exists = false;
$ssl = preg_replace( '/^http:/i', 'https:', $url );
$response = wp_remote_get( $ssl );
$response = wp_remote_head( $ssl );
$response_code = wp_remote_retrieve_response_code( $response );

if ( 200 === $response_code ) {
Expand Down
112 changes: 84 additions & 28 deletions includes/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,67 @@ function rest_routes() {
register_rest_route(
'icw/v1',
'/count-for-fix/',
[
array(
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\\count_for_fix_endpoint',
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => [
'postIds' => [
'type' => [ 'bool', 'string' ],
'args' => array(
'postIds' => array(
'type' => array( 'bool', 'string' ),
'sanitize_callback' => function ( $value ) {
if ( empty( $value ) ) {
return false;
}

return is_bool( $value ) ? rest_sanitize_boolean( $value ) : sanitize_text_field( $value );
},
],
'batchSize' => [
),
'batchSize' => array(
'type' => 'int',
'default' => 10,
'validate_callback' => function( $param ) {
return is_int( $param );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? absint( $value ) : 10;
},
],
'postSelection' => [
),
'postSelection' => array(
'type' => 'string',
'default' => 'all',
'validate_callback' => function( $param ) {
$allowed_post_selection_options = array(
'all',
'posts',
'all_from_post_type',
);

return in_array( $param, $allowed_post_selection_options, true );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? sanitize_text_field( $value ) : 'all';
},
],
'postType' => [
),
'postType' => array(
'type' => 'string',
'default' => '',
'validate_callback' => function( $param ) {
if ( '' === $param ) {
return true;
}

$allowed_post_types = array_keys( get_post_types() );

return in_array( $param, $allowed_post_types, true );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? sanitize_text_field( $value ) : 'post';
},
],
],
]
),
),
)
);

register_rest_route(
Expand All @@ -83,52 +107,84 @@ function rest_routes() {
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => [
'postIds' => [
'type' => [ 'bool', 'string' ],
'args' => array(
'postIds' => array(
'type' => array( 'bool', 'string' ),
'sanitize_callback' => function ( $value ) {
if ( empty( $value ) ) {
return false;
}

return is_bool( $value ) ? rest_sanitize_boolean( $value ) : sanitize_text_field( $value );
},
],
'batchSize' => [
),
'batchSize' => array(
'type' => 'int',
'default' => 10,
'validate_callback' => function( $param ) {
return is_int( $param );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? absint( $value ) : 10;
},
],
'postSelection' => [
),
'postSelection' => array(
'type' => 'string',
'default' => 'all',
'validate_callback' => function( $param ) {
$allowed_post_selection_options = array(
'all',
'posts',
'all_from_post_type',
);

return in_array( $param, $allowed_post_selection_options, true );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? sanitize_text_field( $value ) : 'all';
},
],
'postType' => [
),
'postType' => array(
'type' => 'string',
'default' => '',
'validate_callback' => function( $param ) {
if ( '' === $param ) {
return true;
}

$allowed_post_types = array_keys( get_post_types() );

return in_array( $param, $allowed_post_types, true );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? sanitize_text_field( $value ) : 'post';
},
],
'dryRun' => [
),
'dryRun' => array(
'type' => 'bool',
'default' => true,
'validate_callback' => function( $param ) {
return is_bool( $param );
},
'sanitize_callback' => function ( $value ) {
if ( false === $value ) {
return false;
}

return true;
},
],
'offset' => [
),
'offset' => array(
'type' => 'int',
'default' => 0,
'validate_callback' => function( $param ) {
return is_int( $param );
},
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) ? absint( $value ) : 0;
},
],
],
),
),
)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/js/classic-editor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jQuery from 'jquery';
import blurInsecure from './utils/blur-insecure';
import checkContent from './utils/check-content';
import findElements from './utils/find-elements';
Expand Down
1 change: 1 addition & 0 deletions src/js/gutenberg.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jQuery from 'jquery';
import { debounce } from 'underscore';
import { getScrollContainer } from '@wordpress/dom';
import apiRequest from '@wordpress/api-request';
Expand Down
2 changes: 2 additions & 0 deletions src/js/utils/blur-insecure.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import jQuery from 'jquery';

/**
* Find element on page
*/
Expand Down
1 change: 1 addition & 0 deletions src/js/utils/check-content.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jQuery from 'jquery';
import { __, _nx, sprintf } from '@wordpress/i18n';
import blurInsecure from './blur-insecure';
import { scanElements } from './scan-elements';
Expand Down
1 change: 1 addition & 0 deletions src/js/utils/find-elements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { get } from 'underscore';
import jQuery from 'jquery';

/**
* Find element on page
Expand Down
2 changes: 2 additions & 0 deletions src/js/utils/replace.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import jQuery from 'jquery';

/**
* Replace the urls in post content
*
Expand Down

0 comments on commit 7226db7

Please sign in to comment.