From e7ba2617d132e12bcdbd43d36ac189477e3a9ab5 Mon Sep 17 00:00:00 2001 From: Alberto Medina Date: Thu, 20 Jul 2017 10:43:40 -0700 Subject: [PATCH] Generate hash string for (vendor,config) to avoid duplicates --- includes/admin/functions.php | 4 +-- .../class-amp-analytics-options-submenu.php | 2 +- ...ass-amp-analytics-options-submenu-page.php | 31 ++++++++++--------- .../views/class-amp-options-manager.php | 16 ++++++---- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/includes/admin/functions.php b/includes/admin/functions.php index c135e492cfd..4a6f00033e4 100644 --- a/includes/admin/functions.php +++ b/includes/admin/functions.php @@ -27,7 +27,7 @@ function amp_init_customizer() { function amp_admin_get_preview_permalink() { /** - * Filter the post type to retrieve the latest of for use in the AMP template customizer. + * Filter the post type to retrieve the latest for use in the AMP template customizer. * * @param string $post_type Post type slug. Default 'post'. */ @@ -102,7 +102,7 @@ function amp_add_custom_analytics( $analytics ) { $analytics[ $entry_id ] = array( 'type' => $entry['type'], 'attributes' => array(), - 'config_data' => $entry['config'], + 'config_data' => json_decode( $entry['config'] ), ); } diff --git a/includes/options/class-amp-analytics-options-submenu.php b/includes/options/class-amp-analytics-options-submenu.php index bccaff58d3a..1e250ffac15 100644 --- a/includes/options/class-amp-analytics-options-submenu.php +++ b/includes/options/class-amp-analytics-options-submenu.php @@ -44,7 +44,7 @@ public function amp_options_styles() { margin: 0 5px; } .amp-analytics-options.notice { - width: 200px; + width: 300px; } ; diff --git a/includes/options/views/class-amp-analytics-options-submenu-page.php b/includes/options/views/class-amp-analytics-options-submenu-page.php index 783d4692813..7068cd2c4e9 100644 --- a/includes/options/views/class-amp-analytics-options-submenu-page.php +++ b/includes/options/views/class-amp-analytics-options-submenu-page.php @@ -40,18 +40,18 @@ private function render_entry( $id = '', $type = '', $config = '' ) {

- -

- -

+ +

+ +

- + - + + render_title(); + // Render entries stored in the DB foreach ( $analytics_entries as $entry_id => $entry ) { $this->render_entry( $entry_id, $entry['type'], $entry['config'] ); } - + // Empty form for adding more entries $this->render_entry(); } } diff --git a/includes/options/views/class-amp-options-manager.php b/includes/options/views/class-amp-options-manager.php index 4c30865d39f..4c312471c9c 100644 --- a/includes/options/views/class-amp-options-manager.php +++ b/includes/options/views/class-amp-options-manager.php @@ -49,22 +49,26 @@ public static function update_analytics_options( $data ) { return false; } - // Validate JSON configuration is valid + // Validate JSON configuration $is_valid_json = AMP_HTML_Utils::is_valid_json( stripslashes( $data['config'] ) ); if ( ! $is_valid_json ) { return false; } - $amp_analytics = self::get_option( 'analytics', array() ); + $entry_vendor_type = sanitize_key( $data['vendor-type'] ); + $entry_config = stripslashes( trim( $data['config'] ) ); + if ( ! empty( $data['id-value'] ) ) { $entry_id = sanitize_key( $data['id-value'] ); } else { - // Generate a random string to uniquely identify this entry - $entry_id = substr( md5( wp_rand() ), 0, 12 ); + // Generate a hash string to uniquely identify this entry + $entry_id = substr( md5( $entry_vendor_type . $entry_config ), 0, 12 ); + // Avoid duplicates + if ( isset( $amp_analytics[ $entry_id ] ) ) { + return false; + } } - $entry_vendor_type = sanitize_key( $data['vendor-type'] ); - $entry_config = stripslashes( trim( $data['config'] ) ); if ( isset( $data['delete'] ) ) { unset( $amp_analytics[ $entry_id ] );