Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordAds: Add inline ads within post content #37170

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/packages/sync/changelog/add-wordads-blaze
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Options: sync WordAds inline ads toggle option
2 changes: 1 addition & 1 deletion projects/packages/sync/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "2.15.x-dev"
"dev-trunk": "2.16.x-dev"
},
"dependencies": {
"test-only": [
Expand Down
1 change: 1 addition & 0 deletions projects/packages/sync/src/class-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Defaults {
'wordads_display_page',
'wordads_display_post',
'wordads_second_belowpost',
'wordads_inline_enabled',
'woocommerce_custom_orders_table_enabled',
'wp_mobile_app_promos',
'wp_mobile_excerpt',
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '2.15.1';
const PACKAGE_VERSION = '2.16.0-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/automattic-for-agencies-client/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions projects/plugins/backup/changelog/add-wordads-blaze
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/backup/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions projects/plugins/boost/changelog/add-wordads-blaze
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/boost/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions projects/plugins/jetpack/_inc/client/earn/ads.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export const Ads = withModuleSettingsFormHelpers(
'wordads_second_belowpost',
'wordads'
);
const wordads_inline_enabled = this.props.getOptionValue(
'wordads_inline_enabled',
'wordads'
);
const wordads_display_front_page = this.props.getOptionValue(
'wordads_display_front_page',
'wordads'
Expand Down Expand Up @@ -270,6 +274,17 @@ export const Ads = withModuleSettingsFormHelpers(
onChange={ this.handleChange( 'wordads_second_belowpost' ) }
label={ __( 'Second ad below post', 'jetpack' ) }
/>
<ToggleControl
checked={ wordads_inline_enabled }
disabled={
! isAdsActive ||
unavailableInOfflineMode ||
this.props.isSavingAnyOption( [ 'wordads' ] )
}
toggling={ this.props.isSavingAnyOption( [ 'wordads_inline_enabled' ] ) }
onChange={ this.handleChange( 'wordads_inline_enabled' ) }
label={ __( 'Inline within post content', 'jetpack' ) }
/>
</FormFieldset>
</SettingsGroup>
<SettingsGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,13 @@ public static function get_updateable_data_list( $selector = '' ) {
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'wordads',
),
'wordads_inline_enabled' => array(
'description' => esc_html__( 'Display inline ad within post content?', 'jetpack' ),
'type' => 'boolean',
'default' => 1,
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'wordads',
),
'wordads_display_front_page' => array(
'description' => esc_html__( 'Display ads on the front page?', 'jetpack' ),
'type' => 'boolean',
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-wordads-blaze
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

WordAds: add inline ads within post content.
5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/add-wordads-blaze#2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions projects/plugins/jetpack/modules/wordads/class-wordads.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_once WORDADS_ROOT . '/php/class-wordads-california-privacy.php';
require_once WORDADS_ROOT . '/php/class-wordads-ccpa-do-not-sell-link-widget.php';
require_once WORDADS_ROOT . '/php/class-wordads-consent-management-provider.php';
require_once WORDADS_ROOT . '/php/class-wordads-smart.php';

/**
* Primary WordAds class.
Expand Down Expand Up @@ -214,6 +215,9 @@ public function init() {
WordAds_Consent_Management_Provider::init();
}

// Initialize Smart.
WordAds_Smart::instance()->init( $this->params );

if ( ( isset( $_SERVER['REQUEST_URI'] ) && '/ads.txt' === $_SERVER['REQUEST_URI'] )
|| ( site_url( 'ads.txt', 'relative' ) === $_SERVER['REQUEST_URI'] ) ) {

Expand Down Expand Up @@ -376,6 +380,22 @@ public function insert_head_meta() {
__ATA.criteo.cmd = __ATA.criteo.cmd || [];
</script>
<?php

// Get an inline tag with a macro as id handled on JS side to use as a fallback.
$tag_inline = $this->get_dynamic_ad_snippet( $this->params->blog_id . 5, 'square', 'inline', '', '{{unique_id}}' );

// Remove linebreaks and sanitize.
$tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) );

// phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
echo <<<HTML
<script>
var sas_fallback = sas_fallback || [];
sas_fallback.push(
{ tag: "$tag_inline", type: 'inline' }
);
</script>
HTML;
}

/**
Expand Down Expand Up @@ -689,16 +709,18 @@ public function get_ad_snippet( $section_id, $height, $width, $location = '', $c
/**
* Returns the dynamic snippet to be inserted into the ad unit
*
* @param int $section_id section_id.
* @param string $form_factor form_factor.
* @param string $location location.
* @param string $relocate location to be moved after the fact for themes without required hook.
* @param int $section_id section_id.
* @param string $form_factor form_factor.
* @param string $location location.
* @param string $relocate location to be moved after the fact for themes without required hook.
* @param string | null $id A unique string ID or placeholder.
*
* @return string
*
* @since 8.7
*/
public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '' ) {
$div_id = 'atatags-' . $section_id . '-' . uniqid();
public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) {
$div_id = 'atatags-' . $section_id . '-' . ( $id ?? uniqid() );
$div_id = esc_attr( $div_id );

// Default form factor.
Expand Down
21 changes: 21 additions & 0 deletions projects/plugins/jetpack/modules/wordads/js/adflow-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function a8c_adflow_callback( data ) {
if ( data && data.scripts && Array.isArray( data.scripts ) ) {
if ( data.config ) {
let configurationScript = document.createElement( 'script' );
configurationScript.id = 'adflow-configuration';
configurationScript.type = 'application/configuration';
diogoca marked this conversation as resolved.
Show resolved Hide resolved
configurationScript.innerHTML = JSON.stringify( data.config );

// Add the adflow-configuration script element to the document's body.
document.head.appendChild( configurationScript );
}

// Load each adflow script.
data.scripts.forEach( function ( scriptUrl ) {
let script = document.createElement( 'script' );
script.src = scriptUrl;
document.head.appendChild( script );
} );
}
}
window.a8c_adflow_callback = a8c_adflow_callback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* A utility class that provides functionality for manipulating arrays.
*
* @package automattic/jetpack
*/

/**
* WordAds_Array_Utils Class.
*/
final class WordAds_Array_Utils {

/**
* Converts a (potentially nested) array to a JavaScript object.
*
* Note: JS code strings should be prefixed with 'js:'.
*
* @param array $value The array to convert to a JavaScript object.
* @param bool $in_list True if we are processing an inner list (non-associative array).
*
* @return string String representation of the JavaScript object
*/
public static function array_to_js_object( array $value, bool $in_list = false ): string {
$properties = array();

foreach ( $value as $k => $v ) {
// Don't set property key for values from non-associative array.
$property_key = $in_list ? '' : "'$k': ";

if ( is_array( $v ) ) {
// Check for empty array.
if ( array() === $v ) {
$properties[] = "'$k': []";
continue;
}

// Check if this is a list and not an associative array.
if ( array_keys( $v ) === range( 0, count( $v ) - 1 ) ) {
// Apply recursively.
$properties[] = $property_key . '[ ' . self::array_to_js_object( $v, true ) . ' ]';
} else {
// Apply recursively.
$properties[] = $property_key . self::array_to_js_object( $v );
}
} elseif ( is_string( $v ) && strpos( $v, 'js:' ) === 0 ) {
// JS code. Strip the 'js:' prefix.
$properties[] = $property_key . substr( $v, 3 );
} elseif ( is_string( $v ) ) {
$properties[] = $property_key . "'" . addcslashes( $v, "'" ) . "'";
} elseif ( is_bool( $v ) ) {
$properties[] = $property_key . ( $v ? 'true' : 'false' );
} elseif ( $v === null ) {
$properties[] = $property_key . 'null';
} else {
$properties[] = $property_key . $v;
}
}

$output = implode( ', ', $properties );

if ( ! $in_list ) {
$output = '{ ' . $output . ' }';
}

return $output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function __construct() {
'wordads_unsafe' => false,
'enable_header_ad' => true,
'wordads_second_belowpost' => true,
'wordads_inline_enabled' => true,
'wordads_display_front_page' => true,
'wordads_display_post' => true,
'wordads_display_page' => true,
Expand Down
Loading
Loading