Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
<?php
/**
* Represents a real ads.txt file.
*/
class Advanced_Ads_Ads_Txt_Real_File {
private $records = [];
/**
* Parse a real file.
*
* @param string $file File data.
*/
public function parse_file( $file ) {
$lines = preg_split( '/\r\n|\r|\n/', $file );
$comments = [];
foreach ( $lines as $line ) {
$line = explode( '#', $line );
if ( ! empty( $line[1] ) && $comment = trim( $line[1] ) ) {
$comments[] = '# ' . $comment;
}
if ( ! trim( $line[0] ) ) {
continue;
}
$rec = explode( ',', $line[0] );
$data = [];
foreach ( $rec as $k => $r ) {
$r = trim( $r, " \n\r\t," );
if ( $r ) {
$data[] = $r;
}
}
if ( $data ) {
// Add the record and comments that were placed above or to the right of it.
$this->add_record( implode( ', ', $data ), $comments );
}
$comments = [];
}
}
/**
* Add record.
*
* @string $data Record without comments.
* @array $comments Comments related to the record.
*/
private function add_record( $data, $comments = [] ) {
$this->records[] = [ $data, $comments ];
}
/**
* Get records
*
* @return array
*/
public function get_records() {
return $this->records;
}
/**
* Output file
*
* @return string
*/
public function output() {
$r = '';
foreach ( $this->records as $rec ) {
foreach ( $rec[1] as $rec1 ) {
$r .= $rec1 . "\n";
}
$r .= $rec[0] . "\n";
}
return $r;
}
/**
* Subtract another ads.txt file.
*
* @return string
*/
public function subtract( Advanced_Ads_Ads_Txt_Real_File $subtrahend ) {
$r1 = $subtrahend->get_records();
foreach ( $this->records as $k => $record ) {
foreach ( $r1 as $r ) {
if ( $record[0] === $r[0] ) {
unset( $this->records[ $k ] );
}
}
}
}
}

View File

