*/
class AdstxtManager_Admin
{
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
private $options;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct($plugin_name, $version)
{
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->options = array();
}
public function display_notice()
{
global $hook_suffix, $pagenow;
$adstxtmanager_status = get_option('adstxtmanager_status');
// Initialize as array if it doesn't exist
if (false === $adstxtmanager_status) {
$adstxtmanager_status = array();
}
$adstxtmanager_id_value = AdstxtManager::get_adstxtmanager_id_value();
if ($adstxtmanager_id_value <= 0) {
// delete status option
delete_option('adstxtmanager_status');
} else if (in_array($pagenow, array('options-general.php')) && ($_GET['page'] == 'adstxtmanager-setting-admin')) {
if ((isset($_GET['verify']) && $_GET['verify']) || false === $adstxtmanager_status) {
$solutionFactory = new AdsTxtManager_Solution_Factory();
$adsTxtSolution = $solutionFactory->GetBestSolution();
$adsTxtSolution->SetupSolution();
$redirect_status = $this->verify_adstxt_redirect();
$adstxtmanager_status = get_option('adstxtmanager_status');
// Initialize as array if it doesn't exist
if (false === $adstxtmanager_status) {
$adstxtmanager_status = array();
}
$adstxtmanager_status['status'] = $redirect_status;
update_option('adstxtmanager_status', $adstxtmanager_status);
}
if (get_option('adstxtmanager_id') > 0 && false !== $adstxtmanager_status) {
// Initialize status key if it doesn't exist to prevent "undefined array key" warning
if (!isset($adstxtmanager_status['status'])) {
$adstxtmanager_status['status'] = false;
}
if ($adstxtmanager_status['status'] === true) {
?>
Success: Your ads.txt redirect is successfully setup.
$val) {
$$key = $val;
}
$file = ADSTXT_MANAGER__PLUGIN_DIR . 'admin/partials/' . 'adstxtmanager-admin-display' . '.php';
include($file);
}
}
}
/**
* Add settings action link to the plugins page.
*
* @since 1.0.0
*
* @param $links
*
* @return array
*/
public function add_action_links($links)
{
$settings_link = array(
'' . __('Settings') . '',
'' . __('Login') . '',
);
return array_merge($links, $settings_link);
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles()
{
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in AdstxtManager_Loader as all of the hooks are defined
* in that particular class.
*
* The AdstxtManager_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/adstxtmanager-admin.css', array(), $this->version, 'all');
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Ads.txt Manager Settings',
'manage_options',
'adstxtmanager-setting-admin',
array($this, 'create_admin_page')
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option('adstxtmanager_id');
$adstxtmanager_status = get_option('adstxtmanager_status');
// Initialize as array if it doesn't exist
if (false === $adstxtmanager_status) {
$adstxtmanager_status = array();
}
?>
Ads.txt Manager Settings
options['adstxtmanager_id'])) {
?>
In order to use Ads.txt Manager, you must enter your Ads.txt Manager ID number below.
'array')
);
register_setting(
'adstxtmanager_id_group', // Option group
'adstxtmanager_id', // Option name
array($this, 'sanitize') // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Ads.txt Manager ID', // Title
array($this, 'print_section_info'), // Callback
'adstxtmanager-setting-admin' // Page
);
add_settings_field(
'adstxtmanager_id', // ID
'ID Number', // Title
array($this, 'id_number_callback'), // Callback
'adstxtmanager-setting-admin', // Page
'setting_section_id' // Section
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
* @return array
*/
public function sanitize($input)
{
$new_input = array();
if (isset($input['adstxtmanager_id'])) {
$new_input['adstxtmanager_id'] = absint($input['adstxtmanager_id']);
// No need to add additional verification here as we've already added
// a hook directly in the AdstxtManager class to verify after the solution setup
}
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your Ads.txt Manager ID below.';
}
/**
* Get the settings option array and print one of its values
*/
public function id_number_callback()
{
printf(
'',
isset($this->options['adstxtmanager_id']) ? esc_attr($this->options['adstxtmanager_id']) : ''
);
}
/**
* @return bool
*/
public static function verify_adstxt_redirect()
{
global $wp;
$adstxtmanager_status = get_option('adstxtmanager_status');
// Initialize as array if it doesn't exist
if (false === $adstxtmanager_status) {
$adstxtmanager_status = array();
}
// Check if we have a valid ID first
$adstxtmanager_id_value = AdstxtManager::get_adstxtmanager_id_value();
if ($adstxtmanager_id_value <= 0) {
$adstxtmanager_status['message'] = "No valid Ads.txt Manager ID found. Please enter a valid ID in the settings.";
$adstxtmanager_status['status'] = false;
update_option('adstxtmanager_status', $adstxtmanager_status);
return false;
}
//create endpoint request
$response = wp_remote_get(home_url($wp->request) . "/ads.txt", array(
'timeout' => 5,
'headers' => array('Cache-Control' => 'no-cache'),
));
if (
!is_wp_error($response)
&& isset($response['http_response'])
&& $response['http_response'] instanceof \WP_HTTP_Requests_Response
&& method_exists($response['http_response'], 'get_response_object')
) {
$location_url = $response['http_response']->get_response_object()->url;
$response_code = wp_remote_retrieve_response_code($response);
$url_parse = wp_parse_url($location_url);
// Get the expected URL using our helper function
$ads_txt_url = AdstxtManager::get_ads_txt_url();
$expected_host = $ads_txt_url ? wp_parse_url($ads_txt_url, PHP_URL_HOST) : "srv.adstxtmanager.com";
// Check if redirecting to the correct host
if ($url_parse['host'] == $expected_host) {
// Check if the response is a 404 or other error
if ($response_code == 404) {
$adstxtmanager_status['message'] = "The ads.txt URL returned a 404 error. Your account ID may be incorrect or the account may not be properly set up on adstxtmanager.com or Ezoic.";
update_option('adstxtmanager_status', $adstxtmanager_status);
return false;
} else if ($response_code >= 200 && $response_code < 400) {
// Successful response
$adstxtmanager_status['message'] = "";
update_option('adstxtmanager_status', $adstxtmanager_status);
return true;
} else {
// Other error response
$adstxtmanager_status['message'] = "The ads.txt URL returned an error (HTTP code: " . $response_code . "). Please check your account on adstxtmanager.com.";
update_option('adstxtmanager_status', $adstxtmanager_status);
return false;
}
} else {
$adstxtmanager_status['message'] = "The ads.txt is not redirecting to the correct adstxtmanager.com location. Please remove/fix any existing redirections to your request) . "/ads.txt\" target=\"_blank\">ads.txt file.";
update_option('adstxtmanager_status', $adstxtmanager_status);
return false;
}
}
$adstxtmanager_status['message'] = "Unable to verify your ads.txt redirection.";
update_option('adstxtmanager_status', $adstxtmanager_status);
return false;
}
}