Skip to content

Commit

Permalink
Merge pull request #513 from pattonwebz/william/464/move-edac_insert_…
Browse files Browse the repository at this point in the history
…rule_data-to-a-class

Move edac_insert_rule_data to an inserter class
  • Loading branch information
pattonwebz authored Mar 18, 2024
2 parents bacfd53 + 2340406 commit 8b3485e
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 106 deletions.
2 changes: 0 additions & 2 deletions accessibility-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@
require_once plugin_dir_path( __FILE__ ) . 'includes/meta-boxes.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/options-page.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/validate.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/insert.php';

/**
* Filters and Actions
*/
Expand Down
142 changes: 142 additions & 0 deletions admin/class-insert-rule-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Inserts rule data about a post to the database
*
* @since 1.10.0
*
* @package Accessibility_Checker
*/

namespace EDAC\Admin;

/**
* Class for inserting rule data into the database
*
* @since 1.10.0
*/
class Insert_Rule_Data {

/**
* Insert rule data into database
*
* @since 1.10.0
*
* @param object $post The post object. Must have a valid ID.
* @param string $rule The rule.
* @param string $ruletype The rule type.
* @param string $rule_obj The object.
*
* @return void|int|\WP_Error The ID of the inserted record, void if no
* record was inserted or a WP_Error if the insert failed.
*/
public function insert( object $post, string $rule, string $ruletype, string $rule_obj ) {

if ( ! isset( $post->ID, $post->post_type )
|| empty( $rule )
|| empty( $ruletype )
|| empty( $rule_obj )
) {
return;
}

global $wpdb;
$table_name = $wpdb->prefix . 'accessibility_checker';

// set up rule data array.
$rule_data = array(
'postid' => $post->ID,
'siteid' => get_current_blog_id(),
'type' => $post->post_type,
'rule' => $rule,
'ruletype' => $ruletype,
'object' => esc_attr( $rule_obj ),
'recordcheck' => 1,
'user' => get_current_user_id(),
'ignre' => 0,
'ignre_user' => null,
'ignre_date' => null,
'ignre_comment' => null,
'ignre_global' => 0,
);

// return if revision.
if ( 'revision' === $rule_data['type'] ) {
return;
}

// Check if exists.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for adding data to database, caching not required for one time operation.
$results = $wpdb->get_results(
$wpdb->prepare(
'SELECT postid, ignre FROM %i where type = %s and postid = %d and rule = %s and object = %s and siteid = %d',
$table_name,
$rule_data['type'],
$rule_data['postid'],
$rule_data['rule'],
$rule_data['object'],
$rule_data['siteid']
),
ARRAY_A
);

// Loop existing records.
if ( $results ) {
foreach ( $results as $row ) {

// if being ignored, don't overwrite value.
if ( true === (bool) $row['ignre'] ) {
$rule_data['ignre'] = 1;
}

// update existing record.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for adding data to database, caching not required for one time operation.
$wpdb->query(
$wpdb->prepare(
'UPDATE %i SET recordcheck = %d, ignre = %d WHERE siteid = %d and postid = %d and rule = %s and object = %s and type = %s',
$table_name,
1,
$rule_data['ignre'],
$rule_data['siteid'],
$rule_data['postid'],
$rule_data['rule'],
$rule_data['object'],
$rule_data['type']
)
);

}
}

// Insert new records.
if ( ! $results ) {

// filter post types.
$rule_data = apply_filters( 'edac_filter_insert_rule_data', $rule_data );

// Sanitize rule data since it is filtered, and we can't be sure
// the data is still as valid as it was when it was first set.
// Sanitize the filtered data.
$rule_data_sanitized = array(
'postid' => absint( $rule_data['postid'] ),
'siteid' => absint( $rule_data['siteid'] ),
'type' => sanitize_text_field( $rule_data['type'] ),
'rule' => sanitize_text_field( $rule_data['rule'] ),
'ruletype' => sanitize_text_field( $rule_data['ruletype'] ),
'object' => esc_attr( $rule_data['object'] ),
'recordcheck' => absint( $rule_data['recordcheck'] ),
'user' => absint( $rule_data['user'] ),
'ignre' => absint( $rule_data['ignre'] ),
'ignre_user' => isset( $rule_data['ignre_user'] ) ? absint( $rule_data['ignre_user'] ) : null,
'ignre_date' => isset( $rule_data['ignre_date'] ) ? sanitize_text_field( $rule_data['ignre_date'] ) : null,
'ignre_comment' => isset( $rule_data['ignre_comment'] ) ? sanitize_text_field( $rule_data['ignre_comment'] ) : null,
'ignre_global' => absint( $rule_data['ignre_global'] ),
);

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Using direct query for adding data to database.
$wpdb->insert( $table_name, $rule_data_sanitized );

// Return insert id or error.
return $wpdb->insert_id;
}
}
}
5 changes: 3 additions & 2 deletions includes/classes/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace EDAC\Inc;

