'child-opt')
);
add_settings_field(
'adsforwp_ad_blocker_notice_options', // ID
'', // Title
array($this, 'adsforwp_ad_blocker_notice_opt_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section', // Settings Section ID
array('class' => 'subchild-opt')
);
add_settings_field(
'adsforwp_ad_performance_tracking', // ID
'Ad Performance Tracking', // Title
array($this, 'adsforwp_ad_performance_tracking_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section' // Settings Section ID
);
add_settings_field(
'adsforwp_ad_revenue_sharing', // ID
'Revenue Sharing', // Title
array($this, 'adsforwp_ad_revenue_sharing_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section' // Settings Section ID
);
add_settings_field(
'adsforwp_ad_sponsorship_label', // ID
'Ad Label', // Title
array($this, 'adsforwp_ad_sponsorship_label_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section' // Settings Section ID
);
add_settings_field(
'adsforwp_ad_label_options', // ID
'', // Title
array($this, 'adsforwp_ad_label_options_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section', // Settings Section ID
array('class' => 'adlabel-child-opt')
);
add_settings_field(
'adsforwp_ad_fraud_protection', // ID
'Click Fraud Protection', // Title
array($this, 'adsforwp_ad_fraud_protection_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section' // Settings Section ID
);
add_settings_field(
'adsforwp_ad_fraud_protection_options', // ID
'', // Title
array($this, 'adsforwp_ad_fraud_protection_options_callback'), // Callback
'adsforwp_general_section', // Page slug
'adsforwp_general_section', // Settings Section ID
array('class' => 'adlabel-child-opt')
);
add_settings_section('adsforwp_support_section', 'Contact Us', '__return_false', 'adsforwp_support_section');
add_settings_field(
'adsforwp_contact_us_form', // ID
'', // Title
array($this, 'adsforwp_contact_us_form_callback'), // Callback
'adsforwp_support_section', // Page slug
'adsforwp_support_section' // Settings Section ID
);
add_settings_section('adsforwp_pro_section', '', '__return_false', 'adsforwp_pro_section');
add_settings_field(
'adsforwp_pro_content', // ID
'', // Title
array($this, 'adsforwp_pro_content_callback'), // Callback
'adsforwp_pro_section', // Page slug
'adsforwp_pro_section' // Settings Section ID
);
}
public function adsforwp_custom_upload_mimes($mimes = array()) {
$mimes['json'] = "application/json";
return $mimes;
}
public function adsforwp_handle_file_upload($option){
if ( ! current_user_can( 'upload_files' ) ) {
return $option;
}
$fileInfo = wp_check_filetype(basename($_FILES['adsforwp_import_backup']['name']));
if (!empty($fileInfo['ext']) && $fileInfo['ext'] == 'json') {
if(!empty($_FILES["adsforwp_import_backup"]["tmp_name"])){
$urls = wp_handle_upload($_FILES["adsforwp_import_backup"], array('test_form' => FALSE));
$url = $urls["url"];
update_option('adsforwp-file-upload_url',esc_url($url));
}
}
return $option;
}
public function adsforwp_check_data_imported_from($plugin_post_type_name){
$cc_args = array(
'posts_per_page' => -1,
'post_type' => 'adsforwp',
'meta_key' => 'imported_from',
'meta_value' => $plugin_post_type_name,
);
$imported_from = new WP_Query( $cc_args );
return $imported_from;
}
/**
* since v1.9.3
* @param string $error
* @return type
*/
public function adsforwp_format_error( $error ) {
$messages = $this->adsforwp_get_error_messages();
if ( ! isset( $messages[ $error['type'] ] ) ) {
return __( 'Unknown error', 'ads-for-wp' );
}
if ( ! isset( $error['value'] ) ) {
$error['value'] = '';
}
$message = sprintf( esc_html( $messages[ $error['type'] ] ), '' . esc_html( $error['value'] ) . '' );
$message = sprintf(
/* translators: Error message output. 1: Line number, 2: Error message */
__( 'Line %1$s: %2$s', 'ads-for-wp' ),
esc_html( $error['line'] ),
$message // This is escaped piece-wise above and may contain HTML (code tags) at this point
);
return $message;
}
/**
* since v1.9.3
* Get all non-generic error messages, translated and with placeholders intact.
*
* @return array Associative array of error messages.
*/
public function adsforwp_get_error_messages() {
$messages = array(
'invalid_variable' => __( 'Unrecognized variable' ),
'invalid_record' => __( 'Invalid record' ),
'invalid_account_type' => __( 'Third field should be RESELLER or DIRECT' ),
/* translators: %s: Subdomain */
'invalid_subdomain' => __( '%s does not appear to be a valid subdomain' ),
/* translators: %s: Exchange domain */
'invalid_exchange' => __( '%s does not appear to be a valid exchange domain' ),
/* translators: %s: Alphanumeric TAG-ID */
'invalid_tagid' => __( '%s does not appear to be a valid TAG-ID' ),
);
return $messages;
}
/**
* since v1.9.3
* Validate a single line.
*
* @param string $line The line to validate.
* @param string $line_number The line number being evaluated.
*
* @return array {
* @type string $sanitized Sanitized version of the original line.
* @type array $errors Array of errors associated with the line.
* }
*/
public function adsforwp_validate_line( $line, $line_number ) {
$domain_regex = '/^((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}$/i';
$errors = array();
if ( empty( $line ) ) {
$sanitized = '';
} elseif ( 0 === strpos( $line, '#' ) ) { // This is a full-line comment.
$sanitized = wp_strip_all_tags( $line );
} elseif ( 1 < strpos( $line, '=' ) ) { // This is a variable declaration.
// The spec currently supports CONTACT and SUBDOMAIN.
if ( ! preg_match( '/^(CONTACT|SUBDOMAIN)=/i', $line ) ) {
$errors[] = array(
'line' => $line_number,
'type' => 'invalid_variable',
);
} elseif ( 0 === stripos( $line, 'subdomain=' ) ) { // Subdomains should be, well, subdomains.
// Disregard any comments.
$subdomain = explode( '#', $line );
$subdomain = $subdomain[0];
$subdomain = explode( '=', $subdomain );
array_shift( $subdomain );
// If there's anything other than one piece left something's not right.
if ( 1 !== count( $subdomain ) || ! preg_match( $domain_regex, $subdomain[0] ) ) {
$subdomain = implode( '', $subdomain );
$errors[] = array(
'line' => $line_number,
'type' => 'invalid_subdomain',
'value' => $subdomain,
);
}
}
$sanitized = wp_strip_all_tags( $line );
unset( $subdomain );
} else { // Data records: the most common.
// Disregard any comments.
$record = explode( '#', $line );
$record = $record[0];
// Record format: example.exchange.com,pub-id123456789,RESELLER|DIRECT,tagidhash123(optional).
$fields = explode( ',', $record );
if ( 3 <= count( $fields ) ) {
$exchange = trim( $fields[0] );
$pub_id = trim( $fields[1] );
$account_type = trim( $fields[2] );
if ( ! preg_match( $domain_regex, $exchange ) ) {
$errors[] = array(
'line' => $line_number,
'type' => 'invalid_exchange',
'value' => $exchange,
);
}
if ( ! preg_match( '/^(RESELLER|DIRECT)$/i', $account_type ) ) {
$errors[] = array(
'line' => $line_number,
'type' => 'invalid_account_type',
);
}
if ( isset( $fields[3] ) ) {
$tag_id = trim( $fields[3] );
// TAG-IDs appear to be 16 character hashes.
// TAG-IDs are meant to be checked against their DB - perhaps good for a service or the future.
if ( ! empty( $tag_id ) && ! preg_match( '/^[a-f0-9]{16}$/', $tag_id ) ) {
$errors[] = array(
'line' => $line_number,
'type' => 'invalid_tagid',
'value' => $fields[3],
);
}
}
$sanitized = wp_strip_all_tags( $line );
} else {
// Not a comment, variable declaration, or data record; therefore, invalid.
// Early on we commented the line out for safety but it's kind of a weird thing to do with a JS AYS.
$sanitized = wp_strip_all_tags( $line );
$errors[] = array(
'line' => $line_number,
'type' => 'invalid_record',
);
}
unset( $record, $fields );
}
return array(
'sanitized' => $sanitized,
'errors' => $errors,
);
}
public function adsforwp_pre_update_settings($value, $old_value, $option){
if($option == 'adsforwp_settings'){
$lines = preg_split( '/\r\n|\r|\n/', $value['adsforwp_adstxt'] );
$sanitized = array();
$errors = array();
$response = array();
foreach ( $lines as $i => $line ) {
$line_number = $i + 1;
$result = $this->adsforwp_validate_line( $line, $line_number );
$sanitized[] = $result['sanitized'];
if ( ! empty( $result['errors'] ) ) {
$errors = array_merge( $errors, $result['errors'] );
}
}
$sanitized = implode( PHP_EOL, $sanitized );
$value['adsforwp_adstxt'] = $sanitized;
$value['adsforwp_adstxt_errors'] = $errors;
}
return $value;
}
public function adsforwp_adstxt_manager_callback(){
$settings = adsforwp_defaultSettings();
$errors = array();
if(isset($settings['adsforwp_adstxt_errors']) && is_array($settings['adsforwp_adstxt_errors'])){
$errors = $settings['adsforwp_adstxt_errors'];
}
?>
';
// Errors were originally stored as an array
// This old style only needs to be accounted for here at runtime display
if ( isset( $error['message'] ) ) {
$message = sprintf(
/* translators: Error message output. 1: Line number, 2: Error message */
__( 'Line %1$s: %2$s', 'ads-for-wp' ),
$error['line'],
$error['message']
);
echo esc_html( $message );
} else {
echo $this->adsforwp_format_error( $error );
}
echo '';
}
?>