@@ -0,0 +1,220 @@
<?php
/**
* Manage the data.
*/
class Advanced_Ads_Ads_Txt_Strategy {
const OPTION = 'advanced_ads_ads_txt';
private $options;
private $changed = false;
public function __construct() {
$this->options = $this->get_options();
$this->changed = false;
}
/**
* Whether to include records from other sites of the network.
*
* @return bool
*/
public function is_all_network() {
$options = $this->get_options();
return is_multisite() && $options['all_network'];
}
/**
* Is enabled.
*
* @return bool
*/
public function is_enabled() {
$this->options = $this->get_options();
return $this->options['enabled'];
}
/**
* Get additional content.
*
* @return string.
*/
public function get_additional_content() {
$options = $this->get_options();
return $options['custom'];
}
/**
* Toggle the file and add additional conent.
*
* @return bool.
*/
public function toggle( $is_enabled, $all_network, $additional_content ) {
$prev = $this->get_options();
$this->options['enabled'] = $is_enabled;
$this->options['all_network'] = $all_network;
$this->options['custom'] = $additional_content;
if ( $this->options !== $prev ) {
$this->changed = true;
}
return true;
}
/**
* Add ad network data.
*
* @param string $id Ad network id.
* @param string $rec A Record to add.
*
* @return bool
*/
public function add_network_data( $id, $rec ) {
$prev = $this->get_options();
$this->options['networks'][ $id ]['rec'] = $rec;
if ( $this->options !== $prev ) {
$this->changed = true;
}
return true;
}
/**
* Set additional content.
*
* @param string $custom Additional content.
* @param bool $replace Whether to replace or not.
*
* @return bool
*/
public function set_additional_content( $custom, $replace = false ) {
$prev = $this->get_options();
if ( $replace ) {
$this->options['custom'] = $custom;
} else {
$this->options['custom'] .= "\n" . $custom;
}
if ( $this->options !== $prev ) {
$this->changed = true;
}
return true;
}
/**
* Prepare content of a blog for output.
*
* @param array $options Options.
*
* @return string
*/
public function parse_content( $options ) {
$o = '';
foreach ( $options['networks'] as $_id => $data ) {
if ( ! empty( $data['rec'] ) ) {
$o .= $data['rec'] . "\n";
}
}
if ( ! empty( $options['custom'] ) ) {
$o .= $options['custom'] . "\n";
}
return $o;
}
/**
* Save options.
*
* @return bool
*/
public function save_options() {
if ( ! $this->changed ) {
return true;
}
$blog_id = get_current_blog_id();
$tmp_options = Advanced_Ads_Ads_Txt_Utils::remove_duplicate_lines(
[
$blog_id => $this->options,
]
);
$this->options = $tmp_options[ $blog_id ];
if ( is_multisite() ) {
update_site_meta(
get_current_blog_id(),
self::OPTION,
$this->options
);
} else {
update_option(
self::OPTION,
$this->options
);
}
$this->changed = false;
delete_transient( Advanced_Ads_Ads_Txt_Admin::get_transient_key() );
return true;
}
/**
* Get options.
*
* @return array
*/
public function get_options() {
if ( isset( $this->options ) ) {
return $this->options;
}
if ( is_multisite() ) {
$options = get_site_meta( get_current_blog_id(), self::OPTION, true );
} else {
$options = get_option( self::OPTION, [] );
}
if ( ! is_array( $options ) ) {
$options = [];
}
$this->options = $this->load_default_options( $options );
return $this->options;
}
/**
* Load default options.
*
* @param array $options Options.
*
* @return array
*/
public function load_default_options( $options ) {
if ( ! isset( $options['enabled'] ) ) {
$options['enabled'] = true;
}
if ( ! isset( $options['all_network'] ) ) {
$options['all_network'] = false;
}
if ( ! isset( $options['custom'] ) ) {
$options['custom'] = '';
}
if ( ! isset( $options['networks'] ) || ! is_array( $options['networks'] ) ) {
$options['networks'] = [];
}
return $options;
}
}

View File

