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
Add amp-live-list comments #909
Merged
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
bf03e0f
add amp comment walker
DavidCramer 9a9b1c3
add amp comment walker to autoloader
DavidCramer 5f004e8
add comments sanitiser
DavidCramer 82162c3
add comment walker
DavidCramer 04eebf6
add comment and form sanitizers
DavidCramer 80feeed
correct ID's and max items limit
DavidCramer 396aa91
fix new line issue
DavidCramer 1d8728a
Add form posting handler
DavidCramer b2577c2
rename some methods and add walker depth.
DavidCramer 87c46b6
alternate way to get timestamp
DavidCramer 0822a34
Merge branch 'develop' of https://github.com/Automattic/amp-wp into a…
westonruter dffb7ca
wip - walker changes.
DavidCramer 98fee1b
Rework submission handling
DavidCramer 014807d
Remove uneeded function
DavidCramer ae13421
merge develop
DavidCramer 646debb
remove duplicate form sanitizer entry.
DavidCramer 4b38a5b
remove amp markup from walker
DavidCramer 63d83d0
Lockdown sort direction and admin notice stating unsupported.
DavidCramer 310a74a
Opt to inline the descending-ordering comment logic since temporary
westonruter 5ee1709
Fix script tag generation for amp-mustache as custom-template
westonruter 8f1b37c
Implement form reset after comment submission via amp-bind and amp-state
westonruter 82e5218
Disable comment input fields during submission
westonruter 91b0c8d
Fix construction of attributes in comment wrapper elements
westonruter 77dc127
Power comment replies with amp-state
westonruter ef0e4c3
Show who the commenter is replying to
westonruter 28d760c
Fix setting initial replyToName state
westonruter 8b2232f
Move function with closures out of amp.php for sake of PHP 5.2 check
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,101 +30,116 @@ public function sanitize() { | |
$action = $comment_form->getAttribute( 'action' ); | ||
} | ||
$action_path = wp_parse_url( $action, PHP_URL_PATH ); | ||
if ( ! preg_match( '#/wp-comments-post\.php$#', $action_path ) ) { | ||
continue; | ||
if ( preg_match( '#/wp-comments-post\.php$#', $action_path ) ) { | ||
$this->process_comment_form( $comment_form ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Comment form. | ||
* | ||
* @param DOMElement $comment_form Comment form. | ||
*/ | ||
protected function process_comment_form( $comment_form ) { | ||
/** | ||
* Element. | ||
* | ||
* @var DOMElement $element | ||
*/ | ||
|
||
$amp_state = $this->dom->createElement( 'amp-state' ); | ||
$state_id = sanitize_key( $comment_form->getAttribute( 'id' ) ) . '_props'; | ||
$amp_state->setAttribute( 'id', $state_id ); | ||
/** | ||
* Named input elements. | ||
* | ||
* @var DOMElement[][] $form_fields | ||
*/ | ||
$form_fields = array(); | ||
foreach ( $comment_form->getElementsByTagName( 'input' ) as $element ) { | ||
$name = $element->getAttribute( 'name' ); | ||
if ( $name ) { | ||
$form_fields[ $name ][] = $element; | ||
} | ||
} | ||
foreach ( $comment_form->getElementsByTagName( 'textarea' ) as $element ) { | ||
$name = $element->getAttribute( 'name' ); | ||
if ( $name ) { | ||
$form_fields[ $name ][] = $element; | ||
} | ||
} | ||
|
||
$script = $this->dom->createElement( 'script' ); | ||
$script->setAttribute( 'type', 'application/json' ); | ||
$amp_state->appendChild( $script ); | ||
if ( empty( $form_fields['comment_post_ID'] ) ) { | ||
return; | ||
} | ||
$post_id = (int) $form_fields['comment_post_ID'][0]->getAttribute( 'value' ); | ||
$state_id = AMP_Theme_Support::get_comment_form_state_id( $post_id ); | ||
|
||
$form_state = array( | ||
'values' => array(), | ||
'submitting' => false, | ||
); | ||
$form_state = array( | ||
'values' => array(), | ||
'submitting' => false, | ||
); | ||
|
||
$amp_bind_attr_format = AMP_DOM_Utils::get_amp_bind_placeholder_prefix() . '%s'; | ||
foreach ( $comment_form->getElementsByTagName( 'input' ) as $input ) { | ||
/** | ||
* Input. | ||
* | ||
* @var DOMElement $input | ||
*/ | ||
$name = $input->getAttribute( 'name' ); | ||
if ( ! $name ) { | ||
continue; | ||
} | ||
$amp_bind_attr_format = AMP_DOM_Utils::get_amp_bind_placeholder_prefix() . '%s'; | ||
foreach ( $form_fields as $name => $form_field ) { | ||
foreach ( $form_field as $element ) { | ||
|
||
// @todo Radio and checkbox inputs are not supported yet. | ||
$type = strtolower( $input->getAttribute( 'type' ) ); | ||
if ( in_array( $type, array( 'checkbox', 'radio' ), true ) ) { | ||
if ( in_array( strtolower( $element->getAttribute( 'type' ) ), array( 'checkbox', 'radio' ), true ) ) { | ||
continue; | ||
} | ||
|
||
$form_state['values'][ $name ] = $input->getAttribute( 'value' ); | ||
if ( ! isset( $form_state['values'][ $name ] ) ) { | ||
$form_state['values'][ $name ] = ''; | ||
} | ||
$element->setAttribute( sprintf( $amp_bind_attr_format, 'disabled' ), "$state_id.submitting" ); | ||
|
||
$input->setAttribute( sprintf( $amp_bind_attr_format, 'value' ), "$state_id.values.$name" ); | ||
$input->setAttribute( sprintf( $amp_bind_attr_format, 'disabled' ), "$state_id.submitting" ); | ||
} | ||
foreach ( $comment_form->getElementsByTagName( 'textarea' ) as $textarea ) { | ||
/** | ||
* Textarea. | ||
* | ||
* @var DOMElement $textarea | ||
*/ | ||
$name = $textarea->getAttribute( 'name' ); | ||
if ( ! $name ) { | ||
continue; | ||
if ( 'textarea' === strtolower( $element->nodeName ) ) { | ||
$form_state['values'][ $name ] = $element->textContent; | ||
$element->setAttribute( sprintf( $amp_bind_attr_format, 'text' ), "$state_id.values.$name" ); | ||
} else { | ||
$form_state['values'][ $name ] = $element->hasAttribute( 'value' ) ? $element->getAttribute( 'value' ) : ''; | ||
$element->setAttribute( sprintf( $amp_bind_attr_format, 'value' ), "$state_id.values.$name" ); | ||
} | ||
$form_state['values'][ $name ] = $textarea->textContent; | ||
|
||
$textarea->setAttribute( sprintf( $amp_bind_attr_format, 'text' ), "$state_id.values.$name" ); | ||
$textarea->setAttribute( sprintf( $amp_bind_attr_format, 'disabled' ), "$state_id.submitting" ); | ||
|
||
// Update the state in response to changing the input. | ||
$textarea->setAttribute( 'on', sprintf( | ||
$element->setAttribute( 'on', sprintf( | ||
'change:AMP.setState( { %s: { values: { %s: event.value } } } )', | ||
$state_id, | ||
wp_json_encode( $name ) | ||
) ); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per above, I don't think this |
||
|
||
$script->appendChild( $this->dom->createTextNode( wp_json_encode( $form_state ) ) ); | ||
$comment_form->insertBefore( $amp_state, $comment_form->firstChild ); | ||
// Add amp-state to the document. | ||
$amp_state = $this->dom->createElement( 'amp-state' ); | ||
$amp_state->setAttribute( 'id', $state_id ); | ||
$script = $this->dom->createElement( 'script' ); | ||
$script->setAttribute( 'type', 'application/json' ); | ||
$amp_state->appendChild( $script ); | ||
$script->appendChild( $this->dom->createTextNode( wp_json_encode( $form_state ) ) ); | ||
$comment_form->insertBefore( $amp_state, $comment_form->firstChild ); | ||
|
||
// Update state when submitting form. | ||
$form_reset_state = $form_state; | ||
unset( | ||
$form_reset_state['values']['author'], | ||
$form_reset_state['values']['email'], | ||
$form_reset_state['values']['url'] | ||
); | ||
$on = array( | ||
// Disable the form when submitting. | ||
sprintf( | ||
'submit:AMP.setState( { %s: { submitting: true } } )', | ||
wp_json_encode( $state_id ) | ||
), | ||
// Re-enable the form fields when the submission fails. | ||
sprintf( | ||
'submit-error:AMP.setState( { %s: { submitting: false } } )', | ||
wp_json_encode( $state_id ) | ||
), | ||
// Reset the form to its initial state (with enabled form fields), except for the author, email, and url. | ||
sprintf( | ||
'submit-success:AMP.setState( { %s: %s } )', | ||
$state_id, | ||
wp_json_encode( $form_reset_state ) | ||
), | ||
); | ||
$comment_form->setAttribute( 'on', implode( ';', $on ) ); | ||
} | ||
// Update state when submitting form. | ||
$form_reset_state = $form_state; | ||
unset( | ||
$form_reset_state['values']['author'], | ||
$form_reset_state['values']['email'], | ||
$form_reset_state['values']['url'] | ||
); | ||
$on = array( | ||
// Disable the form when submitting. | ||
sprintf( | ||
'submit:AMP.setState( { %s: { submitting: true } } )', | ||
wp_json_encode( $state_id ) | ||
), | ||
// Re-enable the form fields when the submission fails. | ||
sprintf( | ||
'submit-error:AMP.setState( { %s: { submitting: false } } )', | ||
wp_json_encode( $state_id ) | ||
), | ||
// Reset the form to its initial state (with enabled form fields), except for the author, email, and url. | ||
sprintf( | ||
'submit-success:AMP.setState( { %s: %s } )', | ||
$state_id, | ||
wp_json_encode( $form_reset_state ) | ||
), | ||
); | ||
$comment_form->setAttribute( 'on', implode( ';', $on ) ); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@westonruter should be a static function.