-
Notifications
You must be signed in to change notification settings - Fork 383
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
Conversation
48213a0
to
edeb8ce
Compare
d25f053
to
c88a3da
Compare
…ull page describing the options available
76aac15
to
854da9c
Compare
<?php | ||
|
||
add_action('admin_head', 'amp_options_styles'); | ||
function amp_options_styles() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'?
There was a problem hiding this comment.
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' ) );
There was a problem hiding this comment.
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.
includes/admin/functions.php
Outdated
* Grab the analytics options from the DB and return $analytics option | ||
* @return array | ||
*/ | ||
function get_analytics_component_fields($option) { |
There was a problem hiding this comment.
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_
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
includes/admin/functions.php
Outdated
AMP_CUSTOMIZER_QUERY_VAR => true, | ||
), 'customize.php' ); | ||
// Add a link to Settings | ||
add_action( 'admin_menu', 'amp_add_amp_options_link' ); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Moved.
includes/admin/functions.php
Outdated
$menu_slug | ||
); | ||
// Trigger analytics options serializer on analytics option's save | ||
add_action( 'admin_post_analytics_options', 'Analytics_Options_Serializer::save' ); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
text="alberto"
? :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; ?>" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esc_attr
for $id
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esc_textarea
for $config
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
tests/test-amp-analytics-options.php
Outdated
|
||
public function tearDown() { | ||
global $wpdb; | ||
$wpdb->query( 'ROLLBACK' ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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? :(
There was a problem hiding this comment.
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.
tests/test-amp-analytics-options.php
Outdated
delete_option( 'analytics' ); | ||
|
||
// Delete options, if any | ||
delete_option( 'amp-options' ); |
There was a problem hiding this comment.
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.
22e41dc
to
8c4949d
Compare
…user_can(manage_options)
…alytics option form action submission
f29bf55
to
3939fb9
Compare
Looks like there are some test fatals related to the updated code: PHP 5.2
PHP 5.3+
|
…test suite; Constrain placement of amp-analytics-options styles
d5704bf
to
9b5a6b2
Compare
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
Or if we have an error
On wp_loaded so that we can move the admin_post hook into it's init action, since they're closely tied.
To more accurately represent their function
For clarity and to store less data but in a more structured way.
…tic/amp-wp into amedina/amp-analytics-customizer
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