@@ -0,0 +1,188 @@
<?php
/**
* User interface for managing the 'ads.txt' file.
*/
class Advanced_Ads_Ads_Txt_Utils {
private static $location;
/**
* Get file info.
*
* @param string $url Url to retrieve the file.
* @return array/WP_Error An array containing 'exists', 'is_third_party'.
* A WP_Error upon error.
*/
public static function get_file_info( $url = null ) {
$url = $url ? $url : home_url( '/' );
// Disable ssl verification to prevent errors on servers that are not properly configured with its https certificates.
/** This filter is documented in wp-includes/class-wp-http-streams.php */
$sslverify = apply_filters( 'https_local_ssl_verify', false );
$response = wp_remote_get(
trailingslashit( $url ) . 'ads.txt',
[
'timeout' => 3,
'sslverify' => $sslverify,
'headers' => [
'Cache-Control' => 'no-cache',
],
]
);
$code = wp_remote_retrieve_response_code( $response );
$content = wp_remote_retrieve_body( $response );
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
if ( is_wp_error( $response ) ) {
return $response;
}
$file_exists = ! is_wp_error( $response )
&& 404 !== $code
&& ( false !== stripos( $content_type, 'text/plain' ) );
$header_exists = false !== strpos( $content, Advanced_Ads_Ads_Txt_Public::TOP );
$r = [
'exists' => $file_exists && $header_exists,
'is_third_party' => $file_exists && ! $header_exists
];
return $r;
}
/**
* Check if the another 'ads.txt' file should be hosted on the root domain.
*
* @return bool
*/
public static function need_file_on_root_domain( $url = null ) {
$url = $url ? $url : home_url( '/' );
$parsed_url = wp_parse_url( $url );
if ( ! isset( $parsed_url['host'] ) ) {
return false;
}
$host = $parsed_url['host'];
if ( WP_Http::is_ip_address( $host ) ) {
return false;
}
$host_parts = explode( '.', $host );
$count = count( $host_parts );
if ( $count < 3 ) {
return false;
}
if ( 3 === $count ) {
// Example: `http://one.{net/org/gov/edu/co}.two`.
$suffixes = [ 'net', 'org', 'gov', 'edu', 'co' ];
if ( in_array( $host_parts[ $count - 2 ], $suffixes, true ) ) {
return false;
}
// Example: `one.com.au'.
$suffix_and_tld = implode( '.', array_slice( $host_parts, 1 ) );
if ( in_array( $suffix_and_tld, [ 'com.au', 'com.br', 'com.pl' ] ) ) {
return false;
}
// `http://www.one.two` will only be crawled if `http://one.two` redirects to it.
// Check if such redirect exists.
if ( 'www' === $host_parts[0] ) {
/*
* Do not append `/ads.txt` because otherwise the redirect will not happen.
*/
$no_www_url = $parsed_url['scheme'] . '://' . trailingslashit( $host_parts[1] . '.' . $host_parts[2] );
add_action( 'requests-requests.before_redirect', [ __CLASS__, 'collect_locations' ] );
wp_remote_get( $no_www_url, [ 'timeout' => 5, 'redirection' => 3 ] );
remove_action( 'requests-requests.before_redirect', [ __CLASS__, 'collect_locations' ] );
$no_www_url_parsed = wp_parse_url( self::$location );
if ( isset( $no_www_url_parsed['host'] ) && $no_www_url_parsed['host'] === $host ) {
return false;
}
}
}
return true;
}
/**
* Collect last location.
*
* @return string $location An URL.
*/
public static function collect_locations( $location ) {
self::$location = $location;
}
/**
* Check if the site is in a subdirectory, for example 'http://one.two/three'.
*
* @return bool
*/
public static function is_subdir( $url = null ) {
$url = $url ? $url : home_url( '/' );
$parsed_url = wp_parse_url( $url );
if ( ! empty( $parsed_url['path'] ) && '/' !== $parsed_url['path'] ) {
return true;
}
return false;
}
/**
* Remove duplicate lines.
*
* @param array $blog_data Array of arrays of blog options, keyed by by blog IDs.
* @param array $options {
* Options.
*
* @type string $to_comments Whether to convert duplicate records to comments.
* }
* @return array $blog_data Array of arrays of blog options, keyed by by blog IDs.
*/
public static function remove_duplicate_lines( $blog_data, $options = [] ) {
$to_comments = ! empty( $options['to_comments'] );
$added_records = [];
foreach ( $blog_data as $blog_id => &$blog_options ) {
foreach ( $blog_options['networks'] as $id => $data ) {
// Convert to comments or remove duplicate records that are not comments.
if ( ! empty( $data['rec'] ) && '#' !== substr( $data['rec'], 0, 1 ) && in_array( $data['rec'], $added_records, true ) ) {
if ( $to_comments ) {
$blog_options['networks'][ $id ]['rec'] = '# ' . $blog_options['networks'][ $id ]['rec'];
} else {
unset( $blog_options['networks'][ $id ] );
}
continue;
}
$added_records[] = $data['rec'];
}
$blog_options['custom'] = explode( "\n", $blog_options['custom'] );
$blog_options['custom'] = array_map( 'trim', $blog_options['custom'] );
foreach ( $blog_options['custom'] as $id => $rec ) {
// Convert to comments or remove duplicate records that are not comments.
if ( ! empty( $rec ) && '#' !== substr( $rec, 0, 1 ) && in_array( $rec, $added_records, true ) ) {
if ( $to_comments ) {
$blog_options['custom'][ $id ] = '# ' . $blog_options['custom'][ $id ];
} else {
unset( $blog_options['custom'][ $id ] );
}
continue;
}
$added_records[] = $rec;
}
$blog_options['custom'] = implode( "\n", $blog_options['custom'] );
}
return $blog_data;
}
}