false,
'site' => '',
'skip_editors' => true,
)
);
}
/**
* "https://www.Example.com/blog" -> "example.com", the way ReaderCount names websites.
*
* @param string $value An address or a bare domain.
* @return string The domain, or '' when it isn't one.
*/
function readercount_site_name( $value ) {
$value = trim( (string) $value );
if ( '' === $value ) {
return '';
}
if ( false === strpos( $value, '://' ) ) {
$value = 'https://' . $value;
}
$host = wp_parse_url( $value, PHP_URL_HOST );
if ( ! is_string( $host ) || '' === $host ) {
return '';
}
return preg_replace( '/^www\./', '', strtolower( $host ) );
}
/**
* The website name ReaderCount counts this site as: the one set in Settings, otherwise this site's own address.
*
* @return string
*/
function readercount_site() {
$settings = readercount_settings();
$chosen = readercount_site_name( $settings['site'] );
return '' !== $chosen ? $chosen : readercount_site_name( home_url() );
}
/**
* Whether this page view should be counted. Editors and admins reading their own site are left out by default.
*
* @return bool
*/
function readercount_should_count() {
$settings = readercount_settings();
if ( ! $settings['enabled'] || '' === readercount_site() ) {
return false;
}
if ( is_preview() || is_customize_preview() ) {
return false;
}
if ( $settings['skip_editors'] && current_user_can( 'edit_posts' ) ) {
return false;
}
/**
* Filters whether ReaderCount counts the current page view.
*
* @param bool $count True to add ReaderCount's script to this page.
*/
return (bool) apply_filters( 'readercount_should_count', true );
}
/**
* Adds ReaderCount's script to public pages.
*/
function readercount_enqueue() {
if ( ! readercount_should_count() ) {
return;
}
/**
* Filters the address of ReaderCount's script, for sites that serve it from their own address.
*
* @param string $src The script's address.
*/
$src = apply_filters( 'readercount_script_url', READERCOUNT_SCRIPT );
// No version on the address: ReaderCount's script updates itself, and ?ver= would only split its cache.
wp_enqueue_script(
'readercount',
esc_url_raw( $src ),
array(),
null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
array(
'strategy' => 'defer',
'in_footer' => false,
)
);
}
add_action( 'wp_enqueue_scripts', 'readercount_enqueue' );
/**
* Tells the script which website it is counting.
*
* @param string $tag The script tag WordPress is about to print.
* @param string $handle The script's handle.
* @return string
*/
function readercount_script_tag( $tag, $handle ) {
if ( 'readercount' !== $handle ) {
return $tag;
}
return str_replace( ' src=', ' data-site="' . esc_attr( readercount_site() ) . '" src=', $tag );
}
add_filter( 'script_loader_tag', 'readercount_script_tag', 10, 2 );
/**
* Marks WordPress's "not found" page, so ReaderCount can list missing pages people are trying to reach.
*/
function readercount_not_found_meta() {
if ( is_404() && readercount_should_count() ) {
echo '' . "\n";
}
}
add_action( 'wp_head', 'readercount_not_found_meta', 1 );
/**
* A Settings link on the Plugins screen.
*
* @param string[] $links The plugin's action links.
* @return string[]
*/
function readercount_action_links( $links ) {
array_unshift( $links, '' . esc_html__( 'Settings', 'readercount' ) . '' );
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'readercount_action_links' );