true, 'src' => true, 'crossorigin' => true, 'type' => true, 'data-ad-client' => true, ); $allowed_html['ins'] = array( 'class' => true, 'style' => true, 'data-ad-client' => true, 'data-ad-slot' => true, 'data-ad-format' => true, 'data-full-width-responsive' => true, 'data-ad-layout' => true, 'data-ad-layout-key' => true, 'data-ad-channel' => true, ); return $allowed_html; } function adslastica_get_ads_txt_line() { $publisher_id = adslastica_normalize_publisher_id( get_option( 'adslastica_publisher_id', '' ) ); return ( '' === $publisher_id ) ? '' : 'google.com, ' . substr( $publisher_id, 3 ) . ', DIRECT, f08c47fec0942fa0'; } function adslastica_get_ads_txt_url() { return home_url( '/ads.txt' ); } function adslastica_get_physical_ads_txt_path() { return trailingslashit( ABSPATH ) . 'ads.txt'; } function adslastica_maybe_serve_ads_txt() { $line = adslastica_get_ads_txt_line(); if ( '' === $line || file_exists( adslastica_get_physical_ads_txt_path() ) ) return; $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $request_path = wp_parse_url( $request_uri, PHP_URL_PATH ); $ads_txt_path = wp_parse_url( adslastica_get_ads_txt_url(), PHP_URL_PATH ); if ( untrailingslashit( (string) $request_path ) !== untrailingslashit( (string) $ads_txt_path ) ) return; status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); nocache_headers(); echo esc_html( $line ); exit; } function adslastica_get_ads_txt_status() { $line = adslastica_get_ads_txt_line(); $url = adslastica_get_ads_txt_url(); if ( '' === $line ) { return array( 'status' => '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 ); 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.', 'line' => $line, 'url' => $url ); } $response = wp_remote_get( add_query_arg( 'adslastica_check', time(), $url ), array( 'timeout' => 5, 'headers' => array( 'Cache-Control' => 'no-cache' ) ) ); if ( ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ) ) { if ( false !== strpos( wp_remote_retrieve_body( $response ), $line ) ) { return array( 'status' => 'active', 'message' => 'ads.txt is active.', 'line' => $line, 'url' => $url ); } } return array( 'status' => 'unavailable', 'message' => 'ads.txt is not available.', '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_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; } /* AJAX Handlers */ function adslastica_ajax_save_ad_1() { check_ajax_referer( 'adslastica_save_ad_1_action', 'adslastica_ad_1_nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Unauthorized' ); $ad_code = isset( $_POST['adslastica_manual_ad_1'] ) ? wp_kses( wp_unslash( $_POST['adslastica_manual_ad_1'] ), adslastica_get_allowed_ad_html() ) : ''; 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_send_json_success( 'Ad 1 settings updated.' ); } function adslastica_ajax_save_ad_2() { check_ajax_referer( 'adslastica_save_ad_2_action', 'adslastica_ad_2_nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Unauthorized' ); $ad_code = isset( $_POST['adslastica_manual_ad_2'] ) ? wp_kses( wp_unslash( $_POST['adslastica_manual_ad_2'] ), adslastica_get_allowed_ad_html() ) : ''; 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_send_json_success( 'Ad 2 settings updated.' ); } function adslastica_ajax_save_post_ads() { check_ajax_referer( 'adslastica_save_post_ads_action', 'adslastica_post_ads_nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Unauthorized' ); for ( $i = 1; $i <= 3; $i++ ) { $key = 'adslastica_post_ad_' . $i; $code = isset( $_POST[ $key ] ) ? wp_kses( wp_unslash( $_POST[ $key ] ), adslastica_get_allowed_ad_html() ) : ''; update_option( $key, $code ); } $placement_keys = array( 'before_post', 'after_paragraph_2', 'middle_post', 'before_last_paragraph', 'after_post', ); $used_ads = array(); foreach ( $placement_keys as $placement_key ) { $field = 'adslastica_post_placement_' . $placement_key; $value = isset( $_POST[ $field ] ) ? absint( $_POST[ $field ] ) : 0; if ( $value < 1 || $value > 3 || in_array( $value, $used_ads, true ) ) { $value = 0; } else { $used_ads[] = $value; } update_option( $field, $value ); } wp_send_json_success( 'Post Ads settings updated.' ); } 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; } .adslastica-collapsible { margin-top: 18px; max-width: 860px; } .adslastica-collapsible > summary { cursor: pointer; list-style: none; background: #ffffff; padding: 16px 20px; border: 1px solid #dcdcde; border-left: 4px solid #2271b1; border-radius: 8px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); font-size: 16px; font-weight: 600; color: #1d2327; } .adslastica-collapsible > summary::-webkit-details-marker { display: none; } .adslastica-collapsible > summary::after { content: "\25BC"; float: right; font-size: 12px; margin-top: 3px; } .adslastica-collapsible[open] > summary::after { content: "\25B2"; } .adslastica-collapsible .adslastica-card { margin-top: 12px; } .adslastica-post-ad-tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 12px; } .adslastica-post-ad-tab { width: 100%; min-height: 40px; font-weight: 600; } .adslastica-post-ad-tab.is-active { background: #2271b1; border-color: #2271b1; color: #fff; } .adslastica-post-ad-panel { display: none; padding: 16px; background: #f3f7fb; border: 1px solid #d6e2ee; border-radius: 6px; } .adslastica-post-ad-panel.is-active { display: block; } .adslastica-post-ad-panel textarea { height: 130px; min-height: 130px; background: #fff; } @media (max-width: 600px) { .adslastica-post-ad-tabs { gap: 6px; } } .adslastica-placement-table { width: 100%; max-width: 620px; border-collapse: collapse; margin-top: 8px; } .adslastica-placement-table th, .adslastica-placement-table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid #dcdcde; font-size: 15px; } .adslastica-placement-table th { padding-top: 0; } .adslastica-placement-table select { min-width: 130px; } .adslastica-help { color: #50575e; margin-top: 8px; font-size: 14px; line-height: 1.5; } /* Styled Green AJAX Notification */ .adslastica-ajax-notice { display: none; background: #edfaef; border: 1px solid #00a32a; border-left: 4px solid #00a32a; color: #007017; padding: 10px 15px; margin: 12px 0 0; border-radius: 4px; font-weight: 600; font-size: 14px; } ' ); } function adslastica_render_settings_page() { $ads_txt_status = adslastica_get_ads_txt_status(); $message_code = sanitize_key( (string) filter_input( INPUT_GET, 'adslastica_message', FILTER_UNSAFE_RAW ) ); ?>

Adslastica Settings

Publisher ID settings updated.

Enter a valid Publisher ID, such as ca-pub-1234567890123456.

Publisher ID

✓ Publisher ID is saved.

✓ View ads.txt

✗

Manual Ads
✓ Settings updated successfully.

Ad 1

Ad 2

Post Ads
✓ Post Ads settings updated.

Placement

Choose where each saved post ad appears. Each ad can be used once, for a maximum of three automatic ads per post.

'Before post', 'after_paragraph_2' => 'After paragraph 2', 'middle_post' => 'Middle of post', 'before_last_paragraph' => 'Before last paragraph', 'after_post' => 'After post', ); foreach ( $placements as $placement_key => $placement_label ) : $saved_value = absint( get_option( 'adslastica_post_placement_' . $placement_key, 0 ) ); ?>
LocationShow

