AnyBacklink screen, the dashboard widget and the check request. * * @package AnyBacklink */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Admin screens. */ class AnyBacklink_Admin { const SLUG = 'anybacklink'; const CAP = 'manage_options'; /* The checker takes up to this many pages per click. */ const MAX_PAGES = 10; /** * Hook everything up. */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_action( 'admin_post_anybacklink_scan', array( __CLASS__, 'handle_scan' ) ); add_action( 'wp_ajax_anybacklink_check', array( __CLASS__, 'ajax_check' ) ); add_action( 'wp_dashboard_setup', array( __CLASS__, 'dashboard' ) ); add_filter( 'plugin_action_links_' . plugin_basename( ANYBACKLINK_FILE ), array( __CLASS__, 'action_links' ) ); } /** * Tools > AnyBacklink. */ public static function menu() { add_management_page( __( 'AnyBacklink', 'anybacklink' ), __( 'AnyBacklink', 'anybacklink' ), self::CAP, self::SLUG, array( __CLASS__, 'render' ) ); } /** * Address of one tab. * * @param string $tab links, check or swap. * @return string */ public static function tab_url( $tab ) { return add_query_arg( array( 'page' => self::SLUG, 'tab' => $tab, ), admin_url( 'tools.php' ) ); } /** * "Check backlinks" next to Deactivate on the Plugins screen. * * @param string[] $links Existing links. * @return string[] */ public static function action_links( $links ) { array_unshift( $links, sprintf( '%s', esc_url( self::tab_url( 'check' ) ), esc_html__( 'Check backlinks', 'anybacklink' ) ) ); return $links; } /** * CSS and JS, only on this plugin's screen and the dashboard. * * @param string $hook Current screen. */ public static function assets( $hook ) { if ( 'tools_page_' . self::SLUG !== $hook && 'index.php' !== $hook ) { return; } $url = plugin_dir_url( ANYBACKLINK_FILE ); wp_enqueue_style( 'anybacklink-admin', $url . 'assets/admin.css', array(), ANYBACKLINK_VERSION ); if ( 'index.php' === $hook ) { return; } wp_enqueue_script( 'anybacklink-admin', $url . 'assets/admin.js', array(), ANYBACKLINK_VERSION, true ); wp_localize_script( 'anybacklink-admin', 'AnyBacklink', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'anybacklink_check' ), 'maxPages' => self::MAX_PAGES, 'text' => array( 'follow' => __( 'Dofollow', 'anybacklink' ), 'nofollow' => __( 'Nofollow', 'anybacklink' ), 'sponsored' => __( 'Sponsored', 'anybacklink' ), 'ugc' => __( 'UGC', 'anybacklink' ), 'missing' => __( 'Missing', 'anybacklink' ), 'gone' => __( 'Page deleted', 'anybacklink' ), 'error' => __( 'Could not check', 'anybacklink' ), 'checking' => __( 'Checking…', 'anybacklink' ), 'noindex' => __( 'Page is hidden from Google (noindex)', 'anybacklink' ), 'inContent' => __( 'In the main content', 'anybacklink' ), /* translators: %s: where on the page, e.g. footer */ 'inArea' => __( 'In the %s', 'anybacklink' ), 'anchor' => __( 'Anchor', 'anybacklink' ), 'noPages' => __( 'Add at least one page.', 'anybacklink' ), /* translators: %d: most pages per check */ 'tooMany' => __( 'Only the first %d pages will be checked.', 'anybacklink' ), 'notFound' => __( 'No link to your site was found on this page.', 'anybacklink' ), 'checkButton' => __( 'Check backlinks', 'anybacklink' ), ), ) ); } /** * Rescan button. */ public static function handle_scan() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'anybacklink' ) ); } check_admin_referer( 'anybacklink_scan' ); AnyBacklink_Scanner::run(); wp_safe_redirect( self::tab_url( 'links' ) ); exit; } /** * One page checked, called by admin.js for each line the user entered. */ public static function ajax_check() { check_ajax_referer( 'anybacklink_check', 'nonce' ); if ( ! current_user_can( self::CAP ) ) { wp_send_json_error( array( 'error' => __( 'You are not allowed to do that.', 'anybacklink' ) ), 403 ); } $page = isset( $_POST['page_url'] ) ? sanitize_text_field( wp_unslash( $_POST['page_url'] ) ) : ''; $site = isset( $_POST['site'] ) ? sanitize_text_field( wp_unslash( $_POST['site'] ) ) : ''; if ( '' === $page || '' === $site ) { wp_send_json_error( array( 'error' => __( 'Enter a page and your site.', 'anybacklink' ) ), 400 ); } wp_send_json_success( AnyBacklink_Checker::check( $page, $site ) ); } /** * Dashboard widget, for admins only. */ public static function dashboard() { if ( current_user_can( self::CAP ) ) { wp_add_dashboard_widget( 'anybacklink_widget', __( 'Backlinks', 'anybacklink' ), array( __CLASS__, 'render_widget' ) ); } } /** * Dashboard widget body. */ public static function render_widget() { $scan = AnyBacklink_Scanner::cached(); echo '
'; if ( $scan ) { echo '

'; printf( /* translators: 1: number of sites, 2: dofollow links, 3: nofollow, sponsored and UGC links */ esc_html__( 'Your content links out to %1$s sites: %2$s dofollow and %3$s nofollow, sponsored or UGC links.', 'anybacklink' ), '' . esc_html( number_format_i18n( $scan['sites'] ) ) . '', '' . esc_html( number_format_i18n( $scan['counts']['follow'] ) ) . '', '' . esc_html( number_format_i18n( $scan['total'] - $scan['counts']['follow'] ) ) . '' ); echo '

'; } else { echo '

' . esc_html__( 'See every link your site gives out, and check the links other sites give you.', 'anybacklink' ) . '

'; } echo '

'; printf( '%s ', esc_url( self::tab_url( 'check' ) ), esc_html__( 'Check your backlinks', 'anybacklink' ) ); printf( '%s', esc_url( self::tab_url( 'links' ) ), esc_html__( 'Outbound links', 'anybacklink' ) ); echo '

'; printf( '

%s

', esc_url( anybacklink_url( '/', 'dashboard-widget' ) ), esc_html__( 'Find relevant sites to swap links with, free', 'anybacklink' ) ); echo '
'; } /** * The Tools > AnyBacklink screen. */ public static function render() { if ( ! current_user_can( self::CAP ) ) { return; } // Reading which tab to show changes nothing, so no nonce is needed. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'links'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tabs = array( 'links' => __( 'Outbound links', 'anybacklink' ), 'check' => __( 'Check backlinks', 'anybacklink' ), 'swap' => __( 'Get backlinks', 'anybacklink' ), ); if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'links'; } ?>

AnyBacklink

0 ) : ?>

