2015-10-15 22:52:37 +10:00
< ? php
2020-04-17 14:14:10 +02:00
/**
2019-02-19 23:52:21 +07:00
* Credits
*
* Plugin - Name : Broken Link Checker
* Plugin URI : https :// wordpress . org / plugins / broken - link - checker /
* Author : Janis Elsts , Vladimir Prelovac
*
2020-04-17 14:14:10 +02:00
* The code is used for the MainWP Broken Links Checker Extension ( Retired Extension )
2020-04-17 12:14:19 +00:00
*/
2019-02-19 23:52:21 +07:00
2020-05-13 15:53:45 +07:00
// phpcs:disable PSR1.Classes.ClassDeclaration, WordPress.WP.AlternativeFunctions -- use external code.
2020-05-05 20:13:38 +07:00
2015-10-15 22:52:37 +10:00
class MainWP_Child_Links_Checker {
2020-03-26 19:45:07 +00:00
public static $instance = null ;
2020-03-27 15:13:11 +00:00
public $is_plugin_installed = false ;
2015-10-15 22:52:37 +10:00
2020-05-06 20:22:11 +07:00
public static function instance () {
2020-03-26 14:05:04 +00:00
if ( null === self :: $instance ) {
2020-05-07 23:22:05 +07:00
self :: $instance = new self ();
2015-10-15 22:52:37 +10:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2015-10-15 22:52:37 +10:00
}
2017-01-14 13:16:52 +01:00
public function __construct () {
2020-03-27 15:13:11 +00:00
require_once ABSPATH . 'wp-admin/includes/plugin.php' ;
2017-01-14 13:16:52 +01:00
if ( is_plugin_active ( 'broken-link-checker/broken-link-checker.php' ) ) {
2020-03-27 15:13:11 +00:00
$this -> is_plugin_installed = true ;
2017-01-14 13:16:52 +01:00
}
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
if ( ! $this -> is_plugin_installed ) {
return ;
}
2019-02-14 22:38:14 +07:00
2020-05-07 19:34:36 +07:00
add_filter ( 'mainwp_site_sync_others_data' , array ( $this , 'sync_others_data' ), 10 , 2 );
2017-01-14 13:16:52 +01:00
}
2015-10-15 22:52:37 +10:00
public function action () {
$information = array ();
if ( ! defined ( 'BLC_ACTIVE' ) || ! function_exists ( 'blc_init' ) ) {
$information [ 'error' ] = 'NO_BROKENLINKSCHECKER' ;
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2015-10-15 22:52:37 +10:00
}
blc_init ();
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
MainWP_Helper :: update_option ( 'mainwp_linkschecker_ext_enabled' , 'Y' , 'yes' );
try {
if ( isset ( $_POST [ 'mwp_action' ] ) ) {
switch ( $_POST [ 'mwp_action' ] ) {
case 'set_showhide' :
$information = $this -> set_showhide ();
break ;
case 'sync_data' :
$information = $this -> get_sync_data ();
break ;
case 'sync_links_data' :
$information = $this -> get_links_data ();
break ;
case 'edit_link' :
$information = $this -> edit_link ();
break ;
case 'unlink' :
$information = $this -> unlink ();
break ;
case 'set_dismiss' :
$information = $this -> set_link_dismissed ();
break ;
case 'discard' :
$information = $this -> discard ();
break ;
case 'save_settings' :
$information = $this -> save_settings ();
break ;
case 'force_recheck' :
$information = $this -> force_recheck ();
break ;
}
}
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( array ( 'error' => $e -> getMessage () ) );
2020-03-27 15:13:11 +00:00
}
2015-10-15 22:52:37 +10:00
}
public function init () {
if ( get_option ( 'mainwp_linkschecker_ext_enabled' ) !== 'Y' ) {
return ;
}
if ( get_option ( 'mainwp_linkschecker_hide_plugin' ) === 'hide' ) {
add_filter ( 'all_plugins' , array ( $this , 'hide_plugin' ) );
add_filter ( 'update_footer' , array ( & $this , 'update_footer' ), 15 );
}
}
public static function hook_trashed_comment ( $comment_id ) {
if ( get_option ( 'mainwp_linkschecker_ext_enabled' ) !== 'Y' ) {
return ;
}
if ( ! defined ( 'BLC_ACTIVE' ) || ! function_exists ( 'blc_init' ) ) {
return ;
}
blc_init ();
$container = blcContainerHelper :: get_container ( array ( 'comment' , $comment_id ) );
$container -> delete ();
blc_cleanup_links ();
}
2020-04-17 14:14:10 +02:00
public function save_settings () {
2020-03-26 19:45:07 +00:00
$information = array ();
$check_threshold = intval ( $_POST [ 'check_threshold' ] );
2016-08-02 19:51:22 +02:00
if ( $check_threshold > 0 ) {
2015-10-15 22:52:37 +10:00
$conf = blc_get_configuration ();
2016-08-02 19:51:22 +02:00
$conf -> options [ 'check_threshold' ] = $check_threshold ;
$conf -> save_options ();
2015-10-15 22:52:37 +10:00
}
2016-08-02 19:51:22 +02:00
$information [ 'result' ] = 'SUCCESS' ;
2015-10-15 22:52:37 +10:00
return $information ;
}
2020-04-17 14:14:10 +02:00
public function force_recheck () {
2015-10-15 22:52:37 +10:00
$this -> initiate_recheck ();
$information = array ();
$information [ 'result' ] = 'SUCCESS' ;
return $information ;
}
2020-04-17 14:14:10 +02:00
public function initiate_recheck () {
2015-10-15 22:52:37 +10:00
global $wpdb ;
/** @var wpdb $wpdb */
2020-04-17 14:14:10 +02:00
// Delete all discovered instances.
2015-10-15 22:52:37 +10:00
$wpdb -> query ( " TRUNCATE { $wpdb -> prefix } blc_instances " );
2020-04-17 14:14:10 +02:00
// Delete all discovered links.
2015-10-15 22:52:37 +10:00
$wpdb -> query ( " TRUNCATE { $wpdb -> prefix } blc_links " );
2020-03-26 20:01:04 +00:00
// Mark all posts, custom fields and bookmarks for processing.
2015-10-15 22:52:37 +10:00
blc_resynch ( true );
}
public static function hook_post_deleted ( $post_id ) {
if ( get_option ( 'mainwp_linkschecker_ext_enabled' ) !== 'Y' ) {
return ;
}
if ( ! defined ( 'BLC_ACTIVE' ) || ! function_exists ( 'blc_init' ) ) {
return ;
}
blc_init ();
2020-04-17 14:14:10 +02:00
// Get the container type matching the type of the deleted post.
2015-10-15 22:52:37 +10:00
$post = get_post ( $post_id );
if ( ! $post ) {
return ;
}
2020-04-17 14:14:10 +02:00
// Get the associated container object.
2015-10-15 22:52:37 +10:00
$post_container = blcContainerHelper :: get_container ( array ( $post -> post_type , intval ( $post_id ) ) );
if ( $post_container ) {
2020-04-17 14:14:10 +02:00
// Delete it.
2015-10-15 22:52:37 +10:00
$post_container -> delete ();
2020-04-17 14:14:10 +02:00
// Clean up any dangling links.
2015-10-15 22:52:37 +10:00
blc_cleanup_links ();
}
}
public function hide_plugin ( $plugins ) {
foreach ( $plugins as $key => $value ) {
$plugin_slug = basename ( $key , '.php' );
if ( 'broken-link-checker' === $plugin_slug ) {
unset ( $plugins [ $key ] );
}
}
return $plugins ;
}
2020-04-17 14:14:10 +02:00
public function update_footer ( $text ) {
2015-10-15 22:52:37 +10:00
?>
< script >
jQuery ( document ) . ready ( function () {
jQuery ( '#menu-tools a[href="tools.php?page=view-broken-links"]' ) . closest ( 'li' ) . remove ();
jQuery ( '#menu-settings a[href="options-general.php?page=link-checker-settings"]' ) . closest ( 'li' ) . remove ();
} );
</ script >
< ? php
return $text ;
}
2020-04-17 14:14:10 +02:00
public function set_showhide () {
2015-10-15 22:52:37 +10:00
$hide = isset ( $_POST [ 'showhide' ] ) && ( 'hide' === $_POST [ 'showhide' ] ) ? 'hide' : '' ;
MainWP_Helper :: update_option ( 'mainwp_linkschecker_hide_plugin' , $hide );
$information [ 'result' ] = 'SUCCESS' ;
return $information ;
}
2020-05-06 00:47:59 +07:00
public function sync_others_data ( $information , $data = array () ) {
2020-03-27 15:13:11 +00:00
if ( isset ( $data [ 'syncBrokenLinksCheckerData' ] ) && $data [ 'syncBrokenLinksCheckerData' ] ) {
try {
$information [ 'syncBrokenLinksCheckerData' ] = $this -> get_sync_data ();
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2020-04-17 14:14:10 +02:00
// ok!
2020-03-27 15:13:11 +00:00
}
}
2018-06-26 19:52:53 +02:00
return $information ;
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
public function get_sync_data ( $strategy = '' ) {
2015-10-15 22:52:37 +10:00
$information = array ();
2016-08-02 19:51:22 +02:00
$data = $this -> get_count_links ();
2020-04-17 14:14:10 +02:00
if ( is_array ( $data ) ) {
2020-03-27 15:13:11 +00:00
$information [ 'data' ] = $data ;
}
2016-08-02 19:51:22 +02:00
return $information ;
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
public function get_links_data () {
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
if ( ! defined ( 'BLC_DIRECTORY' ) ) {
2020-03-26 19:51:58 +00:00
return ;
2020-03-27 15:13:11 +00:00
}
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
$file_path1 = BLC_DIRECTORY . '/includes/link-query.php' ;
$file_path2 = BLC_DIRECTORY . '/includes/modules.php' ;
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_files_exists ( array ( $file_path1 , $file_path2 ) );
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
require_once $file_path1 ;
require_once $file_path2 ;
2018-06-26 19:52:53 +02:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_classes_exists ( 'blcLinkQuery' );
MainWP_Helper :: check_methods ( 'blcLinkQuery' , 'getInstance' );
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$blc_link_query = blcLinkQuery :: getInstance ();
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_methods ( $blc_link_query , 'get_filter_links' );
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$total = $blc_link_query -> get_filter_links ( 'all' , array ( 'count_only' => true ) );
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
$max_results = isset ( $_POST [ 'max_results' ] ) ? intval ( $_POST [ 'max_results' ] ) : 50 ;
$offset = isset ( $_POST [ 'offset' ] ) ? intval ( $_POST [ 'offset' ] ) : 0 ;
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$params = array (
2020-03-26 14:11:33 +00:00
array ( 'load_instances' => true ),
'max_results' => $max_results ,
);
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
if ( empty ( $offset ) ) {
2019-02-14 22:38:14 +07:00
$first_sync = true ;
2016-08-02 19:51:22 +02:00
} else {
$params [ 'offset' ] = $offset ;
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
$link_data = $this -> links_checker_data ( $params );
2019-02-14 22:38:14 +07:00
2018-04-17 20:33:10 +02:00
$total_sync = 0 ;
2020-03-27 14:11:21 +00:00
if ( $offset ) {
2018-04-17 20:33:10 +02:00
$total_sync = $offset ;
}
2020-04-17 14:14:10 +02:00
$total_sync += ( is_array ( $link_data ) ? count ( $link_data ) : 0 );
2019-02-14 22:38:14 +07:00
2020-03-26 15:29:54 +00:00
$information = array ( 'links_data' => $link_data );
2019-02-14 22:38:14 +07:00
2020-03-27 14:11:21 +00:00
if ( $first_sync ) {
2016-08-02 19:51:22 +02:00
$information [ 'data' ] = $this -> get_count_links ();
}
2019-02-14 22:38:14 +07:00
2020-03-27 14:11:21 +00:00
if ( $total > $offset + $max_results ) {
2016-08-02 19:51:22 +02:00
$information [ 'sync_offset' ] = $offset + $max_results ;
} else {
2020-03-26 19:45:07 +00:00
$information [ 'last_sync' ] = 1 ;
2018-04-17 20:33:10 +02:00
$information [ 'total_sync' ] = $total_sync ;
2020-03-26 19:45:07 +00:00
$information [ 'data' ] = $this -> get_count_links ();
2019-02-14 22:38:14 +07:00
}
2016-08-02 19:51:22 +02:00
$information [ 'result' ] = 'success' ;
2019-02-14 22:38:14 +07:00
return $information ;
2016-08-02 19:51:22 +02:00
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
public function get_count_links () {
if ( ! defined ( 'BLC_DIRECTORY' ) ) {
2020-03-26 19:51:58 +00:00
return ;
2020-03-27 15:13:11 +00:00
}
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
$file_path1 = BLC_DIRECTORY . '/includes/link-query.php' ;
$file_path2 = BLC_DIRECTORY . '/includes/modules.php' ;
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_files_exists ( array ( $file_path1 , $file_path2 ) );
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
require_once $file_path1 ;
require_once $file_path2 ;
2018-06-26 19:52:53 +02:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_classes_exists ( 'blcLinkQuery' );
MainWP_Helper :: check_methods ( 'blcLinkQuery' , 'getInstance' );
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$data = array ();
$blc_link_query = blcLinkQuery :: getInstance ();
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_methods ( $blc_link_query , 'get_filter_links' );
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$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 [ 'dismissed' ] = $blc_link_query -> get_filter_links ( 'dismissed' , array ( 'count_only' => true ) );
2016-08-02 19:51:22 +02:00
$data [ 'warning' ] = $blc_link_query -> get_filter_links ( 'warning' , array ( 'count_only' => true ) );
2020-03-26 19:45:07 +00:00
$data [ 'all' ] = $blc_link_query -> get_filter_links ( 'all' , array ( 'count_only' => true ) );
2016-08-02 19:51:22 +02:00
return $data ;
2015-10-15 22:52:37 +10:00
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
public function links_checker_data ( $params ) {
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_functions ( 'blc_get_links' );
MainWP_Helper :: check_classes_exists ( 'blcLink' );
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$links = blc_get_links ( $params );
2019-02-14 22:38:14 +07:00
2016-08-02 19:51:22 +02:00
$filter_fields = array (
2015-10-15 22:52:37 +10:00
'link_id' ,
'url' ,
'being_checked' ,
'last_check' ,
'last_check_attempt' ,
'check_count' ,
'http_code' ,
'request_duration' ,
'timeout' ,
'redirect_count' ,
'final_url' ,
'broken' ,
2016-08-02 19:51:22 +02:00
'warning' ,
2015-10-15 22:52:37 +10:00
'first_failure' ,
'last_success' ,
'may_recheck' ,
'false_positive' ,
'dismissed' ,
'status_text' ,
'status_code' ,
2020-03-26 14:05:04 +00:00
'log' ,
2015-10-15 22:52:37 +10:00
);
2020-03-26 19:45:07 +00:00
$return = array ();
2019-02-14 22:38:14 +07:00
2015-10-15 22:52:37 +10:00
$blc_option = get_option ( 'wsblc_options' );
if ( is_string ( $blc_option ) && ! empty ( $blc_option ) ) {
$blc_option = json_decode ( $blc_option , true );
}
if ( is_array ( $links ) ) {
foreach ( $links as $link ) {
2020-05-14 17:26:44 +07:00
$new_link = new \stdClass ();
2016-08-02 19:51:22 +02:00
foreach ( $filter_fields as $field ) {
$new_link -> $field = $link -> $field ;
2015-10-15 22:52:37 +10:00
}
2019-02-14 22:38:14 +07:00
2016-08-02 19:51:22 +02:00
$extra_info = array ();
2019-02-14 22:38:14 +07:00
if ( ! empty ( $link -> post_date ) ) {
2016-08-02 19:51:22 +02:00
$extra_info [ 'post_date' ] = $link -> post_date ;
2015-10-15 22:52:37 +10:00
}
2019-02-14 22:38:14 +07:00
2015-10-15 22:52:37 +10:00
$days_broken = 0 ;
if ( $link -> broken ) {
2020-04-17 14:14:10 +02:00
// Add a highlight to broken links that appear to be permanently broken.
2015-10-15 22:52:37 +10:00
$days_broken = intval ( ( time () - $link -> first_failure ) / ( 3600 * 24 ) );
if ( $days_broken >= $blc_option [ 'failure_duration_threshold' ] ) {
2019-02-14 22:38:14 +07:00
$extra_info [ 'permanently_broken' ] = 1 ;
if ( $blc_option [ 'highlight_permanent_failures' ] ) {
2016-08-02 19:51:22 +02:00
$extra_info [ 'permanently_broken_highlight' ] = 1 ;
2015-10-15 22:52:37 +10:00
}
}
2019-02-14 22:38:14 +07:00
}
2016-08-02 19:51:22 +02:00
$extra_info [ 'days_broken' ] = $days_broken ;
2020-03-26 19:45:07 +00:00
$instances = false ;
2015-10-15 22:52:37 +10:00
$get_link = new blcLink ( intval ( $link -> link_id ) );
if ( $get_link -> valid () ) {
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_methods ( $get_link , 'get_instances' );
2015-10-15 22:52:37 +10:00
$instances = $get_link -> get_instances ();
}
if ( ! empty ( $instances ) ) {
2020-03-27 15:13:11 +00:00
$first_instance = reset ( $instances );
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
MainWP_Helper :: check_methods ( $first_instance , array ( 'ui_get_link_text' , 'get_container' , 'is_link_text_editable' , 'is_url_editable' ) );
2019-02-14 22:38:14 +07:00
2020-03-26 19:45:07 +00:00
$new_link -> link_text = $first_instance -> ui_get_link_text ();
2016-08-02 19:51:22 +02:00
$extra_info [ 'count_instance' ] = count ( $instances );
2020-03-26 19:45:07 +00:00
$container = $first_instance -> get_container ();
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
if ( ! empty ( $container ) ) {
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 ) ) {
2020-03-27 15:13:11 +00:00
$extra_info [ 'container_type' ] = $container -> container_type ;
$extra_info [ 'container_id' ] = $container -> container_id ;
$extra_info [ 'source_data' ] = $this -> ui_get_source ( $container , $first_instance -> container_field );
}
}
2015-10-15 22:52:37 +10:00
}
$can_edit_text = false ;
$can_edit_url = false ;
$editable_link_texts = $non_editable_link_texts = array ();
foreach ( $instances as $instance ) {
if ( $instance -> is_link_text_editable () ) {
$can_edit_text = true ;
$editable_link_texts [ $instance -> link_text ] = true ;
} else {
$non_editable_link_texts [ $instance -> link_text ] = true ;
}
if ( $instance -> is_url_editable () ) {
$can_edit_url = true ;
}
}
$link_texts = $can_edit_text ? $editable_link_texts : $non_editable_link_texts ;
$data_link_text = '' ;
if ( count ( $link_texts ) === 1 ) {
2020-03-26 20:01:04 +00:00
// All instances have the same text - use it.
2015-10-15 22:52:37 +10:00
$link_text = key ( $link_texts );
$data_link_text = esc_attr ( $link_text );
}
2016-08-02 19:51:22 +02:00
$extra_info [ 'data_link_text' ] = $data_link_text ;
2020-03-26 19:45:07 +00:00
$extra_info [ 'can_edit_url' ] = $can_edit_url ;
$extra_info [ 'can_edit_text' ] = $can_edit_text ;
2019-02-14 22:38:14 +07:00
} else {
2020-03-26 19:45:07 +00:00
$new_link -> link_text = '' ;
2016-08-02 19:51:22 +02:00
$extra_info [ 'count_instance' ] = 0 ;
2015-10-15 22:52:37 +10:00
}
2020-05-08 16:44:48 +07:00
$new_link -> extra_info = base64_encode ( serialize ( $extra_info ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
2020-03-26 19:45:07 +00:00
$new_link -> synced = 1 ;
$return [] = $new_link ;
2015-10-15 22:52:37 +10:00
}
} else {
2018-04-17 20:33:10 +02:00
return array ();
2015-10-15 22:52:37 +10:00
}
return $return ;
}
2020-04-17 14:14:10 +02:00
public function edit_link () {
2015-10-15 22:52:37 +10:00
$information = array ();
if ( ! current_user_can ( 'edit_others_posts' ) ) {
$information [ 'error' ] = 'NOTALLOW' ;
return $information ;
}
2020-04-17 14:14:10 +02:00
// Load the link.
2015-10-15 22:52:37 +10:00
$link = new blcLink ( intval ( $_POST [ 'link_id' ] ) );
if ( ! $link -> valid () ) {
2020-04-17 14:14:10 +02:00
$information [ 'error' ] = 'NOTFOUNDLINK' ; // Oops, I can't find the link.
2015-10-15 22:52:37 +10:00
return $information ;
}
2020-03-26 20:01:04 +00:00
// Validate the new URL.
2015-10-15 22:52:37 +10:00
$new_url = stripslashes ( $_POST [ 'new_url' ] );
$parsed = @ parse_url ( $new_url );
if ( ! $parsed ) {
$information [ 'error' ] = 'URLINVALID' ; // Oops, the new URL is invalid!
return $information ;
}
$new_text = ( isset ( $_POST [ 'new_text' ] ) && is_string ( $_POST [ 'new_text' ] ) ) ? stripslashes ( $_POST [ 'new_text' ] ) : null ;
if ( '' === $new_text ) {
$new_text = null ;
}
if ( ! empty ( $new_text ) && ! current_user_can ( 'unfiltered_html' ) ) {
2020-03-26 20:01:04 +00:00
$new_text = stripslashes ( wp_filter_post_kses ( addslashes ( $new_text ) ) ); // wp_filter_post_kses expects slashed data.
2015-10-15 22:52:37 +10:00
}
$rez = $link -> edit ( $new_url , $new_text );
if ( false === $rez ) {
2016-10-24 20:33:37 +02:00
$information [ 'error' ] = __ ( 'An unexpected error occurred!' , 'mainwp-child' );
2015-10-15 22:52:37 +10:00
return $information ;
} else {
$new_link = $rez [ 'new_link' ];
/** @var blcLink $new_link */
$new_status = $new_link -> analyse_status ();
$ui_link_text = null ;
if ( isset ( $new_text ) ) {
$instances = $new_link -> get_instances ();
if ( ! empty ( $instances ) ) {
$first_instance = reset ( $instances );
$ui_link_text = $first_instance -> ui_get_link_text ();
}
}
$response = array (
'new_link_id' => $rez [ 'new_link_id' ],
'cnt_okay' => $rez [ 'cnt_okay' ],
'cnt_error' => $rez [ 'cnt_error' ],
'status_text' => $new_status [ 'text' ],
'status_code' => $new_status [ 'code' ],
'http_code' => empty ( $new_link -> http_code ) ? '' : $new_link -> http_code ,
'url' => $new_link -> url ,
'link_text' => isset ( $new_text ) ? $new_text : null ,
'ui_link_text' => isset ( $new_text ) ? $ui_link_text : null ,
'errors' => array (),
);
2020-04-17 14:14:10 +02:00
// url, status text, status code, link text, editable link text.
2015-10-15 22:52:37 +10:00
foreach ( $rez [ 'errors' ] as $error ) {
array_push ( $response [ 'errors' ], implode ( ', ' , $error -> get_error_messages () ) );
}
return $response ;
}
}
2020-04-17 14:14:10 +02:00
public function unlink () {
2015-10-15 22:52:37 +10:00
$information = array ();
if ( ! current_user_can ( 'edit_others_posts' ) ) {
$information [ 'error' ] = 'NOTALLOW' ;
return $information ;
}
if ( isset ( $_POST [ 'link_id' ] ) ) {
2020-04-17 14:14:10 +02:00
// Load the link.
2015-10-15 22:52:37 +10:00
$link = new blcLink ( intval ( $_POST [ 'link_id' ] ) );
if ( ! $link -> valid () ) {
2020-04-17 14:14:10 +02:00
$information [ 'error' ] = 'NOTFOUNDLINK' ; // Oops, I can't find the link.
2015-10-15 22:52:37 +10:00
return $information ;
}
2020-04-17 14:14:10 +02:00
// Try and unlink it.
2015-10-15 22:52:37 +10:00
$rez = $link -> unlink ();
if ( false === $rez ) {
$information [ 'error' ] = 'UNDEFINEDERROR' ; // An unexpected error occured!
return $information ;
} else {
$response = array (
'cnt_okay' => $rez [ 'cnt_okay' ],
'cnt_error' => $rez [ 'cnt_error' ],
'errors' => array (),
);
foreach ( $rez [ 'errors' ] as $error ) {
2020-05-07 01:03:56 +07:00
/** @var \WP_Error $error */
2015-10-15 22:52:37 +10:00
array_push ( $response [ 'errors' ], implode ( ', ' , $error -> get_error_messages () ) );
}
return $response ;
}
} else {
2016-10-24 20:33:37 +02:00
$information [ 'error' ] = __ ( 'Error: link_id is not specified.' , 'mainwp-child' );
2015-10-15 22:52:37 +10:00
return $information ;
}
}
private function set_link_dismissed () {
$information = array ();
$dismiss = $_POST [ 'dismiss' ];
if ( ! current_user_can ( 'edit_others_posts' ) ) {
$information [ 'error' ] = 'NOTALLOW' ;
return $information ;
}
if ( isset ( $_POST [ 'link_id' ] ) ) {
2020-04-17 14:14:10 +02:00
// Load the link.
2015-10-15 22:52:37 +10:00
$link = new blcLink ( intval ( $_POST [ 'link_id' ] ) );
if ( ! $link -> valid () ) {
2020-04-17 14:14:10 +02:00
$information [ 'error' ] = 'NOTFOUNDLINK' ; // Oops, I can't find the link.
2015-10-15 22:52:37 +10:00
return $information ;
}
$link -> dismissed = $dismiss ;
2020-04-17 14:14:10 +02:00
// Save the changes.
2015-10-15 22:52:37 +10:00
if ( $link -> save () ) {
$information = 'OK' ;
} else {
2020-04-17 14:14:10 +02:00
$information [ 'error' ] = 'COULDNOTMODIFY' ; // Oops, couldn't modify the link.
2015-10-15 22:52:37 +10:00
}
return $information ;
} else {
2016-10-24 20:33:37 +02:00
$information [ 'error' ] = __ ( 'Error: link_id not specified.' , 'mainwp-child' );
2015-10-15 22:52:37 +10:00
return $information ;
}
}
private function discard () {
$information = array ();
if ( ! current_user_can ( 'edit_others_posts' ) ) {
$information [ 'error' ] = 'NOTALLOW' ;
return $information ;
}
if ( isset ( $_POST [ 'link_id' ] ) ) {
2020-04-17 14:14:10 +02:00
// Load the link.
2015-10-15 22:52:37 +10:00
$link = new blcLink ( intval ( $_POST [ 'link_id' ] ) );
if ( ! $link -> valid () ) {
2020-04-17 14:14:10 +02:00
$information [ 'error' ] = 'NOTFOUNDLINK' ; // Oops, I can't find the link.
2015-10-15 22:52:37 +10:00
return $information ;
}
2020-04-17 14:14:10 +02:00
// Make it appear "not broken".
2015-10-15 22:52:37 +10:00
$link -> broken = false ;
$link -> false_positive = true ;
$link -> last_check_attempt = time ();
2016-10-24 20:33:37 +02:00
$link -> log = __ ( 'This link was manually marked as working by the user.' , 'mainwp-child' );
2015-10-15 22:52:37 +10:00
2020-04-17 14:14:10 +02:00
// Save the changes.
2015-10-15 22:52:37 +10:00
if ( $link -> save () ) {
$information [ 'status' ] = 'OK' ;
$information [ 'last_check_attempt' ] = $link -> last_check_attempt ;
} else {
2020-04-17 14:14:10 +02:00
$information [ 'error' ] = 'COULDNOTMODIFY' ; // Oops, couldn't modify the link.
2015-10-15 22:52:37 +10:00
}
} else {
2016-10-24 20:33:37 +02:00
$information [ 'error' ] = __ ( 'Error: link_id is not specified.' , 'mainwp-child' );
2015-10-15 22:52:37 +10:00
}
return $information ;
}
2020-04-17 14:14:10 +02:00
public function ui_get_source ( $container , $container_field = '' ) {
2015-10-15 22:52:37 +10:00
if ( 'comment' === $container -> container_type ) {
return $this -> ui_get_source_comment ( $container , $container_field );
2020-03-26 14:11:33 +00:00
} elseif ( $container instanceof blcAnyPostContainer ) {
2015-10-15 22:52:37 +10:00
return $this -> ui_get_source_post ( $container , $container_field );
}
return array ();
}
2020-04-17 14:14:10 +02:00
public function ui_get_source_comment ( $container , $container_field = '' ) {
2020-03-26 20:01:04 +00:00
// Display a comment icon.
2015-10-15 22:52:37 +10:00
if ( 'comment_author_url' === $container_field ) {
$image = 'font-awesome/font-awesome-user.png' ;
} else {
$image = 'font-awesome/font-awesome-comment-alt.png' ;
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:14:10 +02:00
if ( true !== MainWP_Helper :: check_methods ( $container , array ( 'get_wrapped_object' ), true ) ) {
2020-03-27 15:13:11 +00:00
return false ;
}
2019-02-14 22:38:14 +07:00
2020-03-27 15:13:11 +00:00
$comment = $container -> get_wrapped_object ();
2015-10-15 22:52:37 +10:00
2020-04-17 14:14:10 +02:00
// Display a small text sample from the comment.
2015-10-15 22:52:37 +10:00
$text_sample = strip_tags ( $comment -> comment_content );
$text_sample = blcUtility :: truncate ( $text_sample , 65 );
return array (
'image' => $image ,
'text_sample' => $text_sample ,
'comment_author' => esc_attr ( $comment -> comment_author ),
'comment_id' => esc_attr ( $comment -> comment_ID ),
'comment_status' => wp_get_comment_status ( $comment -> comment_ID ),
'container_post_title' => get_the_title ( $comment -> comment_post_ID ),
'container_post_status' => get_post_status ( $comment -> comment_post_ID ),
'container_post_ID' => $comment -> comment_post_ID ,
);
}
2020-04-17 14:14:10 +02:00
public function ui_get_source_post ( $container , $container_field = '' ) {
2015-10-15 22:52:37 +10:00
return array (
'post_title' => get_the_title ( $container -> container_id ),
2020-04-17 14:14:10 +02:00
'post_status' => get_post_status ( $container -> container_id ),
2015-10-15 22:52:37 +10:00
'container_anypost' => true ,
);
}
}