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

Enable adding AMP Analytics components using the Options API #714

Merged
merged 43 commits into from
Jul 20, 2017

Conversation

amedina
Copy link
Member

@amedina amedina commented Jun 14, 2017

The goal is to provide a simple mechanism for users to add AMP analytics components to posts via a configuration panel of analytics options.

This is an initial PR to kick off the review process. Things missing: (1) testing; (2) ironing some rough edges (e.g. details on the AMP top-level menu); (3) Styling of the Analytics options page

@amedina amedina requested review from mjangda and pbakaus June 14, 2017 00:56
@amedina
Copy link
Member Author

amedina commented Jun 14, 2017

screen shot 2017-06-13 at 5 56 06 pm

@amedina amedina force-pushed the amedina/amp-analytics-customizer branch from 48213a0 to edeb8ce Compare June 14, 2017 16:55
@amedina amedina force-pushed the amedina/amp-analytics-customizer branch from d25f053 to c88a3da Compare June 15, 2017 20:22
@amedina
Copy link
Member Author

amedina commented Jun 19, 2017

screen shot 2017-06-19 at 10 34 19 am

@amedina amedina force-pushed the amedina/amp-analytics-customizer branch from 76aac15 to 854da9c Compare June 19, 2017 18:43
<?php

add_action('admin_head', 'amp_options_styles');
function amp_options_styles() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this inside the AMP_Analytics_Options_Submenu class? We should also limit it to so that the CSS is only output on the AMP Analytics page.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1) Agree. Moved.
(2) What is the best way to restrict it to the analytics options page? The page does not use a template; should I use the URL parameter 'page='amp-analytics'?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use this hook instead: https://developer.wordpress.org/reference/hooks/admin_print_styles-hook_suffix/

add_action( 'admin_print_styles-' . $this->menu_slug, array( $this, 'amp_options_styles' ) );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I replaced the action added to admin_head with this one and the styles do not get applied.

* Grab the analytics options from the DB and return $analytics option
* @return array
*/
function get_analytics_component_fields($option) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be prefixed with amp_

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

private function add_submenu() {
add_submenu_page(
$this->parent_menu_slug,
'AMP Analytics Options',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to pass this and all other translatable strings through the appropriate i18n function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

AMP_CUSTOMIZER_QUERY_VAR => true,
), 'customize.php' );
// Add a link to Settings
add_action( 'admin_menu', 'amp_add_amp_options_link' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved out of amp_init_customizer since it's not related to the customizer. Probably a new callback hooked on admin_init.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Moved.

$menu_slug
);
// Trigger analytics options serializer on analytics option's save
add_action( 'admin_post_analytics_options', 'Analytics_Options_Serializer::save' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably use register_setting here with the sanitize_callback.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss.

<label>Type: </label>
<input class="option-input" type="text" name=vendor-type value="<?php echo $type; ?>" />
<label>Id: </label>
<input type="text" name=id value="<?php echo substr($id, -6); ?>" text="alberto" readonly />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text="alberto"? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esc_attr for the $id

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text="alberto"? Of course! :) ==> removed.

<input class="option-input" type="text" name=vendor-type value="<?php echo $type; ?>" />
<label>Id: </label>
<input type="text" name=id value="<?php echo substr($id, -6); ?>" text="alberto" readonly />
<input type="hidden" name=id-value value="<?php echo $id; ?>" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esc_attr for $id

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

<p>
<label>JSON Configuration:</label>
<br />
<textarea rows="10" cols="100" name="config"><?php echo stripslashes($config); ?></textarea>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esc_textarea for $config.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


public function tearDown() {
global $wpdb;
$wpdb->query( 'ROLLBACK' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? I think the class already does this for us?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True; leftover from trials.


class Analytics_Options_Serializer {

public static function save() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, we should look into using the settings API for this instead of manually handling the save (e.g. https://konstantin.blog/2012/the-wordpress-settings-api/). Right now, this handler doesn't have proper capability checks or CSRF protection, which we get "for free" with the settings API.

We can register the setting with register_setting and with the sanitize_callback where most of the logic from this save would move to. Also, thinking to the future, it might be good to nest options under an amp_settings option so the structure looks something like:

amp_settings => array(
    amp_analytics => array(
        'googleanalytics-basd12' => array( ... ),
        'parsely-sua231' => array( ... ),
    ),
)

If we want to keep it as a top-level option for analytics, we need to rename it to amp_analytics_config (to avoid conflicts with other plugins/themes).

I'll see if I can pull together an example of moving things over.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1) Good idea re:Thinking about the future. The options are saved now according to the hierarchy you suggested.
(2) Thinking about using the Settings API. Need to change everything? :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Discussed offline] Keeping the use of the Options API.

delete_option( 'analytics' );

// Delete options, if any
delete_option( 'amp-options' );
Copy link
Member Author

@amedina amedina Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the ROLLBACK comment before: some tests were failing seemingly due the DB not being rolled back between tests; it should be but... Adding this delete_option back for now.

@amedina amedina force-pushed the amedina/amp-analytics-customizer branch from 22e41dc to 8c4949d Compare June 22, 2017 17:47
@amedina amedina force-pushed the amedina/amp-analytics-customizer branch from f29bf55 to 3939fb9 Compare June 26, 2017 22:00
@mjangda
Copy link
Contributor

mjangda commented Jun 30, 2017

Looks like there are some test fatals related to the updated code:

PHP 5.2
https://travis-ci.org/Automattic/amp-wp/jobs/248203194

Fatal error: Call to undefined function json_last_error() in /home/travis/build/Automattic/amp-wp/includes/utils/class-amp-html-utils.php on line 24

PHP 5.3+
https://travis-ci.org/Automattic/amp-wp/jobs/248203203

1) AMP_Render_Post_Test::test__invalid_post
pre_amp_render_post action fire when it should not have.
Failed asserting that 3 matches expected 0.
/home/travis/build/Automattic/amp-wp/tests/test-amp-render-post.php:9
2) AMP_Render_Post_Test::test__valid_post
Failed asserting that 4 matches expected 1.

…test suite; Constrain placement of amp-analytics-options styles
mjangda and others added 8 commits July 10, 2017 16:59
Fix spacing, indents, and yoda conditions
…tic/amp-wp into amedina/amp-analytics-customizer
To simplify fetching nested options
Clearer titles, better escaping, code formatting.
…tic/amp-wp into amedina/amp-analytics-customizer
@amedina amedina merged commit c0bdac8 into master Jul 20, 2017
@westonruter westonruter deleted the amedina/amp-analytics-customizer branch February 3, 2018 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants