Skip to content

Commit

Permalink
Merge branch 'develop' into fix/78-placeholder-record
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Mar 1, 2024
2 parents b14c1cf + 43316b2 commit c314ad9
Show file tree
Hide file tree
Showing 12 changed files with 1,199 additions and 3,318 deletions.
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# These owners will be the default owners for everything in the repo. Unless a later match takes precedence, @peterwilsoncc, as primary maintainer will be requested for review when someone opens a Pull Request.
* @peterwilsoncc
# These owners will be the default owners for everything in the repo. Unless a later match takes precedence, @jeffpaul and @dkotter, as primary maintainers will be requested for review when someone opens a Pull Request.
* @jeffpaul @dkotter

# GitHub and WordPress.org specifics
/.github/ @jeffpaul
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.11.0
36 changes: 36 additions & 0 deletions .wordpress-org/blueprints/blueprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
"landingPage": "\/wp-admin\/options-general.php?page=adstxt-settings",
"preferredVersions": {
"php": "7.4",
"wp": "latest"
},
"phpExtensionBundles": ["kitchen-sink"],
"steps": [
{
"step": "login",
"username": "admin",
"password": "password"
},
{
"step": "setSiteOptions",
"options": {
"permalink_structure": "\/%25postname%25\/"
}
},
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "wordpress.org\/plugins",
"slug": "ads-txt"
},
"options": {
"activate": true
}
},
{
"step": "runPHP",
"code": "<?php require_once 'wordpress\/wp-load.php'; $adstxt_id = wp_insert_post( array( 'post_title' => 'Ads.txt', 'post_content' => '# Example information\n[email protected]\n\n# Example record\ngoogle.com, pub-1234567890, DIRECT, f08c47fec0942fa0', 'post_status' => 'publish', 'post_type' => 'adstxt' ) ); $app_adstxt_id = wp_insert_post( array( 'post_title' => 'App-ads.txt', 'post_content' => '# Example information\n[email protected]\n\n# Example record\ngoogle.com, pub-1234567890, DIRECT, f08c47fec0942fa0', 'post_status' => 'publish', 'post_type' => 'app-adstxt' ) ); if ( ! $adstxt_id instanceof WP_Error ) { update_option( 'adstxt_post', (int) $adstxt_id ); } if ( ! $app_adstxt_id instanceof WP_Error ) { update_option( 'app_adstxt_post', (int) $app_adstxt_id ); }"
}
]
}
139 changes: 33 additions & 106 deletions ads-txt.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,121 +22,48 @@
define( 'ADS_TXT_MANAGER_POST_OPTION', 'adstxt_post' );
define( 'APP_ADS_TXT_MANAGER_POST_OPTION', 'app_adstxt_post' );

require_once __DIR__ . '/inc/post-type.php';
require_once __DIR__ . '/inc/admin.php';
require_once __DIR__ . '/inc/save.php';

/**
* Display the contents of /ads.txt when requested.
* Get the minimum version of PHP required by this plugin.
*
* @return void
* @return string Minimum version required.
*/
function tenup_display_ads_txt() {
$request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false;
if ( '/ads.txt' === $request || '/ads.txt?' === substr( $request, 0, 9 ) ) {
$post_id = get_option( ADS_TXT_MANAGER_POST_OPTION );

// Set custom header for ads-txt
header( 'X-Ads-Txt-Generator: https://wordpress.org/plugins/ads-txt/' );

// Will fall through if no option found, likely to a 404.
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );

if ( ! $post instanceof WP_Post ) {
return;
}

header( 'Content-Type: text/plain' );
$adstxt = $post->post_content;

/**
* Filter the ads.txt content.
*
* @since 1.2.0
*
* @param type $adstxt The existing ads.txt content.
*/
echo esc_html( apply_filters( 'ads_txt_content', $adstxt ) );
die();
}
} elseif ( '/app-ads.txt' === $request || '/app-ads.txt?' === substr( $request, 0, 13 ) ) {
$post_id = get_option( APP_ADS_TXT_MANAGER_POST_OPTION );

// Set custom header for ads-txt
header( 'X-Ads-Txt-Generator: https://wordpress.org/plugins/ads-txt/' );

// Will fall through if no option found, likely to a 404.
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );

if ( ! $post instanceof WP_Post ) {
return;
}

header( 'Content-Type: text/plain' );
$adstxt = $post->post_content;

/**
* Filter the app-ads.txt content.
*
* @since 1.3.0
*
* @param type $app_adstxt The existing ads.txt content.
*/
echo esc_html( apply_filters( 'app_ads_txt_content', $adstxt ) );
die();
}
}
function adstxt_minimum_php_requirement() {
return '7.4';
}
add_action( 'init', 'tenup_display_ads_txt' );