3 ) return ''; $code = (string) get_option( 'adslastica_post_ad_' . $ad_number, '' ); if ( '' === trim( $code ) ) return ''; return '
' . $code . '
'; } function adslastica_insert_after_paragraph_index( $content, $insertions ) { if ( empty( $insertions ) ) return $content; $parts = preg_split( '/(<\/p>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE ); if ( ! is_array( $parts ) || count( $parts ) < 2 ) return $content; $paragraph_number = 0; $output = ''; foreach ( $parts as $part ) { $output .= $part; if ( preg_match( '/^<\/p>$/i', $part ) ) { $paragraph_number++; if ( isset( $insertions[ $paragraph_number ] ) ) { $output .= implode( '', $insertions[ $paragraph_number ] ); } } } return $output; } function adslastica_insert_post_ads( $content ) { if ( is_admin() || is_feed() || ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) return $content; $locations = array( 'before_post' => absint( get_option( 'adslastica_post_placement_before_post', 0 ) ), 'after_paragraph_2' => absint( get_option( 'adslastica_post_placement_after_paragraph_2', 0 ) ), 'middle_post' => absint( get_option( 'adslastica_post_placement_middle_post', 0 ) ), 'before_last_paragraph' => absint( get_option( 'adslastica_post_placement_before_last_paragraph', 0 ) ), 'after_post' => absint( get_option( 'adslastica_post_placement_after_post', 0 ) ), ); $used_ads = array(); foreach ( $locations as $key => $ad_number ) { if ( $ad_number < 1 || $ad_number > 3 || in_array( $ad_number, $used_ads, true ) ) { $locations[ $key ] = 0; } else { $used_ads[] = $ad_number; } } $before = adslastica_get_post_ad_html( $locations['before_post'] ); $after = adslastica_get_post_ad_html( $locations['after_post'] ); $paragraph_count = preg_match_all( '/<\/p>/i', $content, $matches ); $insertions = array(); if ( $paragraph_count > 0 ) { if ( $locations['after_paragraph_2'] ) { $index = min( 2, $paragraph_count ); $insertions[ $index ][] = adslastica_get_post_ad_html( $locations['after_paragraph_2'] ); } if ( $locations['middle_post'] ) { $index = max( 1, (int) ceil( $paragraph_count / 2 ) ); $insertions[ $index ][] = adslastica_get_post_ad_html( $locations['middle_post'] ); } if ( $locations['before_last_paragraph'] ) { $index = max( 0, $paragraph_count - 1 ); if ( 0 === $index ) { $before .= adslastica_get_post_ad_html( $locations['before_last_paragraph'] ); } else { $insertions[ $index ][] = adslastica_get_post_ad_html( $locations['before_last_paragraph'] ); } } } $content = adslastica_insert_after_paragraph_index( $content, $insertions ); return $before . $content . $after; } function adslastica_shortcode_manual_ad_1() { if ( ! get_option( 'adslastica_manual_ad_1_enabled' ) ) return ''; return '
' . 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', '' ) . '
'; }