Skip to content

Commit

Permalink
Initial commit 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kmgalanakis committed Apr 17, 2023
1 parent 9e1e57d commit 8837f00
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 33 deletions.
5 changes: 5 additions & 0 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function setup() {
add_action( 'admin_menu', __NAMESPACE__ . '\\admin_menu' );
}

/**
* Register the admin menu.
*
* @return void
*/
function admin_menu() {
$hook = add_management_page(
__( 'Insecure Content Warning Admin', 'insecure-content-warning' ),
Expand Down
41 changes: 24 additions & 17 deletions includes/classes/FixInsecureContent.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Handles the fixing of insecure content in posts for REST and WP CLI requests.
*
* @package ICW
*/

namespace ICW\FIX;

Expand All @@ -7,8 +12,10 @@
use WP_CLI_Command;
use WP_Query;

/**
* FixInsecureContent class.
*/
class FixInsecureContent {

/**
* Is current run a dry run?
*
Expand All @@ -35,7 +42,7 @@ class FixInsecureContent {
*
* @var array
*/
private array $warning_count = [];
private array $warning_count = array();

/**
* Class instance
Expand Down Expand Up @@ -67,12 +74,12 @@ public static function get_instance() {
* @return int
*/
public function count_post_to_check( string $post_type, int $posts_per_page, int $post_offset ): int {
$args = [
$args = array(
'posts_per_page' => $posts_per_page,
'post_type' => 'all' === $post_type ? 'any' : $post_type,
'offset' => $post_offset,
'nopaging' => true,
];
);

$query = new WP_Query( $args );

Expand All @@ -92,7 +99,7 @@ public function count_post_to_check( string $post_type, int $posts_per_page, int
*
* @return array|void
*/
public function fix( $include, $all, $post_type, $posts_per_page, $post_offset, $dry_run, $args = [] ) {
public function fix( $include, $all, $post_type, $posts_per_page, $post_offset, $dry_run, $args = array() ) {
$this->dry_run = $dry_run;

if ( defined( 'WP_CLI' ) && WP_CLI ) {
Expand All @@ -112,13 +119,13 @@ public function fix( $include, $all, $post_type, $posts_per_page, $post_offset,

// Loop through all posts and fix content.
while ( true ) {
$args = [
$args = array(
'posts_per_page' => $posts_per_page,
'post_type' => $post_type,
'offset' => $post_offset,
'orderby' => 'ID',
'order' => 'ASC',
];
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
Expand Down Expand Up @@ -151,7 +158,7 @@ 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, [ 'URL(s) fixed summary' ] );
Utils\format_items( 'table', $this->fixed_post_count, array( 'URL(s) fixed summary' ) );
} else {
if ( null === $this->fixed_post_count ) {
return __( 'No post(s) found', 'insecure-content-warning' );
Expand Down Expand Up @@ -190,7 +197,7 @@ protected function fix_insecure_content( $post_id ) {
// translators: Message to show when the system is unable to fetch details for the post.
WP_CLI::warning( $message );
} else {
$this->warning_count[] = [ __( 'Warning', 'insecure-content-warning' ) => $message ];
$this->warning_count[] = array( __( 'Warning', 'insecure-content-warning' ) => $message );
}
return '';
}
Expand All @@ -207,15 +214,15 @@ protected function fix_insecure_content( $post_id ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::warning( $message );
} else {
$this->warning_count[] = [ __( 'Warning', 'insecure-content-warning' ) => $message ];
$this->warning_count[] = array( __( 'Warning', 'insecure-content-warning' ) => $message );
}

if ( $this->dry_run ) {
// translators: Summary to show on dry run fix.
$this->fixed_post_count[] = [ __( 'URL(s) fixed summary', 'insecure-content-warning' ) => sprintf( __( '0/0 URL(s) will be fixed in post %s', 'insecure-content-warning' ), $post_id ) ];
$this->fixed_post_count[] = array( __( 'URL(s) fixed summary', 'insecure-content-warning' ) => sprintf( __( '0/0 URL(s) will be fixed in post %s', 'insecure-content-warning' ), $post_id ) );
} else {
// translators: Summary to show on fix.
$this->fixed_post_count[] = [ __( 'URL(s) fixed summary', 'insecure-content-warning' ) => sprintf( __( '0/0 URL(s) fixed in post %s', 'insecure-content-warning' ), $post_id ) ];
$this->fixed_post_count[] = array( __( 'URL(s) fixed summary', 'insecure-content-warning' ) => sprintf( __( '0/0 URL(s) fixed in post %s', 'insecure-content-warning' ), $post_id ) );
}

return '';
Expand All @@ -232,10 +239,10 @@ protected function fix_insecure_content( $post_id ) {
if ( false === $this->dry_run ) {
// Update post content with HTTPS URLs.
wp_update_post(
[
array(
'ID' => $post_id,
'post_content' => $post_content,
]
)
);
}

Expand All @@ -245,14 +252,14 @@ protected function fix_insecure_content( $post_id ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::debug( $dry_run_message );
}
$this->fixed_post_count[] = [ 'URL(s) fixed summary' => $dry_run_message ];
$this->fixed_post_count[] = array( 'URL(s) fixed summary' => $dry_run_message );
} else {
// translators: Summary to show on fix.
$success_message = sprintf( __( '%1$s/%2$s insecure URLs fixed in post %3$s.', 'insecure-content-warning' ), $count, count( $urls ), $post_id );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::debug( $success_message );
}
$this->fixed_post_count[] = [ 'URL(s) fixed summary' => $success_message ];
$this->fixed_post_count[] = array( 'URL(s) fixed summary' => $success_message );
}
}

Expand Down Expand Up @@ -286,7 +293,7 @@ protected function does_secure_content_exist( $url ) {
*/
protected function parse_content_for_insecure_urls( $post_content ) {
// Check all src and create an array of URLs to check https URL availability.
$src_urls = [];
$src_urls = array();
$src_regex = '/src="([^"]+)"/';
preg_match_all( $src_regex, $post_content, $matches, PREG_SET_ORDER, 0 );

Expand Down
17 changes: 11 additions & 6 deletions includes/partials/admin-page.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?php
/**
* The admin page template.
*
* @package ICW
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

$post_types = get_post_types(
[
array(
'show_ui' => true,
'public' => true,
],
),
'objects'
);
?>
Expand Down Expand Up @@ -42,11 +47,11 @@
<th scope="row"><label for="icw-post-type"><?php esc_html_e( 'Post type', 'insecure-content-warning' ); ?></label></th>
<td>
<select name="post_type" id="icw-post-type">
<?php foreach ( $post_types as $post_type ) : ?>
<?php if ( array_values( $post_types )[0] === $post_type ) : ?>
<option selected="selected" value="<?php echo esc_attr( $post_type->name ); ?>"><?php echo esc_html( $post_type->label ); ?></option>
<?php foreach ( $post_types as $pt ) : ?>
<?php if ( array_values( $post_types )[0] === $pt ) : ?>
<option selected="selected" value="<?php echo esc_attr( $pt->name ); ?>"><?php echo esc_html( $pt->label ); ?></option>
<?php else : ?>
<option value="<?php echo esc_attr( $post_type->name ); ?>"><?php echo esc_html( $post_type->label ); ?></option>
<option value="<?php echo esc_attr( $pt->name ); ?>"><?php echo esc_html( $pt->label ); ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
Expand Down
12 changes: 6 additions & 6 deletions includes/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@ function prepare_fix_params( WP_REST_Request $request ): array {

$args = wp_parse_args(
$params,
[
array(
'postIds' => false,
'postSelection' => 'all',
'postType' => 'post',
'batchSize' => 10,
'offset' => 0,
'dryRun' => false,
]
)
);

return [
return array(
'include' => false === $params['postIds'] ? rest_sanitize_boolean( $params['postIds'] ) : sanitize_text_field( $params['postIds'] ),
'all' => 'all' === sanitize_text_field( $params['postSelection'] ),
'post_type' => 'all' !== sanitize_text_field( $params['postSelection'] ) ? sanitize_text_field( $params['postType'] ) : 'any',
'batch_size' => absint( $args['batchSize'] ),
'post_offset' => absint( $args['offset'] ),
'dry_run' => ! empty( $params['dryRun'] ) && rest_sanitize_boolean( $params['dryRun'] ),
];
);
}

/**
Expand All @@ -119,7 +119,7 @@ function fix_endpoint( WP_REST_Request $request ): WP_REST_Response {

$fixed_post_count = FixInsecureContent::get_instance()->fix( $params['include'], $params['all'], $params['post_type'], $params['batch_size'], $params['post_offset'], $params['dry_run'] );

return rest_ensure_response( $fixed_post_count ?? [] );
return rest_ensure_response( $fixed_post_count ?? array() );
}

/**
Expand All @@ -134,5 +134,5 @@ function count_for_fix_endpoint( WP_REST_Request $request ): WP_REST_Response {

$posts_to_be_fixed_count = FixInsecureContent::get_instance()->count_post_to_check( $params['post_type'], $params['batch_size'], $params['post_offset'] );

return rest_ensure_response( $posts_to_be_fixed_count ?? [] );
return rest_ensure_response( $posts_to_be_fixed_count ?? array() );
}
20 changes: 16 additions & 4 deletions src/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ document.querySelector('#icw-fix-content-btn').addEventListener(

const setLogHTML = (html) => {
log.innerHTML = html;

log.scrollTop = log.scrollHeight
}

const appendLogHTML = (html) => {
Expand Down Expand Up @@ -72,7 +74,9 @@ document.querySelector('#icw-fix-content-btn').addEventListener(

hideElement(false, log);

for (let i=0 ; i<times; i++) {
setLogHTML(getProgressBar(0, times, batchSize, count))
let innerHTML = '';
for (let i=1 ; i<=times; i++) {
const data = await apiFetch({
path: `/icw/v1/fix`,
method: 'POST',
Expand All @@ -81,20 +85,28 @@ document.querySelector('#icw-fix-content-btn').addEventListener(
postIds: postSelection === 'posts' ? postIds : false,
postType: postSelection === 'all_from_post_type' ? postType: '',
batchSize,
offset : i * batchSize,
offset : (i-1) * batchSize,
dryRun,
},
});

appendLogHTML(data);
innerHTML += data;
setLogHTML(innerHTML + getProgressBar(i, times, batchSize, count) )
}

appendLogHTML( '<span class="info">' + sprintf( __( 'Total posts checked for insecure URL(s): %s', 'insecure-content-warning' ), count ) + '</span>' );
toggleSpinnerVisibility();
disableButton(false);
}
);

const getProgressBar = (time, times, batchSize, count) => {
const progressBarMax = 50;
const percentage = time > 0 ? Math.ceil( (100 / times) * (time) ) : 0;
const value = percentage * progressBarMax / 100;
let completed = time !== times ? batchSize*(time) : count;
return `<span>[${('#'.repeat(value))}${('-'.repeat(progressBarMax-value))}] | ${percentage}% || ${completed}/${count} ${__( 'posts', 'insecure-content-warning')}</span>`
}

document.querySelector('#icw-post-selection').addEventListener(
'change',
(event) => {
Expand Down

0 comments on commit 8837f00

Please sign in to comment.