Fixed: CodeFactor issues

This commit is contained in:
Bogdan Rapaić 2020-04-17 14:14:10 +02:00 committed by GitHub
parent fc55fcb6d5
commit 69bf22c065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,15 +1,12 @@
<?php <?php
/**
/*
*
* Credits * Credits
* *
* Plugin-Name: Broken Link Checker * Plugin-Name: Broken Link Checker
* Plugin URI: https://wordpress.org/plugins/broken-link-checker/ * Plugin URI: https://wordpress.org/plugins/broken-link-checker/
* Author: Janis Elsts, Vladimir Prelovac * Author: Janis Elsts, Vladimir Prelovac
* *
* The code is used for the MainWP Broken Links Checker Extension * The code is used for the MainWP Broken Links Checker Extension (Retired Extension)
* Extension URL: https://mainwp.com/extension/broken-links-checker/
* *
*/ */
@ -18,7 +15,7 @@ class MainWP_Child_Links_Checker {
public static $instance = null; public static $instance = null;
public $is_plugin_installed = false; public $is_plugin_installed = false;
static function Instance() { public static function Instance() {
if ( null === self::$instance ) { if ( null === self::$instance ) {
self::$instance = new MainWP_Child_Links_Checker(); self::$instance = new MainWP_Child_Links_Checker();
} }
@ -48,7 +45,6 @@ class MainWP_Child_Links_Checker {
blc_init(); blc_init();
MainWP_Helper::update_option( 'mainwp_linkschecker_ext_enabled', 'Y', 'yes' ); MainWP_Helper::update_option( 'mainwp_linkschecker_ext_enabled', 'Y', 'yes' );
// need this try()
try { try {
if ( isset( $_POST['mwp_action'] ) ) { if ( isset( $_POST['mwp_action'] ) ) {
switch ( $_POST['mwp_action'] ) { switch ( $_POST['mwp_action'] ) {
@ -113,7 +109,7 @@ class MainWP_Child_Links_Checker {
blc_cleanup_links(); blc_cleanup_links();
} }
function save_settings() { public function save_settings() {
$information = array(); $information = array();
$check_threshold = intval( $_POST['check_threshold'] ); $check_threshold = intval( $_POST['check_threshold'] );
if ( $check_threshold > 0 ) { if ( $check_threshold > 0 ) {
@ -125,7 +121,7 @@ class MainWP_Child_Links_Checker {
return $information; return $information;
} }
function force_recheck() { public function force_recheck() {
$this->initiate_recheck(); $this->initiate_recheck();
$information = array(); $information = array();
$information['result'] = 'SUCCESS'; $information['result'] = 'SUCCESS';
@ -133,14 +129,14 @@ class MainWP_Child_Links_Checker {
return $information; return $information;
} }
function initiate_recheck() { public function initiate_recheck() {
global $wpdb; global $wpdb;
/** @var wpdb $wpdb */ /** @var wpdb $wpdb */
// Delete all discovered instances // Delete all discovered instances.
$wpdb->query( "TRUNCATE {$wpdb->prefix}blc_instances" ); $wpdb->query( "TRUNCATE {$wpdb->prefix}blc_instances" );
// Delete all discovered links // Delete all discovered links.
$wpdb->query( "TRUNCATE {$wpdb->prefix}blc_links" ); $wpdb->query( "TRUNCATE {$wpdb->prefix}blc_links" );
// Mark all posts, custom fields and bookmarks for processing. // Mark all posts, custom fields and bookmarks for processing.
@ -158,18 +154,18 @@ class MainWP_Child_Links_Checker {
} }
blc_init(); blc_init();
// Get the container type matching the type of the deleted post // Get the container type matching the type of the deleted post.
$post = get_post( $post_id ); $post = get_post( $post_id );
if ( ! $post ) { if ( ! $post ) {
return; return;
} }
// Get the associated container object // Get the associated container object.
$post_container = blcContainerHelper::get_container( array( $post->post_type, intval( $post_id ) ) ); $post_container = blcContainerHelper::get_container( array( $post->post_type, intval( $post_id ) ) );
if ( $post_container ) { if ( $post_container ) {
// Delete it // Delete it.
$post_container->delete(); $post_container->delete();
// Clean up any dangling links // Clean up any dangling links.
blc_cleanup_links(); blc_cleanup_links();
} }
} }
@ -186,7 +182,7 @@ class MainWP_Child_Links_Checker {
return $plugins; return $plugins;
} }
function update_footer( $text ) { public function update_footer( $text ) {
?> ?>
<script> <script>
jQuery( document ).ready( function () { jQuery( document ).ready( function () {
@ -199,7 +195,7 @@ class MainWP_Child_Links_Checker {
} }
function set_showhide() { public function set_showhide() {
$hide = isset( $_POST['showhide'] ) && ( 'hide' === $_POST['showhide'] ) ? 'hide' : ''; $hide = isset( $_POST['showhide'] ) && ( 'hide' === $_POST['showhide'] ) ? 'hide' : '';
MainWP_Helper::update_option( 'mainwp_linkschecker_hide_plugin', $hide ); MainWP_Helper::update_option( 'mainwp_linkschecker_hide_plugin', $hide );
$information['result'] = 'SUCCESS'; $information['result'] = 'SUCCESS';
@ -207,71 +203,70 @@ class MainWP_Child_Links_Checker {
return $information; return $information;
} }
// ok
public function syncOthersData( $information, $data = array() ) { public function syncOthersData( $information, $data = array() ) {
if ( isset( $data['syncBrokenLinksCheckerData'] ) && $data['syncBrokenLinksCheckerData'] ) { if ( isset( $data['syncBrokenLinksCheckerData'] ) && $data['syncBrokenLinksCheckerData'] ) {
try { try {
$information['syncBrokenLinksCheckerData'] = $this->get_sync_data(); $information['syncBrokenLinksCheckerData'] = $this->get_sync_data();
} catch ( Exception $e ) { } catch ( Exception $e ) {
// ok!
} }
} }
return $information; return $information;
} }
function get_sync_data( $strategy = '' ) { public function get_sync_data( $strategy = '' ) {
$information = array(); $information = array();
$data = $this->get_count_links(); $data = $this->get_count_links();
if ( is_array($data) ) { if ( is_array( $data ) ) {
$information['data'] = $data; $information['data'] = $data;
} }
return $information; return $information;
} }
function get_links_data() { public function get_links_data() {
if ( ! defined('BLC_DIRECTORY') ) { if ( ! defined( 'BLC_DIRECTORY' ) ) {
return; return;
} }
$file_path1 = BLC_DIRECTORY . '/includes/link-query.php'; $file_path1 = BLC_DIRECTORY . '/includes/link-query.php';
$file_path2 = BLC_DIRECTORY . '/includes/modules.php'; $file_path2 = BLC_DIRECTORY . '/includes/modules.php';
MainWP_Helper::check_files_exists(array( $file_path1, $file_path2 )); MainWP_Helper::check_files_exists( array( $file_path1, $file_path2 ) );
require_once $file_path1; require_once $file_path1;
require_once $file_path2; require_once $file_path2;
MainWP_Helper::check_classes_exists('blcLinkQuery'); MainWP_Helper::check_classes_exists( 'blcLinkQuery' );
MainWP_Helper::check_methods('blcLinkQuery', 'getInstance'); MainWP_Helper::check_methods( 'blcLinkQuery', 'getInstance' );
$blc_link_query = blcLinkQuery::getInstance(); $blc_link_query = blcLinkQuery::getInstance();
MainWP_Helper::check_methods($blc_link_query, 'get_filter_links'); MainWP_Helper::check_methods( $blc_link_query, 'get_filter_links' );
$total = $blc_link_query->get_filter_links( 'all', array( 'count_only' => true ) ); $total = $blc_link_query->get_filter_links( 'all', array( 'count_only' => true ) );
$max_results = isset($_POST['max_results']) ? intval($_POST['max_results']) : 50; $max_results = isset( $_POST['max_results'] ) ? intval( $_POST['max_results'] ) : 50;
$offset = isset($_POST['offset']) ? intval($_POST['offset']) : 0; $offset = isset( $_POST['offset'] ) ? intval( $_POST['offset'] ) : 0;
$params = array( $params = array(
array( 'load_instances' => true ), array( 'load_instances' => true ),
'max_results' => $max_results, 'max_results' => $max_results,
); );
if ( empty($offset) ) { if ( empty( $offset ) ) {
$first_sync = true; $first_sync = true;
} else { } else {
$params['offset'] = $offset; $params['offset'] = $offset;
} }
$link_data = $this->links_checker_data($params); $link_data = $this->links_checker_data( $params );
$total_sync = 0; $total_sync = 0;
if ( $offset ) { if ( $offset ) {
$total_sync = $offset; $total_sync = $offset;
} }
$total_sync += ( is_array($link_data) ? count($link_data) : 0 ); $total_sync += ( is_array( $link_data ) ? count( $link_data ) : 0 );
$information = array( 'links_data' => $link_data ); $information = array( 'links_data' => $link_data );
@ -291,26 +286,26 @@ class MainWP_Child_Links_Checker {
return $information; return $information;
} }
function get_count_links() { public function get_count_links() {
if ( ! defined('BLC_DIRECTORY') ) { if ( ! defined( 'BLC_DIRECTORY' ) ) {
return; return;
} }
$file_path1 = BLC_DIRECTORY . '/includes/link-query.php'; $file_path1 = BLC_DIRECTORY . '/includes/link-query.php';
$file_path2 = BLC_DIRECTORY . '/includes/modules.php'; $file_path2 = BLC_DIRECTORY . '/includes/modules.php';
MainWP_Helper::check_files_exists(array( $file_path1, $file_path2 )); MainWP_Helper::check_files_exists( array( $file_path1, $file_path2 ) );
require_once $file_path1; require_once $file_path1;
require_once $file_path2; require_once $file_path2;
MainWP_Helper::check_classes_exists('blcLinkQuery'); MainWP_Helper::check_classes_exists( 'blcLinkQuery' );
MainWP_Helper::check_methods('blcLinkQuery', 'getInstance'); MainWP_Helper::check_methods( 'blcLinkQuery', 'getInstance' );
$data = array(); $data = array();
$blc_link_query = blcLinkQuery::getInstance(); $blc_link_query = blcLinkQuery::getInstance();
MainWP_Helper::check_methods($blc_link_query, 'get_filter_links'); MainWP_Helper::check_methods( $blc_link_query, 'get_filter_links' );
$data['broken'] = $blc_link_query->get_filter_links( 'broken', array( 'count_only' => true ) ); $data['broken'] = $blc_link_query->get_filter_links( 'broken', array( 'count_only' => true ) );
$data['redirects'] = $blc_link_query->get_filter_links( 'redirects', array( 'count_only' => true ) ); $data['redirects'] = $blc_link_query->get_filter_links( 'redirects', array( 'count_only' => true ) );
@ -320,10 +315,10 @@ class MainWP_Child_Links_Checker {
return $data; return $data;
} }
function links_checker_data( $params ) { public function links_checker_data( $params ) {
MainWP_Helper::check_functions('blc_get_links'); MainWP_Helper::check_functions( 'blc_get_links' );
MainWP_Helper::check_classes_exists('blcLink'); MainWP_Helper::check_classes_exists( 'blcLink' );
$links = blc_get_links( $params ); $links = blc_get_links( $params );
@ -345,7 +340,6 @@ class MainWP_Child_Links_Checker {
'last_success', 'last_success',
'may_recheck', 'may_recheck',
'false_positive', 'false_positive',
// 'result_hash',
'dismissed', 'dismissed',
'status_text', 'status_text',
'status_code', 'status_code',
@ -374,7 +368,7 @@ class MainWP_Child_Links_Checker {
$days_broken = 0; $days_broken = 0;
if ( $link->broken ) { if ( $link->broken ) {
// Add a highlight to broken links that appear to be permanently broken // Add a highlight to broken links that appear to be permanently broken.
$days_broken = intval( ( time() - $link->first_failure ) / ( 3600 * 24 ) ); $days_broken = intval( ( time() - $link->first_failure ) / ( 3600 * 24 ) );
if ( $days_broken >= $blc_option['failure_duration_threshold'] ) { if ( $days_broken >= $blc_option['failure_duration_threshold'] ) {
$extra_info['permanently_broken'] = 1; $extra_info['permanently_broken'] = 1;
@ -388,24 +382,22 @@ class MainWP_Child_Links_Checker {
$get_link = new blcLink( intval( $link->link_id ) ); $get_link = new blcLink( intval( $link->link_id ) );
if ( $get_link->valid() ) { if ( $get_link->valid() ) {
MainWP_Helper::check_methods($get_link, 'get_instances'); MainWP_Helper::check_methods( $get_link, 'get_instances' );
$instances = $get_link->get_instances(); $instances = $get_link->get_instances();
} }
if ( ! empty( $instances ) ) { if ( ! empty( $instances ) ) {
$first_instance = reset( $instances ); $first_instance = reset( $instances );
MainWP_Helper::check_methods($first_instance, array( 'ui_get_link_text', 'get_container', 'is_link_text_editable', 'is_url_editable' ) ); MainWP_Helper::check_methods( $first_instance, array( 'ui_get_link_text', 'get_container', 'is_link_text_editable', 'is_url_editable' ) );
$new_link->link_text = $first_instance->ui_get_link_text(); $new_link->link_text = $first_instance->ui_get_link_text();
$extra_info['count_instance'] = count( $instances ); $extra_info['count_instance'] = count( $instances );
$container = $first_instance->get_container(); $container = $first_instance->get_container();
/** @var blcContainer $container */ if ( ! empty( $container ) ) {
if ( true === MainWP_Helper::check_properties( $first_instance, array( 'container_field' ), true ) ) {
if ( ! empty( $container ) /* && ($container instanceof blcAnyPostContainer) */ ) { if ( true === MainWP_Helper::check_properties( $container, array( 'container_type', 'container_id' ), true ) ) {
if ( true === MainWP_Helper::check_properties($first_instance, array( 'container_field' ), true ) ) {
if ( true === MainWP_Helper::check_properties($container, array( 'container_type', 'container_id' ), true ) ) {
$extra_info['container_type'] = $container->container_type; $extra_info['container_type'] = $container->container_type;
$extra_info['container_id'] = $container->container_id; $extra_info['container_id'] = $container->container_id;
$extra_info['source_data'] = $this->ui_get_source( $container, $first_instance->container_field ); $extra_info['source_data'] = $this->ui_get_source( $container, $first_instance->container_field );
@ -444,7 +436,7 @@ class MainWP_Child_Links_Checker {
$new_link->link_text = ''; $new_link->link_text = '';
$extra_info['count_instance'] = 0; $extra_info['count_instance'] = 0;
} }
$new_link->extra_info = base64_encode(serialize($extra_info)); $new_link->extra_info = base64_encode( serialize( $extra_info ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for benign reasons.
$new_link->synced = 1; $new_link->synced = 1;
$return[] = $new_link; $return[] = $new_link;
} }
@ -455,17 +447,17 @@ class MainWP_Child_Links_Checker {
return $return; return $return;
} }
function edit_link() { public function edit_link() {
$information = array(); $information = array();
if ( ! current_user_can( 'edit_others_posts' ) ) { if ( ! current_user_can( 'edit_others_posts' ) ) {
$information['error'] = 'NOTALLOW'; $information['error'] = 'NOTALLOW';
return $information; return $information;
} }
// Load the link // Load the link.
$link = new blcLink( intval( $_POST['link_id'] ) ); $link = new blcLink( intval( $_POST['link_id'] ) );
if ( ! $link->valid() ) { if ( ! $link->valid() ) {
$information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link $information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link.
return $information; return $information;
} }
@ -515,10 +507,9 @@ class MainWP_Child_Links_Checker {
'ui_link_text' => isset( $new_text ) ? $ui_link_text : null, 'ui_link_text' => isset( $new_text ) ? $ui_link_text : null,
'errors' => array(), 'errors' => array(),
); );
// url, status text, status code, link text, editable link text // url, status text, status code, link text, editable link text.
foreach ( $rez['errors'] as $error ) { foreach ( $rez['errors'] as $error ) {
/** @var $error WP_Error */
array_push( $response['errors'], implode( ', ', $error->get_error_messages() ) ); array_push( $response['errors'], implode( ', ', $error->get_error_messages() ) );
} }
@ -526,7 +517,7 @@ class MainWP_Child_Links_Checker {
} }
} }
function unlink() { public function unlink() {
$information = array(); $information = array();
if ( ! current_user_can( 'edit_others_posts' ) ) { if ( ! current_user_can( 'edit_others_posts' ) ) {
$information['error'] = 'NOTALLOW'; $information['error'] = 'NOTALLOW';
@ -535,15 +526,15 @@ class MainWP_Child_Links_Checker {
} }
if ( isset( $_POST['link_id'] ) ) { if ( isset( $_POST['link_id'] ) ) {
// Load the link // Load the link.
$link = new blcLink( intval( $_POST['link_id'] ) ); $link = new blcLink( intval( $_POST['link_id'] ) );
if ( ! $link->valid() ) { if ( ! $link->valid() ) {
$information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link $information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link.
return $information; return $information;
} }
// Try and unlink it // Try and unlink it.
$rez = $link->unlink(); $rez = $link->unlink();
if ( false === $rez ) { if ( false === $rez ) {
@ -580,21 +571,21 @@ class MainWP_Child_Links_Checker {
} }
if ( isset( $_POST['link_id'] ) ) { if ( isset( $_POST['link_id'] ) ) {
// Load the link // Load the link.
$link = new blcLink( intval( $_POST['link_id'] ) ); $link = new blcLink( intval( $_POST['link_id'] ) );
if ( ! $link->valid() ) { if ( ! $link->valid() ) {
$information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link $information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link.
return $information; return $information;
} }
$link->dismissed = $dismiss; $link->dismissed = $dismiss;
// Save the changes // Save the changes.
if ( $link->save() ) { if ( $link->save() ) {
$information = 'OK'; $information = 'OK';
} else { } else {
$information['error'] = 'COULDNOTMODIFY'; // Oops, couldn't modify the link $information['error'] = 'COULDNOTMODIFY'; // Oops, couldn't modify the link.
} }
return $information; return $information;
@ -613,26 +604,26 @@ class MainWP_Child_Links_Checker {
return $information; return $information;
} }
if ( isset( $_POST['link_id'] ) ) { if ( isset( $_POST['link_id'] ) ) {
// Load the link // Load the link.
$link = new blcLink( intval( $_POST['link_id'] ) ); $link = new blcLink( intval( $_POST['link_id'] ) );
if ( ! $link->valid() ) { if ( ! $link->valid() ) {
$information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link $information['error'] = 'NOTFOUNDLINK'; // Oops, I can't find the link.
return $information; return $information;
} }
// Make it appear "not broken" // Make it appear "not broken".
$link->broken = false; $link->broken = false;
$link->false_positive = true; $link->false_positive = true;
$link->last_check_attempt = time(); $link->last_check_attempt = time();
$link->log = __( 'This link was manually marked as working by the user.', 'mainwp-child' ); $link->log = __( 'This link was manually marked as working by the user.', 'mainwp-child' );
// Save the changes // Save the changes.
if ( $link->save() ) { if ( $link->save() ) {
$information['status'] = 'OK'; $information['status'] = 'OK';
$information['last_check_attempt'] = $link->last_check_attempt; $information['last_check_attempt'] = $link->last_check_attempt;
} else { } else {
$information['error'] = 'COULDNOTMODIFY'; // Oops, couldn't modify the link $information['error'] = 'COULDNOTMODIFY'; // Oops, couldn't modify the link.
} }
} else { } else {
$information['error'] = __( 'Error: link_id is not specified.', 'mainwp-child' ); $information['error'] = __( 'Error: link_id is not specified.', 'mainwp-child' );
@ -641,7 +632,7 @@ class MainWP_Child_Links_Checker {
return $information; return $information;
} }
function ui_get_source( $container, $container_field = '' ) { public function ui_get_source( $container, $container_field = '' ) {
if ( 'comment' === $container->container_type ) { if ( 'comment' === $container->container_type ) {
return $this->ui_get_source_comment( $container, $container_field ); return $this->ui_get_source_comment( $container, $container_field );
} elseif ( $container instanceof blcAnyPostContainer ) { } elseif ( $container instanceof blcAnyPostContainer ) {
@ -651,7 +642,7 @@ class MainWP_Child_Links_Checker {
return array(); return array();
} }
function ui_get_source_comment( $container, $container_field = '' ) { public function ui_get_source_comment( $container, $container_field = '' ) {
// Display a comment icon. // Display a comment icon.
if ( 'comment_author_url' === $container_field ) { if ( 'comment_author_url' === $container_field ) {
$image = 'font-awesome/font-awesome-user.png'; $image = 'font-awesome/font-awesome-user.png';
@ -659,13 +650,13 @@ class MainWP_Child_Links_Checker {
$image = 'font-awesome/font-awesome-comment-alt.png'; $image = 'font-awesome/font-awesome-comment-alt.png';
} }
if ( true !== MainWP_Helper::check_methods($container, array( 'get_wrapped_object' ), true ) ) { if ( true !== MainWP_Helper::check_methods( $container, array( 'get_wrapped_object' ), true ) ) {
return false; return false;
} }
$comment = $container->get_wrapped_object(); $comment = $container->get_wrapped_object();
// Display a small text sample from the comment // Display a small text sample from the comment.
$text_sample = strip_tags( $comment->comment_content ); $text_sample = strip_tags( $comment->comment_content );
$text_sample = blcUtility::truncate( $text_sample, 65 ); $text_sample = blcUtility::truncate( $text_sample, 65 );
@ -681,12 +672,11 @@ class MainWP_Child_Links_Checker {
); );
} }
function ui_get_source_post( $container, $container_field = '' ) { public function ui_get_source_post( $container, $container_field = '' ) {
return array( return array(
'post_title' => get_the_title( $container->container_id ), 'post_title' => get_the_title( $container->container_id ),
'post_status' => get_post_status($container->container_id), 'post_status' => get_post_status( $container->container_id ),
'container_anypost' => true, 'container_anypost' => true,
); );
} }
} }