Skip to content

Commit

Permalink
MGID support
Browse files Browse the repository at this point in the history
  • Loading branch information
leprik committed Jun 13, 2023
1 parent 9146605 commit 19b4772
Show file tree
Hide file tree
Showing 21 changed files with 719 additions and 0 deletions.
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,
'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 Ad_Manager
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2020 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.12.0
*
* @param Settings $settings Settings instance.
* @return void
*/
public function __construct( Settings $settings ) {
$this->settings = $settings;
}

/**
* Initializes all hooks.
*
* @since 1.3.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.13.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.32.0
*/
public function print_mgid_tag(): void {
$widget = $this->get_widget_id();
$enabled = $this->is_enabled();

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

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

/**
* Filters MGID configuration passed to `<amp-story-auto-ads>`.
*
* @since 1.10.0
*
* @param array $settings MGID configuration.
* @param string $widget MGID Widget ID.
*/
$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.32.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.32.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
16 changes: 16 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(
self::SETTING_GROUP,
self::SETTING_NAME_MGID_WIDGET_ID,
[
'description' => __( 'MGID Widget ID', 'web-stories' ),
'type' => 'string',
'default' => '',
'show_in_rest' => true,
]
);

register_setting(
self::SETTING_GROUP,
self::SETTING_NAME_ACTIVE_PUBLISHER_LOGO,
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 @@ const transformSettingResponse = (response) => ({
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 @@ export function updateSettings(apiPath, queryParams) {
adSensePublisherId,
adSenseSlotId,
adManagerSlotId,
mgidWidgetId,
adNetwork,
videoCache,
dataRemoval,
Expand Down Expand Up @@ -108,6 +110,10 @@ export function updateSettings(apiPath, queryParams) {
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;

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

View check run for this annotation

Codecov / codecov/patch

packages/wp-dashboard/src/components/editorSettings/adManagement/adNetwork/index.js#L103-L104

Added lines #L103 - L104 were not covered by tests
}

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;

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

View check run for this annotation

Codecov / codecov/patch

packages/wp-dashboard/src/components/editorSettings/adManagement/adNetwork/index.js#L119-L120

Added lines #L119 - L120 were not covered by tests
}

return null;
}, [adNetwork]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { AD_NETWORK_TYPE } from '../../../constants';
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 @@ function AdManagement({
publisherId,
adSenseSlotId,
adManagerSlotId,
mgidWidgetId,
siteKitStatus,
}) {
const { adsenseActive, installed, adsenseLink } = siteKitStatus;
Expand Down Expand Up @@ -94,6 +96,12 @@ function AdManagement({
[updateSettings]
);

const handleUpdateMgidWidgetId = useCallback(

Check warning on line 99 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#L99

Added line #L99 was not covered by tests
(newMgidWidgetId) =>

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

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎·····`
updateSettings({ mgidWidgetId: newMgidWidgetId }),

Check warning on line 101 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#L101

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

const handleUpdateAdNetwork = useCallback(
(newAdNetwork) => {
updateSettings({ adNetwork: newAdNetwork });
Expand Down Expand Up @@ -187,6 +195,12 @@ function AdManagement({
slotId={adManagerSlotId}
/>
)}
{AD_NETWORK_TYPE.MGID === adNetwork && (
<MgidSettings
handleUpdate={handleUpdateMgidWidgetId}
widgetId={mgidWidgetId}
/>
)}
</div>
)}
</MultilineForm>
Expand All @@ -199,6 +213,7 @@ AdManagement.propTypes = {
publisherId: PropTypes.string,
adSenseSlotId: PropTypes.string,
adManagerSlotId: PropTypes.string,
mgidWidgetId: PropTypes.string,
siteKitStatus: PropTypes.shape({
installed: PropTypes.bool,
active: PropTypes.bool,
Expand Down

0 comments on commit 19b4772

Please sign in to comment.