'missing_id',
'message' => 'Publisher ID is missing or invalid.',
'line' => '',
'url' => $url,
);
}
$physical_path = adslastica_get_physical_ads_txt_path();
if ( file_exists( $physical_path ) ) {
$physical_contents = file_get_contents( $physical_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( false !== $physical_contents && false !== strpos( $physical_contents, $line ) ) {
return array(
'status' => 'active',
'message' => 'ads.txt is active.',
'line' => $line,
'url' => $url,
);
}
return array(
'status' => 'physical_conflict',
'message' => 'An existing ads.txt file was found. Add the Google line to it manually.',
'line' => $line,
'url' => $url,
);
}
$response = wp_remote_get(
add_query_arg( 'adslastica_check', time(), $url ),
array(
'timeout' => 5,
'redirection' => 3,
'headers' => array( 'Cache-Control' => 'no-cache' ),
)
);
if ( ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ) ) {
$body = wp_remote_retrieve_body( $response );
if ( false !== strpos( $body, $line ) ) {
return array(
'status' => 'active',
'message' => 'ads.txt is active.',
'line' => $line,
'url' => $url,
);
}
}
return array(
'status' => 'unavailable',
'message' => 'ads.txt is not available. Add the Google line manually in your hosting File Manager.',
'line' => $line,
'url' => $url,
);
}
function adslastica_add_admin_menu() {
add_menu_page( 'Adslastica', 'Adslastica', 'manage_options', 'adslastica', 'adslastica_render_settings_page', 'dashicons-megaphone', 65 );
}
function adslastica_display_admin_notices() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
if ( 'adslastica' !== $current_page ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['adslastica_message'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$message_code = sanitize_key( wp_unslash( $_GET['adslastica_message'] ) );
$text = '';
$type = 'success';
switch ( $message_code ) {
case 'publisher_saved':
$text = 'Publisher ID saved successfully.';
break;
case 'publisher_removed':
$text = 'Publisher ID removed.';
break;
case 'publisher_invalid':
$text = 'Enter a valid Publisher ID, such as ca-pub-1234567890123456.';
$type = 'error';
break;
case 'ad_1_saved':
$text = 'Ad 1 settings updated.';
break;
case 'ad_2_saved':
$text = 'Ad 2 settings updated.';
break;
}
if ( $text ) {
echo '
' . esc_html( $text ) . '
';
}
}
}
function adslastica_save_publisher_settings() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'Unauthorized' );
}
check_admin_referer( 'adslastica_save_publisher_action', 'adslastica_publisher_nonce' );
$publisher_input = isset( $_POST['adslastica_publisher_id'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['adslastica_publisher_id'] ) ) ) : '';
if ( '' === $publisher_input ) {
update_option( 'adslastica_publisher_id', '' );
wp_safe_redirect( add_query_arg( array( 'page' => 'adslastica', 'adslastica_message' => 'publisher_removed' ), admin_url( 'admin.php' ) ) );
exit;
}
$publisher_id = adslastica_normalize_publisher_id( $publisher_input );
if ( '' === $publisher_id ) {
wp_safe_redirect( add_query_arg( array( 'page' => 'adslastica', 'adslastica_message' => 'publisher_invalid' ), admin_url( 'admin.php' ) ) );
exit;
}
update_option( 'adslastica_publisher_id', $publisher_id );
wp_safe_redirect( add_query_arg( array( 'page' => 'adslastica', 'adslastica_message' => 'publisher_saved' ), admin_url( 'admin.php' ) ) );
exit;
}
function adslastica_save_ad_1_settings() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'Unauthorized' );
}
check_admin_referer( 'adslastica_save_ad_1_action', 'adslastica_ad_1_nonce' );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$ad_code = isset( $_POST['adslastica_manual_ad_1'] ) ? wp_unslash( $_POST['adslastica_manual_ad_1'] ) : '';
update_option( 'adslastica_manual_ad_1', $ad_code );
update_option( 'adslastica_manual_ad_1_enabled', isset( $_POST['adslastica_manual_ad_1_enabled'] ) ? 1 : 0 );
wp_safe_redirect( add_query_arg( array( 'page' => 'adslastica', 'adslastica_message' => 'ad_1_saved' ), admin_url( 'admin.php' ) ) );
exit;
}
function adslastica_save_ad_2_settings() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'Unauthorized' );
}
check_admin_referer( 'adslastica_save_ad_2_action', 'adslastica_ad_2_nonce' );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$ad_code = isset( $_POST['adslastica_manual_ad_2'] ) ? wp_unslash( $_POST['adslastica_manual_ad_2'] ) : '';
update_option( 'adslastica_manual_ad_2', $ad_code );
update_option( 'adslastica_manual_ad_2_enabled', isset( $_POST['adslastica_manual_ad_2_enabled'] ) ? 1 : 0 );
wp_safe_redirect( add_query_arg( array( 'page' => 'adslastica', 'adslastica_message' => 'ad_2_saved' ), admin_url( 'admin.php' ) ) );
exit;
}
function adslastica_enqueue_adsense_header() {
$publisher_id = adslastica_normalize_publisher_id( get_option( 'adslastica_publisher_id', '' ) );
if ( '' === $publisher_id ) {
return;
}
wp_enqueue_script(
'adslastica-adsense',
'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' . rawurlencode( $publisher_id ),
array(),
ADSLASTICA_VERSION,
array( 'strategy' => 'async' )
);
}
function adslastica_admin_styles( $hook ) {
if ( 'toplevel_page_adslastica' !== $hook ) {
return;
}
wp_add_inline_style(
'common',
'
.adslastica-wrap {
max-width: 900px;
}
.adslastica-card {
background: #ffffff;
padding: 20px;
border: 1px solid #dcdcde;
border-left: 4px solid #2271b1;
margin-top: 18px;
border-radius: 8px;
max-width: 860px;
box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.adslastica-card-ad-1 {
border-left-color: #2271b1;
}
.adslastica-card-ad-2 {
border-left-color: #00a32a;
}
.adslastica-shortcode-row {
display: flex;
align-items: center;
gap: 8px;
margin: 8px 0 14px;
}
.adslastica-shortcode-box {
width: 220px !important;
max-width: 220px !important;
padding: 8px 10px;
font-size: 16px;
font-family: Consolas, Monaco, monospace;
background: #f6f7f7;
border: 1px solid #c3c4c7;
border-radius: 4px;
margin: 0 !important;
display: block;
box-sizing: border-box;
}
.adslastica-copy-shortcode {
min-width: 76px;
height: 36px;
}
.adslastica-card h3 {
margin-top: 0;
color: #1d2327;
border-bottom: 1px solid #f0f0f1;
padding-bottom: 12px;
line-height: 1.4;
}
.adslastica-card input[type="text"] {
width: 100%;
max-width: 420px;
padding: 8px;
margin-bottom: 10px;
}
.adslastica-card textarea {
width: 100%;
max-width: 820px;
height: 180px;
min-height: 180px;
font-family: Consolas, Monaco, monospace;
font-size: 15px;
line-height: 1.6;
padding: 12px;
background: #f6f7f7;
border: 1px solid #c3c4c7;
border-radius: 4px;
display: block;
box-sizing: border-box;
}
.adslastica-card p.submit {
margin-bottom: 0;
padding-bottom: 0;
}
.adslastica-status {
margin: 4px 0 0;
font-size: 14px;
line-height: 1.7;
}
.adslastica-status-good {
color: #008a20;
}
.adslastica-status-error {
color: #b32d2e;
}
.adslastica-manual-line {
max-width: 760px;
margin-top: 12px;
padding: 12px;
background: #f6f7f7;
border: 1px solid #dcdcde;
border-radius: 4px;
}
.adslastica-manual-line code {
display: block;
margin-bottom: 10px;
white-space: normal;
word-break: break-word;
}
'
);
}
function adslastica_render_settings_page() {
$ads_txt_status = adslastica_get_ads_txt_status();
?>
Adslastica Settings
Publisher ID
✓ Publisher ID is saved.
✓ View ads.txt
✗
' . get_option( 'adslastica_manual_ad_1', '' ) . '';
}
function adslastica_shortcode_manual_ad_2() {
if ( ! get_option( 'adslastica_manual_ad_2_enabled' ) ) {
return '';
}
return '' . get_option( 'adslastica_manual_ad_2', '' ) . '
';
}