C:\xampp\htdocs\landing\wp-content\plugins\amp\includes\sanitizers\class-amp-comments-sanitizer.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Class AMP_Comments_Sanitizer.
 *
 * @package AMP
 */

use AmpProject\Dom\Document;

/**
 * Class AMP_Comments_Sanitizer
 *
 * Strips and corrects attributes in forms.
 *
 * @internal
 */
class AMP_Comments_Sanitizer extends AMP_Base_Sanitizer {

    
/**
     * Default args.
     *
     * @since 1.1
     *
     * @var array
     */
    
protected $DEFAULT_ARGS = [
        
'comment_live_list' => false,
    ];

    
/**
     * Pre-process the comment form and comment list for AMP.
     *
     * @since 0.7
     */
    
public function sanitize() {
        foreach ( 
$this->dom->getElementsByTagName'form' ) as $comment_form ) {
            
/**
             * Comment form.
             *
             * @var DOMElement $comment_form
             */
            
$action $comment_form->getAttribute'action-xhr' );
            if ( ! 
$action ) {
                
$action $comment_form->getAttribute'action' );
            }
            
$action_path wp_parse_url$actionPHP_URL_PATH );
            if ( 
preg_match'#/wp-comments-post\.php$#'$action_path ) ) {
                
$this->process_comment_form$comment_form );
            }
        }

        if ( ! empty( 
$this->args['comments_live_list'] ) ) {
            
$comments $this->dom->xpath->query'//amp-live-list/*[ @items ]/*[ starts-with( @id, "comment-" ) ]' );

            foreach ( 
$comments as $comment ) {
                
$this->add_amp_live_list_comment_attributes$comment );
            }
        }
    }

    
/**
     * Comment form.
     *
     * @since 0.7
     *
     * @param DOMElement $comment_form Comment form.
     */
    
protected function process_comment_form$comment_form ) {
        
/**
         * Element.
         *
         * @var DOMElement $element
         */

        /**
         * Named input elements.
         *
         * @var DOMElement[][] $form_fields
         */
        
$form_fields = [];
        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;
            }
        }

        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 = [
            
'values'      => [],
            
'submitting'  => false,
            
'replyToName' => '',
        ];

        if ( ! empty( 
$form_fields['comment_parent'] ) ) {
            
$comment_id = (int) $form_fields['comment_parent'][0]->getAttribute'value' );
            if ( 
$comment_id ) {
                
$reply_comment get_comment$comment_id );
                if ( 
$reply_comment ) {
                    
$form_state['replyToName'] = $reply_comment->comment_author;
                }
            }
        }

        
$amp_bind_attr_format Document::AMP_BIND_DATA_ATTR_PREFIX '%s';
        foreach ( 
$form_fields as $name => $form_field ) {
            foreach ( 
$form_field as $element ) {

                
// @todo Radio and checkbox inputs are not supported yet.
                
if ( in_arraystrtolower$element->getAttribute'type' ) ), [ 'checkbox''radio' ], true ) ) {
                    continue;
                }

                
$element->setAttributesprintf$amp_bind_attr_format'disabled' ), "$state_id.submitting" );

                if ( 
'textarea' === strtolower$element->nodeName ) ) {
                    
$form_state['values'][ $name ] = $element->textContent;
                    
$element->setAttributesprintf$amp_bind_attr_format'text' ), "$state_id.values.$name);
                } else {
                    
$form_state['values'][ $name ] = $element->hasAttribute'value' ) ? $element->getAttribute'value' ) : '';
                    
$element->setAttributesprintf$amp_bind_attr_format'value' ), "$state_id.values.$name);
                }

                
// Update the state in response to changing the input.
                
$element->setAttribute(
                    
'on',
                    
sprintf(
                        
'change:AMP.setState( { %s: { values: { %s: event.value } } } )',
                        
$state_id,
                        
wp_json_encode$name )
                    )
                );
            }
        }

        
// 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->createTextNodewp_json_encode$form_stateJSON_UNESCAPED_UNICODE ) ) );
        
$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 = [
            
// 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_stateJSON_UNESCAPED_UNICODE )
            ),
        ];
        
$comment_form->setAttribute'on'implode';'$on ) );
    }

    
/**
     * Add attributes to comment elements when comments are being presented in amp-live-list, when comments_live_list theme support flag is present.
     *
     * @since 1.1
     *
     * @param DOMElement $comment_element Comment element.
     */
    
protected function add_amp_live_list_comment_attributes$comment_element ) {
        
$comment_id = (int) str_replace'comment-'''$comment_element->getAttribute'id' ) );
        if ( ! 
$comment_id ) {
            return;
        }
        
$comment_object get_comment$comment_id );

        
// Skip if the comment is not valid or the comment has a parent, since in that case it is not relevant for amp-live-list.
        
if ( ! ( $comment_object instanceof WP_Comment ) || $comment_object->comment_parent ) {
            return;
        }

        
$comment_element->setAttribute'data-sort-time'strtotime$comment_object->comment_date ) );

        
$update_time strtotime$comment_object->comment_date );

        
// Ensure the top-level data-update-time reflects the max time of the comments in the thread.
        
$children $comment_object->get_children(
            [
                
'format'       => 'flat',
                
'hierarchical' => 'flat',
                
'orderby'      => 'none',
            ]
        );
        foreach ( 
$children as $child_comment ) {
            
$update_time maxstrtotime$child_comment->comment_date ), $update_time );
        }

        
$comment_element->setAttribute'data-update-time'$update_time );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586