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

Monetization: Add MGID support #13368

Merged
merged 7 commits into from
Jun 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'amp_story_player_assets' => \Google\Web_Stories\AMP_Story_Player_Assets::class,
'adsense' => \Google\Web_Stories\AdSense::class,
'ad_manager' => \Google\Web_Stories\Ad_Manager::class,
'mgid' => \Google\Web_Stories\Mgid::class,

Check warning on line 16 in .phpstorm.meta.php

View check run for this annotation

Codecov / codecov/patch

.phpstorm.meta.php#L16

Added line #L16 was not covered by tests
'admin' => \Google\Web_Stories\Admin\Admin::class,
'admin.revisions' => \Google\Web_Stories\Admin\Revisions::class,
'analytics' => \Google\Web_Stories\Analytics::class,
Expand Down
141 changes: 141 additions & 0 deletions includes/Mgid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* Class Mgid
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types = 1);

namespace Google\Web_Stories;

use Google\Web_Stories\Infrastructure\HasRequirements;

/**
* Class MGID
*/
class Mgid extends Service_Base implements HasRequirements {
/**
* Settings instance.
*
* @var Settings Settings instance.
*/
private Settings $settings;

/**
* Analytics constructor.
*
* @since 1.33.0
*
* @param Settings $settings Settings instance.
* @return void
*/
public function __construct( Settings $settings ) {
$this->settings = $settings;
}

/**
* Initializes all hooks.
*
* @since 1.33.0
*/
public function register(): void {
add_action( 'web_stories_print_analytics', [ $this, 'print_mgid_tag' ] );
}

/**
* Get the list of service IDs required for this service to be registered.
*
* Needed because settings needs to be registered first.
*
* @since 1.33.0
*
* @return string[] List of required services.
*/
public static function get_requirements(): array {
return [ 'settings' ];
}

/**
* Prints the <amp-story-auto-ads> tag for single stories.
*
* @since 1.33.0
*/
public function print_mgid_tag(): void {
$widget = $this->get_widget_id();
$enabled = $this->is_enabled();

if ( ! $enabled || ! $widget ) {
return;

Check warning on line 88 in includes/Mgid.php

View check run for this annotation

Codecov / codecov/patch

includes/Mgid.php#L88

Added line #L88 was not covered by tests
}

$configuration = [
'ad-attributes' => [
'type' => 'mgid',
'data-widget' => $widget,
],
];

/**
* Filters MGID configuration passed to `<amp-story-auto-ads>`.
*
* @since 1.33.0
*
* @param array $settings MGID configuration.
* @param string $widget MGID Widget ID.
leprik marked this conversation as resolved.
Show resolved Hide resolved
*/
$configuration = apply_filters( 'web_stories_mgid_configuration', $configuration, $widget );

?>
<amp-story-auto-ads>
<script type="application/json">
<?php echo wp_json_encode( $configuration, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); ?>
</script>
</amp-story-auto-ads>
<?php
}

/**
* Returns the MGID Widget ID.
*
* @since 1.33.0
*
* @return string Widget ID.
*/
private function get_widget_id(): string {
/**
* Widget ID.
*
* @var string
*/
return $this->settings->get_setting( $this->settings::SETTING_NAME_MGID_WIDGET_ID );
}

/**
* Returns if MGID is enabled.
*
* @since 1.33.0
*/
private function is_enabled(): bool {
return ( 'mgid' === $this->settings->get_setting( $this->settings::SETTING_NAME_AD_NETWORK, 'none' ) );
}
}
1 change: 1 addition & 0 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Plugin extends ServiceBasedPlugin {
'amp_story_player_assets' => AMP_Story_Player_Assets::class,
'adsense' => AdSense::class,
'ad_manager' => Ad_Manager::class,
'mgid' => Mgid::class,
'admin' => Admin\Admin::class,
'analytics' => Analytics::class,
'coi' => Admin\Cross_Origin_Isolation::class,
Expand Down
17 changes: 17 additions & 0 deletions includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Settings implements Service, Registerable, PluginUninstallAware {
*/
public const SETTING_NAME_AD_MANAGER_SLOT_ID = 'web_stories_ad_manager_slot_id';

/**
* MGID Widget ID setting name.
*/
public const SETTING_NAME_MGID_WIDGET_ID = 'web_stories_mgid_widget_id';

/**
* Active publisher logo setting name.
*/
Expand Down Expand Up @@ -231,6 +236,17 @@ public function register(): void {
]
);

register_setting(
leprik marked this conversation as resolved.
Show resolved Hide resolved
self::SETTING_GROUP,
self::SETTING_NAME_MGID_WIDGET_ID,
[
'description' => __( 'MGID Widget ID', 'web-stories' ),
'type' => 'string',
leprik marked this conversation as resolved.
Show resolved Hide resolved
'default' => '',
'show_in_rest' => true,
]
);

register_setting(
self::SETTING_GROUP,
self::SETTING_NAME_ACTIVE_PUBLISHER_LOGO,
Expand Down Expand Up @@ -467,6 +483,7 @@ public function on_plugin_uninstall(): void {
delete_option( self::SETTING_NAME_ADSENSE_PUBLISHER_ID );
delete_option( self::SETTING_NAME_ADSENSE_SLOT_ID );
delete_option( self::SETTING_NAME_AD_MANAGER_SLOT_ID );
delete_option( self::SETTING_NAME_MGID_WIDGET_ID );
delete_option( self::SETTING_NAME_ACTIVE_PUBLISHER_LOGO );
delete_option( self::SETTING_NAME_PUBLISHER_LOGOS );
delete_option( self::SETTING_NAME_VIDEO_CACHE );
Expand Down
2 changes: 2 additions & 0 deletions packages/wp-dashboard/src/api/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const defaultSettingsState = {
adSensePublisherId: '',
adSenseSlotId: '',
adManagerSlotId: '',
mgidWidgetId: '',
adNetwork: AD_NETWORK_TYPE.NONE,
archive: ARCHIVE_TYPE.DEFAULT,
archivePageId: 0,
Expand Down Expand Up @@ -77,6 +78,7 @@ function settingsReducer(state, action) {
adSensePublisherId: action.payload.adSensePublisherId,
adSenseSlotId: action.payload.adSenseSlotId,
adManagerSlotId: action.payload.adManagerSlotId,
mgidWidgetId: action.payload.mgidWidgetId,
adNetwork: action.payload.adNetwork,
videoCache: action.payload.videoCache,
dataRemoval: action.payload.dataRemoval,
Expand Down
6 changes: 6 additions & 0 deletions packages/wp-dashboard/src/api/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
adSensePublisherId: response.web_stories_adsense_publisher_id,
adSenseSlotId: response.web_stories_adsense_slot_id,
adManagerSlotId: response.web_stories_ad_manager_slot_id,
mgidWidgetId: response.web_stories_mgid_widget_id,
adNetwork: response.web_stories_ad_network,
videoCache: response.web_stories_video_cache,
dataRemoval: response.web_stories_data_removal,
Expand Down Expand Up @@ -74,6 +75,7 @@
adSensePublisherId,
adSenseSlotId,
adManagerSlotId,
mgidWidgetId,
adNetwork,
videoCache,
dataRemoval,
Expand Down Expand Up @@ -108,6 +110,10 @@
query.web_stories_ad_manager_slot_id = adManagerSlotId;
}

if (mgidWidgetId !== undefined) {
query.web_stories_mgid_widget_id = mgidWidgetId;

Check warning on line 114 in packages/wp-dashboard/src/api/settings.js

View check run for this annotation

Codecov / codecov/patch

packages/wp-dashboard/src/api/settings.js#L113-L114

Added lines #L113 - L114 were not covered by tests
}

if (adNetwork !== undefined) {
query.web_stories_ad_network = adNetwork;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const TEXT = {
'Learn how to <a>enable programmatic demand in Web Stories</a> through Ad Manager.',
'web-stories'
),
HELPER_MESSAGE_MGID: __(
'Learn how to start <a>monetizing Web Stories with MGID Native Solution</a>',
'web-stories'
),
HELPER_LINK_ADSENSE: __(
'https://support.google.com/adsense/answer/10175505',
'web-stories'
Expand All @@ -57,6 +61,10 @@ export const TEXT = {
'https://support.google.com/admanager/answer/9416436',
'web-stories'
),
HELPER_LINK_MGID: __(
'https://help.mgid.com/generate-revenue-with-amp-web-stories?utm_source=wp-plugin&utm_medium=web-stories-by-google',
'web-stories'
),
};

const OPTIONS = [
Expand All @@ -72,6 +80,10 @@ const OPTIONS = [
label: __('Google Ad Manager', 'web-stories'),
value: AD_NETWORK_TYPE.ADMANAGER,
},
{
label: __('MGID', 'web-stories'),
value: AD_NETWORK_TYPE.MGID,
},
];

function AdNetworkSettings({ adNetwork: adNetworkRaw, handleUpdate }) {
Expand All @@ -88,6 +100,10 @@ function AdNetworkSettings({ adNetwork: adNetworkRaw, handleUpdate }) {
return TEXT.HELPER_MESSAGE_ADSENSE;
}

if (AD_NETWORK_TYPE.MGID === adNetwork) {
return TEXT.HELPER_MESSAGE_MGID;
}

return null;
}, [adNetwork]);

Expand All @@ -100,6 +116,10 @@ function AdNetworkSettings({ adNetwork: adNetworkRaw, handleUpdate }) {
return TEXT.HELPER_LINK_ADSENSE;
}

if (AD_NETWORK_TYPE.MGID === adNetwork) {
return TEXT.HELPER_LINK_MGID;
}

return null;
}, [adNetwork]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import AdNetworkSettings from './adNetwork';
import GoogleAdManagerSettings from './googleAdManager';
import GoogleAdSenseSettings from './googleAdSense';
import MgidSettings from './mgid';

export const TEXT = {
SECTION_HEADING: __('Monetization', 'web-stories'),
Expand Down Expand Up @@ -67,6 +68,7 @@
publisherId,
adSenseSlotId,
adManagerSlotId,
mgidWidgetId,
siteKitStatus,
}) {
const { adsenseActive, installed, adsenseLink } = siteKitStatus;
Expand Down Expand Up @@ -94,6 +96,11 @@
[updateSettings]
);

const handleUpdateMgidWidgetId = useCallback(
(newMgidWidgetId) => updateSettings({ mgidWidgetId: newMgidWidgetId }),

Check warning on line 100 in packages/wp-dashboard/src/components/editorSettings/adManagement/index.js

View check run for this annotation

Codecov / codecov/patch

packages/wp-dashboard/src/components/editorSettings/adManagement/index.js#L100

Added line #L100 was not covered by tests
[updateSettings]
);

const handleUpdateAdNetwork = useCallback(
(newAdNetwork) => {
updateSettings({ adNetwork: newAdNetwork });
Expand Down Expand Up @@ -187,6 +194,12 @@
slotId={adManagerSlotId}
/>
)}
{AD_NETWORK_TYPE.MGID === adNetwork && (
<MgidSettings
handleUpdate={handleUpdateMgidWidgetId}
widgetId={mgidWidgetId}
/>
)}
</div>
)}
</MultilineForm>
Expand All @@ -199,6 +212,7 @@
publisherId: PropTypes.string,
adSenseSlotId: PropTypes.string,
adManagerSlotId: PropTypes.string,
mgidWidgetId: PropTypes.string,
leprik marked this conversation as resolved.
Show resolved Hide resolved
siteKitStatus: PropTypes.shape({
installed: PropTypes.bool,
active: PropTypes.bool,
Expand Down