' . esc_html( number_format_i18n( $scan['follow_sites'] ) ) . '' ); ?>

__( 'All', 'anybacklink' ), 'follow' => __( 'Dofollow', 'anybacklink' ), 'nofollow' => __( 'Nofollow', 'anybacklink' ), 'sponsored' => __( 'Sponsored', 'anybacklink' ), 'ugc' => __( 'UGC', 'anybacklink' ), ); ?>

%s', esc_url( $edit ), esc_html( $name ) ); } else { echo esc_html( $name ); } ?>
500 ) : ?>

' . esc_html__( 'Terms', 'anybacklink' ) . '', '' . esc_html__( 'Privacy', 'anybacklink' ) . '' ); ?>

' . esc_html__( 'bulk backlink checker', 'anybacklink' ) . '' ); ?>

%s%s', esc_attr( $type ? 'abl-card abl-card-' . $type : 'abl-card' ), esc_html( number_format_i18n( (int) $value ) ), esc_html( $label ) ); } /** * Coloured link type label. * * @param string $type follow, nofollow, sponsored or ugc. */ private static function badge( $type ) { $labels = array( 'follow' => __( 'Dofollow', 'anybacklink' ), 'nofollow' => __( 'Nofollow', 'anybacklink' ), 'sponsored' => __( 'Sponsored', 'anybacklink' ), 'ugc' => __( 'UGC', 'anybacklink' ), ); printf( '%s', esc_attr( $type ), esc_html( isset( $labels[ $type ] ) ? $labels[ $type ] : $type ) ); } /** * The AnyBacklink swap mark: two arrows passing a link each way. */ private static function mark() { echo ''; } }