use EDAC\Admin\Helpers;
use EDAC\Admin\Insert_Rule_Data;
use EDAC\Admin\Scans_Stats;
use EDAC\Admin\Settings;

Expand Down Expand Up @@ -195,7 +196,7 @@ public function set_post_scan_results( $request ) {
// TODO: setup a rules class for loading/filtering rules.
$rules = edac_register_rules();
$js_rule_ids = array();
foreach ( $rules as $rule ) {
foreach ( $rules as $rule ) {
if ( array_key_exists( 'ruleset', $rule ) && 'js' === $rule['ruleset'] ) {
$js_rule_ids[] = $rule['slug'];
}
Expand Down Expand Up @@ -235,7 +236,7 @@ public function set_post_scan_results( $request ) {

do_action( 'edac_before_rule', $post_id, $rule_id, 'js' );

edac_insert_rule_data( $post, $rule_id, $impact, $html );
( new Insert_Rule_Data() )->insert( $post, $rule_id, $impact, $html );

do_action( 'edac_after_rule', $post_id, $rule_id, 'js' );

Expand Down
17 changes: 17 additions & 0 deletions includes/deprecated/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @package Accessibility_Checker
*/

use EDAC\Admin\Insert_Rule_Data;
use EDAC\Admin\Purge_Post_Data;

/**
Expand Down Expand Up @@ -73,3 +74,19 @@ function edac_delete_cpt_posts( $post_type ) {
_deprecated_function( __FUNCTION__, '1.10.0', 'EDAC\Admin\Purge_Post_Data::delete_cpt_posts' );
Purge_Post_Data::delete_cpt_posts( $post_type );
}

/**
* Insert rule date into database
*
* @deprecated 1.10.0
*
* @param object $post The post object.
* @param string $rule The rule.
* @param string $ruletype The rule type.
* @param string $rule_obj The object.
* @return void|int
*/
function edac_insert_rule_data( $post, $rule, $ruletype, $rule_obj ) {
_deprecated_function( __FUNCTION__, '1.10.0', 'EDAC\Admin\Insert_Rule_Data' );
return ( new Insert_Rule_Data() )->insert( $post, $rule, $ruletype, $rule_obj );
}
100 changes: 0 additions & 100 deletions includes/insert.php

This file was deleted.

5 changes: 3 additions & 2 deletions includes/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use EDAC\Admin\Helpers;
use EDAC\Admin\Insert_Rule_Data;

/**
* Oxygen Builder on save
Expand Down Expand Up @@ -146,7 +147,7 @@ function edac_validate( $post_ID, $post, $action ) {
if ( $errors && is_array( $errors ) ) {
do_action( 'edac_rule_errors', $post_ID, $rule, $errors, $action );
foreach ( $errors as $error ) {
edac_insert_rule_data( $post, $rule['slug'], $rule['rule_type'], $object = $error );
( new Insert_Rule_Data() )->insert( $post, $rule['slug'], $rule['rule_type'], $object = $error );
}
}
if ( EDAC_DEBUG === true ) {
Expand Down Expand Up @@ -200,7 +201,7 @@ function edac_remove_corrected_posts( $post_ID, $type, $pre = 1, $ruleset = 'php
if ( 0 === count( $rule_slugs ) ) {
return;
}

if ( 1 === $pre ) {

// Set record flag before validating content.
Expand Down
Loading

0 comments on commit 8b3485e

Please sign in to comment.