@@ -493,40 +493,40 @@ protected function create_data_request( Data_Request $data ) {
493493 }
494494 return $ this ->get_tagmanager_service ()->accounts_containers ->listAccountsContainers ( "accounts/ {$ data ['accountID ' ]}" );
495495 case 'POST:settings ' :
496- if ( ! isset ( $ data ['accountID ' ] ) ) {
497- /* translators: %s: Missing parameter name */
498- return new WP_Error ( 'missing_required_param ' , sprintf ( __ ( 'Request parameter is empty: %s. ' , 'google-site-kit ' ), 'accountID ' ), array ( 'status ' => 400 ) );
496+ $ required_params = array ( 'accountID ' , 'usageContext ' );
497+
498+ if ( self ::USAGE_CONTEXT_WEB === $ data ['usageContext ' ] ) { // No AMP.
499+ $ required_params [] = $ this ->context_map [ self ::USAGE_CONTEXT_WEB ];
500+ } elseif ( self ::USAGE_CONTEXT_AMP === $ data ['usageContext ' ] ) { // Primary AMP.
501+ $ required_params [] = $ this ->context_map [ self ::USAGE_CONTEXT_AMP ];
502+ } else { // Secondary AMP.
503+ array_push ( $ required_params , ...array_values ( $ this ->context_map ) );
499504 }
500505
501- $ usage_context = $ data ['usageContext ' ] ?: self ::USAGE_CONTEXT_WEB ;
502-
503- if ( self ::USAGE_CONTEXT_WEB === $ usage_context && ! isset ( $ data ['containerID ' ] ) ) {
504- /* translators: %s: Missing parameter name */
505- return new WP_Error ( 'missing_required_param ' , sprintf ( __ ( 'Request parameter is empty: %s. ' , 'google-site-kit ' ), 'containerID ' ), array ( 'status ' => 400 ) );
506- }
507- if ( self ::USAGE_CONTEXT_AMP === $ usage_context && ! isset ( $ data ['ampContainerID ' ] ) ) {
508- /* translators: %s: Missing parameter name */
509- return new WP_Error ( 'missing_required_param ' , sprintf ( __ ( 'Request parameter is empty: %s. ' , 'google-site-kit ' ), 'ampContainerID ' ), array ( 'status ' => 400 ) );
506+ foreach ( $ required_params as $ required_param ) {
507+ if ( ! isset ( $ data [ $ required_param ] ) ) {
508+ /* translators: %s: Missing parameter name */
509+ return new WP_Error ( 'missing_required_param ' , sprintf ( __ ( 'Request parameter is empty: %s. ' , 'google-site-kit ' ), $ required_param ), array ( 'status ' => 400 ) );
510+ }
510511 }
511512
512- return function () use ( $ data , $ usage_context ) {
513- $ option = $ data ->data ;
514- $ container_key = $ this ->context_map [ $ usage_context ];
515- $ container_id = $ data [ $ container_key ];
516-
517- if ( 'container_create ' === $ container_id ) {
518- $ create_container_response = $ this ->create_container ( $ data ['accountID ' ], $ usage_context );
513+ return function () use ( $ data ) {
514+ $ option = $ data ->data ;
519515
520- if ( is_wp_error ( $ create_container_response ) ) {
521- return $ create_container_response ;
516+ try {
517+ if ( 'container_create ' === $ data ['containerID ' ] ) {
518+ $ option ['containerID ' ] = $ this ->create_container ( $ data ['accountID ' ], self ::USAGE_CONTEXT_WEB );
522519 }
523-
524- $ option [ $ container_key ] = $ create_container_response ;
520+ if ( 'container_create ' === $ data ['ampContainerID ' ] ) {
521+ $ option ['ampContainerID ' ] = $ this ->create_container ( $ data ['accountID ' ], self ::USAGE_CONTEXT_AMP );
522+ }
523+ } catch ( Exception $ e ) {
524+ return $ this ->exception_to_error ( $ e , $ data ->datapoint );
525525 }
526526
527527 $ this ->get_settings ()->merge ( $ option );
528528
529- return $ option ;
529+ return $ this -> get_settings ()-> get () ;
530530 };
531531 case 'GET:tag-permission ' :
532532 return function () use ( $ data ) {
@@ -566,38 +566,37 @@ protected function create_data_request( Data_Request $data ) {
566566 * Creates GTM Container.
567567 *
568568 * @since 1.0.0
569- * @param string $account_id The account ID.
569+ * @param string $account_id The account ID.
570570 * @param string|array $usage_context The container usage context(s).
571571 *
572- * @return mixed Container ID on success, or WP_Error on failure.
572+ * @return string Container public ID.
573+ * @throws Exception Throws an exception if raised during container creation.
573574 */
574575 protected function create_container ( $ account_id , $ usage_context = self ::USAGE_CONTEXT_WEB ) {
575576 $ restore_defer = $ this ->with_client_defer ( false );
576577
577578 // Use site name for container, fallback to domain of reference URL.
578579 $ container_name = get_bloginfo ( 'name ' ) ?: wp_parse_url ( $ this ->context ->get_reference_site_url (), PHP_URL_HOST );
580+ // Prevent naming conflict (Tag Manager does not allow more than one with same name).
581+ if ( self ::USAGE_CONTEXT_AMP === $ usage_context ) {
582+ $ container_name .= ' AMP ' ;
583+ }
579584 $ container_name = self ::sanitize_container_name ( $ container_name );
580585
581586 $ container = new Google_Service_TagManager_Container ();
582587 $ container ->setName ( $ container_name );
583588 $ container ->setUsageContext ( (array ) $ usage_context );
584589
585590 try {
586- $ container = $ this ->get_tagmanager_service ()->accounts_containers ->create ( "accounts/ {$ account_id }" , $ container );
587- } catch ( Google_Service_Exception $ e ) {
588- $ restore_defer ();
589- $ message = $ e ->getErrors ();
590- if ( isset ( $ message [0 ]['message ' ] ) ) {
591- $ message = $ message [0 ]['message ' ];
592- }
593- return new WP_Error ( $ e ->getCode (), $ message );
594- } catch ( Exception $ e ) {
591+ $ new_container = $ this ->get_tagmanager_service ()->accounts_containers ->create ( "accounts/ {$ account_id }" , $ container );
592+ } catch ( Exception $ exception ) {
595593 $ restore_defer ();
596- return new WP_Error ( $ e -> getCode (), $ e -> getMessage () ) ;
594+ throw $ exception ;
597595 }
598596
599597 $ restore_defer ();
600- return $ container ->getPublicId ();
598+
599+ return $ new_container ->getPublicId ();
601600 }
602601
603602 /**
@@ -646,27 +645,15 @@ protected function parse_data_response( Data_Request $data, $response ) {
646645 return array_merge ( $ response , compact ( 'containers ' ) );
647646 case 'GET:containers ' :
648647 /* @var Google_Service_TagManager_ListContainersResponse $response Response object. */
649- $ account_id = $ data ['accountID ' ];
650648 $ usage_context = $ data ['usageContext ' ] ?: self ::USAGE_CONTEXT_WEB ;
651649 /* @var Google_Service_TagManager_Container[] $containers Filtered containers. */
652650 $ containers = array_filter (
653651 (array ) $ response ->getContainer (),
654652 function ( Google_Service_TagManager_Container $ container ) use ( $ usage_context ) {
655- return in_array ( $ usage_context , $ container ->getUsageContext (), true );
653+ return array_intersect ( ( array ) $ usage_context , $ container ->getUsageContext () );
656654 }
657655 );
658656
659- if ( ! $ containers && $ account_id ) {
660- // If no containers, attempt to create a new container.
661- $ new_container = $ this ->create_container ( $ account_id , $ usage_context );
662-
663- if ( is_wp_error ( $ new_container ) ) {
664- return $ new_container ;
665- }
666-
667- return $ this ->get_data ( 'containers ' , array ( 'accountID ' => $ account_id ) );
668- }
669-
670657 return array_values ( $ containers );
671658 }
672659
@@ -693,7 +680,13 @@ function ( Google_Service_TagManager_Container $container ) use ( $usage_context
693680 private function get_account_for_container ( $ container_id , $ accounts ) {
694681 foreach ( (array ) $ accounts as $ account ) {
695682 /* @var Google_Service_TagManager_Account $account Tag manager account */
696- $ containers = $ this ->get_data ( 'containers ' , array ( 'accountID ' => $ account ->getAccountId () ) );
683+ $ containers = $ this ->get_data (
684+ 'containers ' ,
685+ array (
686+ 'accountID ' => $ account ->getAccountId (),
687+ 'usageContext ' => array_keys ( $ this ->context_map ),
688+ )
689+ );
697690
698691 if ( is_wp_error ( $ containers ) ) {
699692 break ;
0 commit comments