/**
* Add custom capabilities.
* Whether PHP installation meets the minimum requirements
*
* @return void
* @return bool True if meets minimum requirements, false otherwise.
*/
function add_adstxt_capabilities() {
$role = get_role( 'administrator' );

// Bail early if the administrator role doesn't exist.
if ( null === $role ) {
return;
}

if ( ! $role->has_cap( ADS_TXT_MANAGE_CAPABILITY ) ) {
$role->add_cap( ADS_TXT_MANAGE_CAPABILITY );
}
function adstxt_site_meets_php_requirements() {
return version_compare( phpversion(), adstxt_minimum_php_requirement(), '>=' );
}
add_action( 'admin_init', 'add_adstxt_capabilities' );
register_activation_hook( __FILE__, 'add_adstxt_capabilities' );

/**
* Remove custom capabilities when deactivating the plugin.
*
* @return void
*/
function remove_adstxt_capabilities() {
$role = get_role( 'administrator' );

// Bail early if the administrator role doesn't exist.
if ( null === $role ) {
return;
}

$role->remove_cap( ADS_TXT_MANAGE_CAPABILITY );
// Ensuring our PHP version requirement is met first before loading plugin.
if ( ! adstxt_site_meets_php_requirements() ) {
add_action(
'admin_notices',
function() {
?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: Minimum required PHP version */
esc_html__( 'Ads.txt requires PHP version %s or later. Please upgrade PHP or disable the plugin.', 'ads-txt' ),
esc_html( adstxt_minimum_php_requirement() )
);
?>
</p>
</div>
<?php
}
);
return;
}
register_deactivation_hook( __FILE__, 'remove_adstxt_capabilities' );

/**
* Add a query var to detect when ads.txt has been saved.
*
* @param array $qvars Array of query vars.
*
* @return array Array of query vars.
*/
function tenup_ads_txt_add_query_vars( $qvars ) {
$qvars[] = 'ads_txt_saved';
return $qvars;
}
add_filter( 'query_vars', 'tenup_ads_txt_add_query_vars' );
require_once __DIR__ . '/inc/helpers.php';
require_once __DIR__ . '/inc/post-type.php';
require_once __DIR__ . '/inc/admin.php';
require_once __DIR__ . '/inc/save.php';
123 changes: 123 additions & 0 deletions inc/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Helper functions for Ads.txt.
*
* @package Ads_Txt_Manager
*/

namespace AdsTxt;

/**
* Display the contents of /ads.txt when requested.
*
* @return void
*/
function display_ads_txt() {
$request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false;
if ( '/ads.txt' === $request || '/ads.txt?' === substr( $request, 0, 9 ) ) {
$post_id = get_option( ADS_TXT_MANAGER_POST_OPTION );

// Set custom header for ads-txt
header( 'X-Ads-Txt-Generator: https://wordpress.org/plugins/ads-txt/' );

// Will fall through if no option found, likely to a 404.
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );

if ( ! $post instanceof \WP_Post ) {
return;
}

header( 'Content-Type: text/plain' );
$adstxt = $post->post_content;

/**
* Filter the ads.txt content.
*
* @since 1.2.0
*
* @param type $adstxt The existing ads.txt content.
*/
echo esc_html( apply_filters( 'ads_txt_content', $adstxt ) );
die();
}
} elseif ( '/app-ads.txt' === $request || '/app-ads.txt?' === substr( $request, 0, 13 ) ) {
$post_id = get_option( APP_ADS_TXT_MANAGER_POST_OPTION );

// Set custom header for ads-txt
header( 'X-Ads-Txt-Generator: https://wordpress.org/plugins/ads-txt/' );

// Will fall through if no option found, likely to a 404.
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );

if ( ! $post instanceof \WP_Post ) {
return;
}

header( 'Content-Type: text/plain' );
$adstxt = $post->post_content;

/**
* Filter the app-ads.txt content.
*
* @since 1.3.0
*
* @param type $app_adstxt The existing ads.txt content.
*/
echo esc_html( apply_filters( 'app_ads_txt_content', $adstxt ) );
die();
}
}
}
add_action( 'init', __NAMESPACE__ . '\display_ads_txt' );

/**
* Add custom capabilities.
*
* @return void
*/
function add_capabilities() {
$role = get_role( 'administrator' );

// Bail early if the administrator role doesn't exist.
if ( null === $role ) {
return;
}

if ( ! $role->has_cap( ADS_TXT_MANAGE_CAPABILITY ) ) {
$role->add_cap( ADS_TXT_MANAGE_CAPABILITY );
}
}
add_action( 'admin_init', __NAMESPACE__ . '\add_capabilities' );
register_activation_hook( __FILE__, __NAMESPACE__ . '\add_capabilities' );

/**
* Remove custom capabilities when deactivating the plugin.
*
* @return void
*/
function remove_capabilities() {
$role = get_role( 'administrator' );

// Bail early if the administrator role doesn't exist.
if ( null === $role ) {
return;
}

$role->remove_cap( ADS_TXT_MANAGE_CAPABILITY );
}
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\remove_capabilities' );

/**
* Add a query var to detect when ads.txt has been saved.
*
* @param array $qvars Array of query vars.
*
* @return array Array of query vars.
*/
function add_query_vars( $qvars ) {
$qvars[] = 'ads_txt_saved';
return $qvars;
}
add_filter( 'query_vars', __NAMESPACE__ . '\add_query_vars' );
Loading

0 comments on commit c314ad9

Please sign in to comment.