Complex classes like Grunion_Contact_Form_Plugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Grunion_Contact_Form_Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 29 | class Grunion_Contact_Form_Plugin { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string The Widget ID of the widget currently being processed. Used to build the unique contact-form ID for forms embedded in widgets. |
||
| 33 | */ |
||
| 34 | public $current_widget_id; |
||
| 35 | |||
| 36 | static $using_contact_form_field = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int The last Feedback Post ID Erased as part of the Personal Data Eraser. |
||
| 40 | * Helps with pagination. |
||
| 41 | */ |
||
| 42 | private $pde_last_post_id_erased = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string The email address for which we are deleting/exporting all feedbacks |
||
| 46 | * as part of a Personal Data Eraser or Personal Data Exporter request. |
||
| 47 | */ |
||
| 48 | private $pde_email_address = ''; |
||
| 49 | |||
| 50 | static function init() { |
||
| 51 | static $instance = false; |
||
| 52 | |||
| 53 | if ( ! $instance ) { |
||
| 54 | $instance = new Grunion_Contact_Form_Plugin(); |
||
| 55 | |||
| 56 | // Schedule our daily cleanup |
||
| 57 | add_action( 'wp_scheduled_delete', array( $instance, 'daily_akismet_meta_cleanup' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $instance; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Runs daily to clean up spam detection metadata after 15 days. Keeps your DB squeaky clean. |
||
| 65 | */ |
||
| 66 | public function daily_akismet_meta_cleanup() { |
||
| 67 | global $wpdb; |
||
| 68 | |||
| 69 | $feedback_ids = $wpdb->get_col( "SELECT p.ID FROM {$wpdb->posts} as p INNER JOIN {$wpdb->postmeta} as m on m.post_id = p.ID WHERE p.post_type = 'feedback' AND m.meta_key = '_feedback_akismet_values' AND DATE_SUB(NOW(), INTERVAL 15 DAY) > p.post_date_gmt LIMIT 10000" ); |
||
| 70 | |||
| 71 | if ( empty( $feedback_ids ) ) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Fires right before deleting the _feedback_akismet_values post meta on $feedback_ids |
||
| 77 | * |
||
| 78 | * @module contact-form |
||
| 79 | * |
||
| 80 | * @since 6.1.0 |
||
| 81 | * |
||
| 82 | * @param array $feedback_ids list of feedback post ID |
||
| 83 | */ |
||
| 84 | do_action( 'jetpack_daily_akismet_meta_cleanup_before', $feedback_ids ); |
||
| 85 | foreach ( $feedback_ids as $feedback_id ) { |
||
| 86 | delete_post_meta( $feedback_id, '_feedback_akismet_values' ); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Fires right after deleting the _feedback_akismet_values post meta on $feedback_ids |
||
| 91 | * |
||
| 92 | * @module contact-form |
||
| 93 | * |
||
| 94 | * @since 6.1.0 |
||
| 95 | * |
||
| 96 | * @param array $feedback_ids list of feedback post ID |
||
| 97 | */ |
||
| 98 | do_action( 'jetpack_daily_akismet_meta_cleanup_after', $feedback_ids ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Strips HTML tags from input. Output is NOT HTML safe. |
||
| 103 | * |
||
| 104 | * @param mixed $data_with_tags |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | public static function strip_tags( $data_with_tags ) { |
||
| 108 | if ( is_array( $data_with_tags ) ) { |
||
| 109 | foreach ( $data_with_tags as $index => $value ) { |
||
| 110 | $index = sanitize_text_field( (string) $index ); |
||
| 111 | $value = wp_kses( (string) $value, array() ); |
||
| 112 | $value = str_replace( '&', '&', $value ); // undo damage done by wp_kses_normalize_entities() |
||
| 113 | |||
| 114 | $data_without_tags[ $index ] = $value; |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | $data_without_tags = wp_kses( $data_with_tags, array() ); |
||
| 118 | $data_without_tags = str_replace( '&', '&', $data_without_tags ); // undo damage done by wp_kses_normalize_entities() |
||
| 119 | } |
||
| 120 | |||
| 121 | return $data_without_tags; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Class uses singleton pattern; use Grunion_Contact_Form_Plugin::init() to initialize. |
||
| 126 | */ |
||
| 127 | protected function __construct() { |
||
| 128 | $this->add_shortcode(); |
||
| 129 | |||
| 130 | // While generating the output of a text widget with a contact-form shortcode, we need to know its widget ID. |
||
| 131 | add_action( 'dynamic_sidebar', array( $this, 'track_current_widget' ) ); |
||
| 132 | |||
| 133 | // Add a "widget" shortcode attribute to all contact-form shortcodes embedded in widgets |
||
| 134 | add_filter( 'widget_text', array( $this, 'widget_atts' ), 0 ); |
||
| 135 | |||
| 136 | // If Text Widgets don't get shortcode processed, hack ours into place. |
||
| 137 | if ( |
||
| 138 | version_compare( get_bloginfo( 'version' ), '4.9-z', '<=' ) |
||
| 139 | && ! has_filter( 'widget_text', 'do_shortcode' ) |
||
| 140 | ) { |
||
| 141 | add_filter( 'widget_text', array( $this, 'widget_shortcode_hack' ), 5 ); |
||
| 142 | } |
||
| 143 | |||
| 144 | add_filter( 'jetpack_contact_form_is_spam', array( $this, 'is_spam_blocklist' ), 10, 2 ); |
||
| 145 | add_filter( 'jetpack_contact_form_in_comment_disallowed_list', array( $this, 'is_in_disallowed_list' ), 10, 2 ); |
||
| 146 | // Akismet to the rescue |
||
| 147 | if ( defined( 'AKISMET_VERSION' ) || function_exists( 'akismet_http_post' ) ) { |
||
| 148 | add_filter( 'jetpack_contact_form_is_spam', array( $this, 'is_spam_akismet' ), 10, 2 ); |
||
| 149 | add_action( 'contact_form_akismet', array( $this, 'akismet_submit' ), 10, 2 ); |
||
| 150 | } |
||
| 151 | |||
| 152 | add_action( 'loop_start', array( 'Grunion_Contact_Form', '_style_on' ) ); |
||
| 153 | add_action( 'pre_amp_render_post', array( 'Grunion_Contact_Form', '_style_on' ) ); |
||
| 154 | |||
| 155 | add_action( 'wp_ajax_grunion-contact-form', array( $this, 'ajax_request' ) ); |
||
| 156 | add_action( 'wp_ajax_nopriv_grunion-contact-form', array( $this, 'ajax_request' ) ); |
||
| 157 | |||
| 158 | // GDPR: personal data exporter & eraser. |
||
| 159 | add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ) ); |
||
| 160 | add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_personal_data_eraser' ) ); |
||
| 161 | |||
| 162 | // Export to CSV feature |
||
| 163 | if ( is_admin() ) { |
||
| 164 | add_action( 'admin_init', array( $this, 'download_feedback_as_csv' ) ); |
||
| 165 | add_action( 'admin_footer-edit.php', array( $this, 'export_form' ) ); |
||
| 166 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
||
| 167 | add_action( 'current_screen', array( $this, 'unread_count' ) ); |
||
| 168 | } |
||
| 169 | |||
| 170 | // custom post type we'll use to keep copies of the feedback items |
||
| 171 | $slug = 'edit.php?post_type=feedback'; |
||
| 172 | register_post_type( |
||
| 173 | 'feedback', array( |
||
| 174 | 'labels' => array( |
||
| 175 | 'name' => __( 'Form Responses', 'jetpack' ), |
||
| 176 | 'singular_name' => __( 'Form Responses', 'jetpack' ), |
||
| 177 | 'search_items' => __( 'Search Responses', 'jetpack' ), |
||
| 178 | 'not_found' => __( 'No responses found', 'jetpack' ), |
||
| 179 | 'not_found_in_trash' => __( 'No responses found', 'jetpack' ), |
||
| 180 | ), |
||
| 181 | 'menu_icon' => 'dashicons-feedback', |
||
| 182 | 'show_ui' => true, |
||
| 183 | 'show_in_menu' => $slug, |
||
| 184 | 'show_in_admin_bar' => false, |
||
| 185 | 'public' => false, |
||
| 186 | 'rewrite' => false, |
||
| 187 | 'query_var' => false, |
||
| 188 | 'capability_type' => 'page', |
||
| 189 | 'show_in_rest' => true, |
||
| 190 | 'rest_controller_class' => 'Grunion_Contact_Form_Endpoint', |
||
| 191 | 'capabilities' => array( |
||
| 192 | 'create_posts' => 'do_not_allow', |
||
| 193 | 'publish_posts' => 'publish_pages', |
||
| 194 | 'edit_posts' => 'edit_pages', |
||
| 195 | 'edit_others_posts' => 'edit_others_pages', |
||
| 196 | 'delete_posts' => 'delete_pages', |
||
| 197 | 'delete_others_posts' => 'delete_others_pages', |
||
| 198 | 'read_private_posts' => 'read_private_pages', |
||
| 199 | 'edit_post' => 'edit_page', |
||
| 200 | 'delete_post' => 'delete_page', |
||
| 201 | 'read_post' => 'read_page', |
||
| 202 | ), |
||
| 203 | 'map_meta_cap' => true, |
||
| 204 | ) |
||
| 205 | ); |
||
| 206 | |||
| 207 | add_menu_page( |
||
| 208 | __( 'Feedback', 'jetpack' ), |
||
| 209 | __( 'Feedback', 'jetpack' ), |
||
| 210 | 'edit_pages', |
||
| 211 | $slug, |
||
| 212 | null, |
||
| 213 | 'dashicons-feedback', |
||
| 214 | 45 |
||
| 215 | ); |
||
| 216 | add_action( 'admin_menu', array( $this, 'rename_feedback_menu' ) ); |
||
| 217 | |||
| 218 | // Add to REST API post type allowed list. |
||
| 219 | add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_feedback_rest_api_type' ) ); |
||
| 220 | |||
| 221 | // Add "spam" as a post status |
||
| 222 | register_post_status( |
||
| 223 | 'spam', array( |
||
| 224 | 'label' => 'Spam', |
||
| 225 | 'public' => false, |
||
| 226 | 'exclude_from_search' => true, |
||
| 227 | 'show_in_admin_all_list' => false, |
||
| 228 | 'label_count' => _n_noop( 'Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>', 'jetpack' ), |
||
| 229 | 'protected' => true, |
||
| 230 | '_builtin' => false, |
||
| 231 | ) |
||
| 232 | ); |
||
| 233 | |||
| 234 | // POST handler |
||
| 235 | if ( |
||
| 236 | isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) |
||
| 237 | && |
||
| 238 | isset( $_POST['action'] ) && 'grunion-contact-form' == $_POST['action'] |
||
| 239 | && |
||
| 240 | isset( $_POST['contact-form-id'] ) |
||
| 241 | ) { |
||
| 242 | add_action( 'template_redirect', array( $this, 'process_form_submission' ) ); |
||
| 243 | } |
||
| 244 | |||
| 245 | /* |
||
| 246 | Can be dequeued by placing the following in wp-content/themes/yourtheme/functions.php |
||
| 247 | * |
||
| 248 | * function remove_grunion_style() { |
||
| 249 | * wp_deregister_style('grunion.css'); |
||
| 250 | * } |
||
| 251 | * add_action('wp_print_styles', 'remove_grunion_style'); |
||
| 252 | */ |
||
| 253 | wp_register_style( 'grunion.css', GRUNION_PLUGIN_URL . 'css/grunion.css', array(), JETPACK__VERSION ); |
||
| 254 | wp_style_add_data( 'grunion.css', 'rtl', 'replace' ); |
||
| 255 | |||
| 256 | self::register_contact_form_blocks(); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function rename_feedback_menu() { |
||
| 260 | $slug = 'edit.php?post_type=feedback'; |
||
| 261 | remove_submenu_page( |
||
| 262 | $slug, |
||
| 263 | $slug, |
||
| 264 | ); |
||
|
|
|||
| 265 | add_submenu_page( |
||
| 266 | $slug, |
||
| 267 | __( 'Form Responses', 'jetpack' ), |
||
| 268 | __( 'Form Responses', 'jetpack' ), |
||
| 269 | 'edit_pages', |
||
| 270 | $slug, |
||
| 271 | null |
||
| 272 | ); |
||
| 273 | } |
||
| 274 | |||
| 275 | private static function register_contact_form_blocks() { |
||
| 276 | Blocks::jetpack_register_block( |
||
| 277 | 'jetpack/contact-form', |
||
| 278 | array( |
||
| 279 | 'render_callback' => array( __CLASS__, 'gutenblock_render_form' ), |
||
| 280 | ) |
||
| 281 | ); |
||
| 282 | |||
| 283 | // Field render methods. |
||
| 284 | Blocks::jetpack_register_block( |
||
| 285 | 'jetpack/field-text', |
||
| 286 | array( |
||
| 287 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 288 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_text' ), |
||
| 289 | ) |
||
| 290 | ); |
||
| 291 | Blocks::jetpack_register_block( |
||
| 292 | 'jetpack/field-name', |
||
| 293 | array( |
||
| 294 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 295 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_name' ), |
||
| 296 | ) |
||
| 297 | ); |
||
| 298 | Blocks::jetpack_register_block( |
||
| 299 | 'jetpack/field-email', |
||
| 300 | array( |
||
| 301 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 302 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_email' ), |
||
| 303 | ) |
||
| 304 | ); |
||
| 305 | Blocks::jetpack_register_block( |
||
| 306 | 'jetpack/field-url', |
||
| 307 | array( |
||
| 308 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 309 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_url' ), |
||
| 310 | ) |
||
| 311 | ); |
||
| 312 | Blocks::jetpack_register_block( |
||
| 313 | 'jetpack/field-date', |
||
| 314 | array( |
||
| 315 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 316 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_date' ), |
||
| 317 | ) |
||
| 318 | ); |
||
| 319 | Blocks::jetpack_register_block( |
||
| 320 | 'jetpack/field-telephone', |
||
| 321 | array( |
||
| 322 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 323 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_telephone' ), |
||
| 324 | ) |
||
| 325 | ); |
||
| 326 | Blocks::jetpack_register_block( |
||
| 327 | 'jetpack/field-textarea', |
||
| 328 | array( |
||
| 329 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 330 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_textarea' ), |
||
| 331 | ) |
||
| 332 | ); |
||
| 333 | Blocks::jetpack_register_block( |
||
| 334 | 'jetpack/field-checkbox', |
||
| 335 | array( |
||
| 336 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 337 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_checkbox' ), |
||
| 338 | ) |
||
| 339 | ); |
||
| 340 | Blocks::jetpack_register_block( |
||
| 341 | 'jetpack/field-checkbox-multiple', |
||
| 342 | array( |
||
| 343 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 344 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_checkbox_multiple' ), |
||
| 345 | ) |
||
| 346 | ); |
||
| 347 | Blocks::jetpack_register_block( |
||
| 348 | 'jetpack/field-radio', |
||
| 349 | array( |
||
| 350 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 351 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_radio' ), |
||
| 352 | ) |
||
| 353 | ); |
||
| 354 | Blocks::jetpack_register_block( |
||
| 355 | 'jetpack/field-select', |
||
| 356 | array( |
||
| 357 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 358 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_select' ), |
||
| 359 | ) |
||
| 360 | ); |
||
| 361 | Blocks::jetpack_register_block( |
||
| 362 | 'jetpack/field-consent', |
||
| 363 | array( |
||
| 364 | 'parent' => array( 'jetpack/contact-form' ), |
||
| 365 | 'render_callback' => array( __CLASS__, 'gutenblock_render_field_consent' ), |
||
| 366 | ) |
||
| 367 | ); |
||
| 368 | } |
||
| 369 | |||
| 370 | public static function gutenblock_render_form( $atts, $content ) { |
||
| 371 | |||
| 372 | // Render fallback in other contexts than frontend (i.e. feed, emails, API, etc.), unless the form is being submitted. |
||
| 373 | if ( ! jetpack_is_frontend() && ! isset( $_POST['contact-form-id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
||
| 374 | return sprintf( |
||
| 375 | '<div class="%1$s"><a href="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a></div>', |
||
| 376 | esc_attr( Blocks::classes( 'contact-form', $atts ) ), |
||
| 377 | esc_url( get_the_permalink() ), |
||
| 378 | esc_html__( 'Submit a form.', 'jetpack' ) |
||
| 379 | ); |
||
| 380 | } |
||
| 381 | |||
| 382 | return Grunion_Contact_Form::parse( $atts, do_blocks( $content ) ); |
||
| 383 | } |
||
| 384 | |||
| 385 | public static function block_attributes_to_shortcode_attributes( $atts, $type ) { |
||
| 386 | $atts['type'] = $type; |
||
| 387 | if ( isset( $atts['className'] ) ) { |
||
| 388 | $atts['class'] = $atts['className']; |
||
| 389 | unset( $atts['className'] ); |
||
| 390 | } |
||
| 391 | |||
| 392 | if ( isset( $atts['defaultValue'] ) ) { |
||
| 393 | $atts['default'] = $atts['defaultValue']; |
||
| 394 | unset( $atts['defaultValue'] ); |
||
| 395 | } |
||
| 396 | |||
| 397 | return $atts; |
||
| 398 | } |
||
| 399 | |||
| 400 | public static function gutenblock_render_field_text( $atts, $content ) { |
||
| 401 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'text' ); |
||
| 402 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 403 | } |
||
| 404 | public static function gutenblock_render_field_name( $atts, $content ) { |
||
| 405 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'name' ); |
||
| 406 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 407 | } |
||
| 408 | public static function gutenblock_render_field_email( $atts, $content ) { |
||
| 409 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'email' ); |
||
| 410 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 411 | } |
||
| 412 | public static function gutenblock_render_field_url( $atts, $content ) { |
||
| 413 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'url' ); |
||
| 414 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 415 | } |
||
| 416 | public static function gutenblock_render_field_date( $atts, $content ) { |
||
| 417 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'date' ); |
||
| 418 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 419 | } |
||
| 420 | public static function gutenblock_render_field_telephone( $atts, $content ) { |
||
| 421 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'telephone' ); |
||
| 422 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 423 | } |
||
| 424 | public static function gutenblock_render_field_textarea( $atts, $content ) { |
||
| 425 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'textarea' ); |
||
| 426 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 427 | } |
||
| 428 | public static function gutenblock_render_field_checkbox( $atts, $content ) { |
||
| 429 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'checkbox' ); |
||
| 430 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 431 | } |
||
| 432 | public static function gutenblock_render_field_checkbox_multiple( $atts, $content ) { |
||
| 433 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'checkbox-multiple' ); |
||
| 434 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 435 | } |
||
| 436 | public static function gutenblock_render_field_radio( $atts, $content ) { |
||
| 437 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'radio' ); |
||
| 438 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 439 | } |
||
| 440 | public static function gutenblock_render_field_select( $atts, $content ) { |
||
| 441 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'select' ); |
||
| 442 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Render the consent field. |
||
| 447 | * |
||
| 448 | * @param string $atts consent attributes. |
||
| 449 | * @param string $content html content. |
||
| 450 | */ |
||
| 451 | public static function gutenblock_render_field_consent( $atts, $content ) { |
||
| 452 | $atts = self::block_attributes_to_shortcode_attributes( $atts, 'consent' ); |
||
| 453 | |||
| 454 | if ( ! isset( $atts['implicitConsentMessage'] ) ) { |
||
| 455 | $atts['implicitConsentMessage'] = __( "By submitting your information, you're giving us permission to email you. You may unsubscribe at any time.", 'jetpack' ); |
||
| 456 | } |
||
| 457 | |||
| 458 | if ( ! isset( $atts['explicitConsentMessage'] ) ) { |
||
| 459 | $atts['explicitConsentMessage'] = __( 'Can we send you an email from time to time?', 'jetpack' ); |
||
| 460 | } |
||
| 461 | |||
| 462 | return Grunion_Contact_Form::parse_contact_field( $atts, $content ); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Add the 'Export' menu item as a submenu of Feedback. |
||
| 467 | */ |
||
| 468 | public function admin_menu() { |
||
| 469 | add_submenu_page( |
||
| 470 | 'edit.php?post_type=feedback', |
||
| 471 | __( 'Export feedback as CSV', 'jetpack' ), |
||
| 472 | __( 'Export CSV', 'jetpack' ), |
||
| 473 | 'export', |
||
| 474 | 'feedback-export', |
||
| 475 | array( $this, 'export_form' ) |
||
| 476 | ); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Add to REST API post type allowed list. |
||
| 481 | */ |
||
| 482 | function allow_feedback_rest_api_type( $post_types ) { |
||
| 483 | $post_types[] = 'feedback'; |
||
| 484 | return $post_types; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Display the count of new feedback entries received. It's reset when user visits the Feedback screen. |
||
| 489 | * |
||
| 490 | * @since 4.1.0 |
||
| 491 | * |
||
| 492 | * @param object $screen Information about the current screen. |
||
| 493 | */ |
||
| 494 | function unread_count( $screen ) { |
||
| 495 | if ( isset( $screen->post_type ) && 'feedback' == $screen->post_type ) { |
||
| 496 | update_option( 'feedback_unread_count', 0 ); |
||
| 497 | } else { |
||
| 498 | global $menu; |
||
| 499 | if ( isset( $menu ) && is_array( $menu ) && ! empty( $menu ) ) { |
||
| 500 | foreach ( $menu as $index => $menu_item ) { |
||
| 501 | if ( 'edit.php?post_type=feedback' == $menu_item[2] ) { |
||
| 502 | $unread = get_option( 'feedback_unread_count', 0 ); |
||
| 503 | if ( $unread > 0 ) { |
||
| 504 | $unread_count = current_user_can( 'publish_pages' ) ? " <span class='feedback-unread count-{$unread} awaiting-mod'><span class='feedback-unread-count'>" . number_format_i18n( $unread ) . '</span></span>' : ''; |
||
| 505 | $menu[ $index ][0] .= $unread_count; |
||
| 506 | } |
||
| 507 | break; |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Handles all contact-form POST submissions |
||
| 516 | * |
||
| 517 | * Conditionally attached to `template_redirect` |
||
| 518 | */ |
||
| 519 | function process_form_submission() { |
||
| 520 | // Add a filter to replace tokens in the subject field with sanitized field values |
||
| 521 | add_filter( 'contact_form_subject', array( $this, 'replace_tokens_with_input' ), 10, 2 ); |
||
| 522 | |||
| 523 | $id = stripslashes( $_POST['contact-form-id'] ); |
||
| 524 | $hash = isset( $_POST['contact-form-hash'] ) ? $_POST['contact-form-hash'] : ''; |
||
| 525 | $hash = preg_replace( '/[^\da-f]/i', '', $hash ); |
||
| 526 | |||
| 527 | if ( ! is_string( $id ) || ! is_string( $hash ) ) { |
||
| 528 | return false; |
||
| 529 | } |
||
| 530 | |||
| 531 | if ( is_user_logged_in() ) { |
||
| 532 | check_admin_referer( "contact-form_{$id}" ); |
||
| 533 | } |
||
| 534 | |||
| 535 | $is_widget = 0 === strpos( $id, 'widget-' ); |
||
| 536 | |||
| 537 | $form = false; |
||
| 538 | |||
| 539 | if ( $is_widget ) { |
||
| 540 | // It's a form embedded in a text widget |
||
| 541 | $this->current_widget_id = substr( $id, 7 ); // remove "widget-" |
||
| 542 | $widget_type = implode( '-', array_slice( explode( '-', $this->current_widget_id ), 0, -1 ) ); // Remove trailing -# |
||
| 543 | |||
| 544 | // Is the widget active? |
||
| 545 | $sidebar = is_active_widget( false, $this->current_widget_id, $widget_type ); |
||
| 546 | |||
| 547 | // This is lame - no core API for getting a widget by ID |
||
| 548 | $widget = isset( $GLOBALS['wp_registered_widgets'][ $this->current_widget_id ] ) ? $GLOBALS['wp_registered_widgets'][ $this->current_widget_id ] : false; |
||
| 549 | |||
| 550 | if ( $sidebar && $widget && isset( $widget['callback'] ) ) { |
||
| 551 | // prevent PHP notices by populating widget args |
||
| 552 | $widget_args = array( |
||
| 553 | 'before_widget' => '', |
||
| 554 | 'after_widget' => '', |
||
| 555 | 'before_title' => '', |
||
| 556 | 'after_title' => '', |
||
| 557 | ); |
||
| 558 | // This is lamer - no API for outputting a given widget by ID |
||
| 559 | ob_start(); |
||
| 560 | // Process the widget to populate Grunion_Contact_Form::$last |
||
| 561 | call_user_func( $widget['callback'], $widget_args, $widget['params'][0] ); |
||
| 562 | ob_end_clean(); |
||
| 563 | } |
||
| 564 | } else { |
||
| 565 | // It's a form embedded in a post |
||
| 566 | $post = get_post( $id ); |
||
| 567 | |||
| 568 | // Process the content to populate Grunion_Contact_Form::$last |
||
| 569 | if ( $post ) { |
||
| 570 | /** This filter is already documented in core. wp-includes/post-template.php */ |
||
| 571 | apply_filters( 'the_content', $post->post_content ); |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | $form = isset( Grunion_Contact_Form::$forms[ $hash ] ) ? Grunion_Contact_Form::$forms[ $hash ] : null; |
||
| 576 | |||
| 577 | // No form may mean user is using do_shortcode, grab the form using the stored post meta |
||
| 578 | if ( ! $form && is_numeric( $id ) && $hash ) { |
||
| 579 | |||
| 580 | // Get shortcode from post meta |
||
| 581 | $shortcode = get_post_meta( $id, "_g_feedback_shortcode_{$hash}", true ); |
||
| 582 | |||
| 583 | // Format it |
||
| 584 | if ( $shortcode != '' ) { |
||
| 585 | |||
| 586 | // Get attributes from post meta. |
||
| 587 | $parameters = ''; |
||
| 588 | $attributes = get_post_meta( $id, "_g_feedback_shortcode_atts_{$hash}", true ); |
||
| 589 | if ( ! empty( $attributes ) && is_array( $attributes ) ) { |
||
| 590 | foreach ( array_filter( $attributes ) as $param => $value ) { |
||
| 591 | $parameters .= " $param=\"$value\""; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | $shortcode = '[contact-form' . $parameters . ']' . $shortcode . '[/contact-form]'; |
||
| 596 | do_shortcode( $shortcode ); |
||
| 597 | |||
| 598 | // Recreate form |
||
| 599 | $form = Grunion_Contact_Form::$last; |
||
| 600 | } |
||
| 601 | } |
||
| 602 | |||
| 603 | if ( ! $form ) { |
||
| 604 | return false; |
||
| 605 | } |
||
| 606 | |||
| 607 | if ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) { |
||
| 608 | return $form->errors; |
||
| 609 | } |
||
| 610 | |||
| 611 | // Process the form |
||
| 612 | return $form->process_submission(); |
||
| 613 | } |
||
| 614 | |||
| 615 | function ajax_request() { |
||
| 616 | $submission_result = self::process_form_submission(); |
||
| 617 | |||
| 618 | if ( ! $submission_result ) { |
||
| 619 | header( 'HTTP/1.1 500 Server Error', 500, true ); |
||
| 620 | echo '<div class="form-error"><ul class="form-errors"><li class="form-error-message">'; |
||
| 621 | esc_html_e( 'An error occurred. Please try again later.', 'jetpack' ); |
||
| 622 | echo '</li></ul></div>'; |
||
| 623 | } elseif ( is_wp_error( $submission_result ) ) { |
||
| 624 | header( 'HTTP/1.1 400 Bad Request', 403, true ); |
||
| 625 | echo '<div class="form-error"><ul class="form-errors"><li class="form-error-message">'; |
||
| 626 | echo esc_html( $submission_result->get_error_message() ); |
||
| 627 | echo '</li></ul></div>'; |
||
| 628 | } else { |
||
| 629 | echo '<h3>' . esc_html__( 'Message Sent', 'jetpack' ) . '</h3>' . $submission_result; |
||
| 630 | } |
||
| 631 | |||
| 632 | die; |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Ensure the post author is always zero for contact-form feedbacks |
||
| 637 | * Attached to `wp_insert_post_data` |
||
| 638 | * |
||
| 639 | * @see Grunion_Contact_Form::process_submission() |
||
| 640 | * |
||
| 641 | * @param array $data the data to insert |
||
| 642 | * @param array $postarr the data sent to wp_insert_post() |
||
| 643 | * @return array The filtered $data to insert |
||
| 644 | */ |
||
| 645 | function insert_feedback_filter( $data, $postarr ) { |
||
| 646 | if ( $data['post_type'] == 'feedback' && $postarr['post_type'] == 'feedback' ) { |
||
| 647 | $data['post_author'] = 0; |
||
| 648 | } |
||
| 649 | |||
| 650 | return $data; |
||
| 651 | } |
||
| 652 | /* |
||
| 653 | * Adds our contact-form shortcode |
||
| 654 | * The "child" contact-field shortcode is enabled as needed by the contact-form shortcode handler |
||
| 655 | */ |
||
| 656 | function add_shortcode() { |
||
| 657 | add_shortcode( 'contact-form', array( 'Grunion_Contact_Form', 'parse' ) ); |
||
| 658 | add_shortcode( 'contact-field', array( 'Grunion_Contact_Form', 'parse_contact_field' ) ); |
||
| 659 | } |
||
| 660 | |||
| 661 | static function tokenize_label( $label ) { |
||
| 662 | return '{' . trim( preg_replace( '#^\d+_#', '', $label ) ) . '}'; |
||
| 663 | } |
||
| 664 | |||
| 665 | static function sanitize_value( $value ) { |
||
| 666 | return preg_replace( '=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $value ); |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Replaces tokens like {city} or {City} (case insensitive) with the value |
||
| 671 | * of an input field of that name |
||
| 672 | * |
||
| 673 | * @param string $subject |
||
| 674 | * @param array $field_values Array with field label => field value associations |
||
| 675 | * |
||
| 676 | * @return string The filtered $subject with the tokens replaced |
||
| 677 | */ |
||
| 678 | function replace_tokens_with_input( $subject, $field_values ) { |
||
| 679 | // Wrap labels into tokens (inside {}) |
||
| 680 | $wrapped_labels = array_map( array( 'Grunion_Contact_Form_Plugin', 'tokenize_label' ), array_keys( $field_values ) ); |
||
| 681 | // Sanitize all values |
||
| 682 | $sanitized_values = array_map( array( 'Grunion_Contact_Form_Plugin', 'sanitize_value' ), array_values( $field_values ) ); |
||
| 683 | |||
| 684 | foreach ( $sanitized_values as $k => $sanitized_value ) { |
||
| 685 | if ( is_array( $sanitized_value ) ) { |
||
| 686 | $sanitized_values[ $k ] = implode( ', ', $sanitized_value ); |
||
| 687 | } |
||
| 688 | } |
||
| 689 | |||
| 690 | // Search for all valid tokens (based on existing fields) and replace with the field's value |
||
| 691 | $subject = str_ireplace( $wrapped_labels, $sanitized_values, $subject ); |
||
| 692 | return $subject; |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Tracks the widget currently being processed. |
||
| 697 | * Attached to `dynamic_sidebar` |
||
| 698 | * |
||
| 699 | * @see $current_widget_id |
||
| 700 | * |
||
| 701 | * @param array $widget The widget data |
||
| 702 | */ |
||
| 703 | function track_current_widget( $widget ) { |
||
| 704 | $this->current_widget_id = $widget['id']; |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Adds a "widget" attribute to every contact-form embedded in a text widget. |
||
| 709 | * Used to tell the difference between post-embedded contact-forms and widget-embedded contact-forms |
||
| 710 | * Attached to `widget_text` |
||
| 711 | * |
||
| 712 | * @param string $text The widget text |
||
| 713 | * @return string The filtered widget text |
||
| 714 | */ |
||
| 715 | function widget_atts( $text ) { |
||
| 716 | Grunion_Contact_Form::style( true ); |
||
| 717 | |||
| 718 | return preg_replace( '/\[contact-form([^a-zA-Z_-])/', '[contact-form widget="' . $this->current_widget_id . '"\\1', $text ); |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * For sites where text widgets are not processed for shortcodes, we add this hack to process just our shortcode |
||
| 723 | * Attached to `widget_text` |
||
| 724 | * |
||
| 725 | * @param string $text The widget text |
||
| 726 | * @return string The contact-form filtered widget text |
||
| 727 | */ |
||
| 728 | function widget_shortcode_hack( $text ) { |
||
| 729 | if ( ! preg_match( '/\[contact-form([^a-zA-Z_-])/', $text ) ) { |
||
| 730 | return $text; |
||
| 731 | } |
||
| 732 | |||
| 733 | $old = $GLOBALS['shortcode_tags']; |
||
| 734 | remove_all_shortcodes(); |
||
| 735 | Grunion_Contact_Form_Plugin::$using_contact_form_field = true; |
||
| 736 | $this->add_shortcode(); |
||
| 737 | |||
| 738 | $text = do_shortcode( $text ); |
||
| 739 | |||
| 740 | Grunion_Contact_Form_Plugin::$using_contact_form_field = false; |
||
| 741 | $GLOBALS['shortcode_tags'] = $old; |
||
| 742 | |||
| 743 | return $text; |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Check if a submission matches the Comment Blocklist. |
||
| 748 | * The Comment Blocklist is a means to moderate discussion, and contact |
||
| 749 | * forms are 1:1 discussion forums, ripe for abuse by users who are being |
||
| 750 | * removed from the public discussion. |
||
| 751 | * Attached to `jetpack_contact_form_is_spam` |
||
| 752 | * |
||
| 753 | * @param bool $is_spam |
||
| 754 | * @param array $form |
||
| 755 | * @return bool TRUE => spam, FALSE => not spam |
||
| 756 | */ |
||
| 757 | public function is_spam_blocklist( $is_spam, $form = array() ) { |
||
| 758 | if ( $is_spam ) { |
||
| 759 | return $is_spam; |
||
| 760 | } |
||
| 761 | |||
| 762 | return $this->is_in_disallowed_list( false, $form ); |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Check if a submission matches the comment disallowed list. |
||
| 767 | * Attached to `jetpack_contact_form_in_comment_disallowed_list`. |
||
| 768 | * |
||
| 769 | * @param boolean $in_disallowed_list Whether the feedback is in the disallowed list. |
||
| 770 | * @param array $form The form array. |
||
| 771 | * @return bool Returns true if the form submission matches the disallowed list and false if it doesn't. |
||
| 772 | */ |
||
| 773 | public function is_in_disallowed_list( $in_disallowed_list, $form = array() ) { |
||
| 774 | if ( $in_disallowed_list ) { |
||
| 775 | return $in_disallowed_list; |
||
| 776 | } |
||
| 777 | |||
| 778 | if ( |
||
| 779 | wp_check_comment_disallowed_list( |
||
| 780 | $form['comment_author'], |
||
| 781 | $form['comment_author_email'], |
||
| 782 | $form['comment_author_url'], |
||
| 783 | $form['comment_content'], |
||
| 784 | $form['user_ip'], |
||
| 785 | $form['user_agent'] |
||
| 786 | ) |
||
| 787 | ) { |
||
| 788 | return true; |
||
| 789 | } |
||
| 790 | |||
| 791 | return false; |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Populate an array with all values necessary to submit a NEW contact-form feedback to Akismet. |
||
| 796 | * Note that this includes the current user_ip etc, so this should only be called when accepting a new item via $_POST |
||
| 797 | * |
||
| 798 | * @param array $form Contact form feedback array |
||
| 799 | * @return array feedback array with additional data ready for submission to Akismet |
||
| 800 | */ |
||
| 801 | function prepare_for_akismet( $form ) { |
||
| 802 | $form['comment_type'] = 'contact_form'; |
||
| 803 | $form['user_ip'] = $_SERVER['REMOTE_ADDR']; |
||
| 804 | $form['user_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
||
| 805 | $form['referrer'] = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ''; |
||
| 806 | $form['blog'] = get_option( 'home' ); |
||
| 807 | |||
| 808 | foreach ( $_SERVER as $key => $value ) { |
||
| 809 | if ( ! is_string( $value ) ) { |
||
| 810 | continue; |
||
| 811 | } |
||
| 812 | if ( in_array( $key, array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'HTTP_USER_AGENT', 'HTTP_REFERER' ) ) ) { |
||
| 813 | // We don't care about cookies, and the UA and Referrer were caught above. |
||
| 814 | continue; |
||
| 815 | } elseif ( in_array( $key, array( 'REMOTE_ADDR', 'REQUEST_URI', 'DOCUMENT_URI' ) ) ) { |
||
| 816 | // All three of these are relevant indicators and should be passed along. |
||
| 817 | $form[ $key ] = $value; |
||
| 818 | } elseif ( wp_startswith( $key, 'HTTP_' ) ) { |
||
| 819 | // Any other HTTP header indicators. |
||
| 820 | // `wp_startswith()` is a wpcom helper function and is included in Jetpack via `functions.compat.php` |
||
| 821 | $form[ $key ] = $value; |
||
| 822 | } |
||
| 823 | } |
||
| 824 | |||
| 825 | return $form; |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Submit contact-form data to Akismet to check for spam. |
||
| 830 | * If you're accepting a new item via $_POST, run it Grunion_Contact_Form_Plugin::prepare_for_akismet() first |
||
| 831 | * Attached to `jetpack_contact_form_is_spam` |
||
| 832 | * |
||
| 833 | * @param bool $is_spam |
||
| 834 | * @param array $form |
||
| 835 | * @return bool|WP_Error TRUE => spam, FALSE => not spam, WP_Error => stop processing entirely |
||
| 836 | */ |
||
| 837 | function is_spam_akismet( $is_spam, $form = array() ) { |
||
| 838 | global $akismet_api_host, $akismet_api_port; |
||
| 839 | |||
| 840 | // The signature of this function changed from accepting just $form. |
||
| 841 | // If something only sends an array, assume it's still using the old |
||
| 842 | // signature and work around it. |
||
| 843 | if ( empty( $form ) && is_array( $is_spam ) ) { |
||
| 844 | $form = $is_spam; |
||
| 845 | $is_spam = false; |
||
| 846 | } |
||
| 847 | |||
| 848 | // If a previous filter has alrady marked this as spam, trust that and move on. |
||
| 849 | if ( $is_spam ) { |
||
| 850 | return $is_spam; |
||
| 851 | } |
||
| 852 | |||
| 853 | if ( ! function_exists( 'akismet_http_post' ) && ! defined( 'AKISMET_VERSION' ) ) { |
||
| 854 | return false; |
||
| 855 | } |
||
| 856 | |||
| 857 | $query_string = http_build_query( $form ); |
||
| 858 | |||
| 859 | if ( method_exists( 'Akismet', 'http_post' ) ) { |
||
| 860 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
||
| 861 | } else { |
||
| 862 | $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port ); |
||
| 863 | } |
||
| 864 | |||
| 865 | $result = false; |
||
| 866 | |||
| 867 | if ( isset( $response[0]['x-akismet-pro-tip'] ) && 'discard' === trim( $response[0]['x-akismet-pro-tip'] ) && get_option( 'akismet_strictness' ) === '1' ) { |
||
| 868 | $result = new WP_Error( 'feedback-discarded', __( 'Feedback discarded.', 'jetpack' ) ); |
||
| 869 | } elseif ( isset( $response[1] ) && 'true' == trim( $response[1] ) ) { // 'true' is spam |
||
| 870 | $result = true; |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Filter the results returned by Akismet for each submitted contact form. |
||
| 875 | * |
||
| 876 | * @module contact-form |
||
| 877 | * |
||
| 878 | * @since 1.3.1 |
||
| 879 | * |
||
| 880 | * @param WP_Error|bool $result Is the submitted feedback spam. |
||
| 881 | * @param array|bool $form Submitted feedback. |
||
| 882 | */ |
||
| 883 | return apply_filters( 'contact_form_is_spam_akismet', $result, $form ); |
||
| 884 | } |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Submit a feedback as either spam or ham |
||
| 888 | * |
||
| 889 | * @param string $as Either 'spam' or 'ham'. |
||
| 890 | * @param array $form the contact-form data |
||
| 891 | */ |
||
| 892 | function akismet_submit( $as, $form ) { |
||
| 893 | global $akismet_api_host, $akismet_api_port; |
||
| 894 | |||
| 895 | if ( ! in_array( $as, array( 'ham', 'spam' ) ) ) { |
||
| 896 | return false; |
||
| 897 | } |
||
| 898 | |||
| 899 | $query_string = ''; |
||
| 900 | if ( is_array( $form ) ) { |
||
| 901 | $query_string = http_build_query( $form ); |
||
| 902 | } |
||
| 903 | if ( method_exists( 'Akismet', 'http_post' ) ) { |
||
| 904 | $response = Akismet::http_post( $query_string, "submit-{$as}" ); |
||
| 905 | } else { |
||
| 906 | $response = akismet_http_post( $query_string, $akismet_api_host, "/1.1/submit-{$as}", $akismet_api_port ); |
||
| 907 | } |
||
| 908 | |||
| 909 | return trim( $response[1] ); |
||
| 910 | } |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Prints the menu |
||
| 914 | */ |
||
| 915 | function export_form() { |
||
| 916 | $current_screen = get_current_screen(); |
||
| 917 | if ( ! in_array( $current_screen->id, array( 'edit-feedback', 'feedback_page_feedback-export' ) ) ) { |
||
| 918 | return; |
||
| 919 | } |
||
| 920 | |||
| 921 | if ( ! current_user_can( 'export' ) ) { |
||
| 922 | return; |
||
| 923 | } |
||
| 924 | |||
| 925 | // if there aren't any feedbacks, bail out |
||
| 926 | if ( ! (int) wp_count_posts( 'feedback' )->publish ) { |
||
| 927 | return; |
||
| 928 | } |
||
| 929 | ?> |
||
| 930 | |||
| 931 | <div id="feedback-export" style="display:none"> |
||
| 932 | <h2><?php _e( 'Export feedback as CSV', 'jetpack' ); ?></h2> |
||
| 933 | <div class="clear"></div> |
||
| 934 | <form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post" class="form"> |
||
| 935 | <?php wp_nonce_field( 'feedback_export', 'feedback_export_nonce' ); ?> |
||
| 936 | |||
| 937 | <input name="action" value="feedback_export" type="hidden"> |
||
| 938 | <label for="post"><?php _e( 'Select feedback to download', 'jetpack' ); ?></label> |
||
| 939 | <select name="post"> |
||
| 940 | <option value="all"><?php esc_html_e( 'All posts', 'jetpack' ); ?></option> |
||
| 941 | <?php echo $this->get_feedbacks_as_options(); ?> |
||
| 942 | </select> |
||
| 943 | |||
| 944 | <br><br> |
||
| 945 | <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Download', 'jetpack' ); ?>"> |
||
| 946 | </form> |
||
| 947 | </div> |
||
| 948 | |||
| 949 | <?php |
||
| 950 | // There aren't any usable actions in core to output the "export feedback" form in the correct place, |
||
| 951 | // so this inline JS moves it from the top of the page to the bottom. |
||
| 952 | ?> |
||
| 953 | <script type='text/javascript'> |
||
| 954 | var menu = document.getElementById( 'feedback-export' ), |
||
| 955 | wrapper = document.getElementsByClassName( 'wrap' )[0]; |
||
| 956 | <?php if ( 'edit-feedback' === $current_screen->id ) : ?> |
||
| 957 | wrapper.appendChild(menu); |
||
| 958 | <?php endif; ?> |
||
| 959 | menu.style.display = 'block'; |
||
| 960 | </script> |
||
| 961 | <?php |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Fetch post content for a post and extract just the comment. |
||
| 966 | * |
||
| 967 | * @param int $post_id The post id to fetch the content for. |
||
| 968 | * |
||
| 969 | * @return string Trimmed post comment. |
||
| 970 | * |
||
| 971 | * @codeCoverageIgnore |
||
| 972 | */ |
||
| 973 | public function get_post_content_for_csv_export( $post_id ) { |
||
| 974 | $post_content = get_post_field( 'post_content', $post_id ); |
||
| 975 | $content = explode( '<!--more-->', $post_content ); |
||
| 976 | |||
| 977 | return trim( $content[0] ); |
||
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Get `_feedback_extra_fields` field from post meta data. |
||
| 982 | * |
||
| 983 | * @param int $post_id Id of the post to fetch meta data for. |
||
| 984 | * |
||
| 985 | * @return mixed |
||
| 986 | */ |
||
| 987 | public function get_post_meta_for_csv_export( $post_id ) { |
||
| 988 | $md = get_post_meta( $post_id, '_feedback_extra_fields', true ); |
||
| 989 | $md['feedback_date'] = get_the_date( DATE_RFC3339, $post_id ); |
||
| 990 | $content_fields = self::parse_fields_from_content( $post_id ); |
||
| 991 | $md['feedback_ip'] = ( isset( $content_fields['_feedback_ip'] ) ) ? $content_fields['_feedback_ip'] : 0; |
||
| 992 | |||
| 993 | // add the email_marketing_consent to the post meta. |
||
| 994 | $md['email_marketing_consent'] = 0; |
||
| 995 | if ( isset( $content_fields['_feedback_all_fields'] ) ) { |
||
| 996 | $all_fields = $content_fields['_feedback_all_fields']; |
||
| 997 | // check if the email_marketing_consent field exists. |
||
| 998 | if ( isset( $all_fields['email_marketing_consent'] ) ) { |
||
| 999 | $md['email_marketing_consent'] = $all_fields['email_marketing_consent']; |
||
| 1000 | } |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | return $md; |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Get parsed feedback post fields. |
||
| 1008 | * |
||
| 1009 | * @param int $post_id Id of the post to fetch parsed contents for. |
||
| 1010 | * |
||
| 1011 | * @return array |
||
| 1012 | * |
||
| 1013 | * @codeCoverageIgnore - No need to be covered. |
||
| 1014 | */ |
||
| 1015 | public function get_parsed_field_contents_of_post( $post_id ) { |
||
| 1016 | return self::parse_fields_from_content( $post_id ); |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Properly maps fields that are missing from the post meta data |
||
| 1021 | * to names, that are similar to those of the post meta. |
||
| 1022 | * |
||
| 1023 | * @param array $parsed_post_content Parsed post content |
||
| 1024 | * |
||
| 1025 | * @see parse_fields_from_content for how the input data is generated. |
||
| 1026 | * |
||
| 1027 | * @return array Mapped fields. |
||
| 1028 | */ |
||
| 1029 | public function map_parsed_field_contents_of_post_to_field_names( $parsed_post_content ) { |
||
| 1030 | |||
| 1031 | $mapped_fields = array(); |
||
| 1032 | |||
| 1033 | $field_mapping = array( |
||
| 1034 | '_feedback_subject' => __( 'Contact Form', 'jetpack' ), |
||
| 1035 | '_feedback_author' => '1_Name', |
||
| 1036 | '_feedback_author_email' => '2_Email', |
||
| 1037 | '_feedback_author_url' => '3_Website', |
||
| 1038 | '_feedback_main_comment' => '4_Comment', |
||
| 1039 | '_feedback_author_ip' => '5_IP', |
||
| 1040 | ); |
||
| 1041 | |||
| 1042 | foreach ( $field_mapping as $parsed_field_name => $field_name ) { |
||
| 1043 | if ( |
||
| 1044 | isset( $parsed_post_content[ $parsed_field_name ] ) |
||
| 1045 | && ! empty( $parsed_post_content[ $parsed_field_name ] ) |
||
| 1046 | ) { |
||
| 1047 | $mapped_fields[ $field_name ] = $parsed_post_content[ $parsed_field_name ]; |
||
| 1048 | } |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | return $mapped_fields; |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Registers the personal data exporter. |
||
| 1056 | * |
||
| 1057 | * @since 6.1.1 |
||
| 1058 | * |
||
| 1059 | * @param array $exporters An array of personal data exporters. |
||
| 1060 | * |
||
| 1061 | * @return array $exporters An array of personal data exporters. |
||
| 1062 | */ |
||
| 1063 | public function register_personal_data_exporter( $exporters ) { |
||
| 1064 | $exporters['jetpack-feedback'] = array( |
||
| 1065 | 'exporter_friendly_name' => __( 'Feedback', 'jetpack' ), |
||
| 1066 | 'callback' => array( $this, 'personal_data_exporter' ), |
||
| 1067 | ); |
||
| 1068 | |||
| 1069 | return $exporters; |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Registers the personal data eraser. |
||
| 1074 | * |
||
| 1075 | * @since 6.1.1 |
||
| 1076 | * |
||
| 1077 | * @param array $erasers An array of personal data erasers. |
||
| 1078 | * |
||
| 1079 | * @return array $erasers An array of personal data erasers. |
||
| 1080 | */ |
||
| 1081 | public function register_personal_data_eraser( $erasers ) { |
||
| 1082 | $erasers['jetpack-feedback'] = array( |
||
| 1083 | 'eraser_friendly_name' => __( 'Feedback', 'jetpack' ), |
||
| 1084 | 'callback' => array( $this, 'personal_data_eraser' ), |
||
| 1085 | ); |
||
| 1086 | |||
| 1087 | return $erasers; |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Exports personal data. |
||
| 1092 | * |
||
| 1093 | * @since 6.1.1 |
||
| 1094 | * |
||
| 1095 | * @param string $email Email address. |
||
| 1096 | * @param int $page Page to export. |
||
| 1097 | * |
||
| 1098 | * @return array $return Associative array with keys expected by core. |
||
| 1099 | */ |
||
| 1100 | public function personal_data_exporter( $email, $page = 1 ) { |
||
| 1101 | return $this->_internal_personal_data_exporter( $email, $page ); |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Internal method for exporting personal data. |
||
| 1106 | * |
||
| 1107 | * Allows us to have a different signature than core expects |
||
| 1108 | * while protecting against future core API changes. |
||
| 1109 | * |
||
| 1110 | * @internal |
||
| 1111 | * @since 6.5 |
||
| 1112 | * |
||
| 1113 | * @param string $email Email address. |
||
| 1114 | * @param int $page Page to export. |
||
| 1115 | * @param int $per_page Number of feedbacks to process per page. Internal use only (testing) |
||
| 1116 | * |
||
| 1117 | * @return array Associative array with keys expected by core. |
||
| 1118 | */ |
||
| 1119 | public function _internal_personal_data_exporter( $email, $page = 1, $per_page = 250 ) { |
||
| 1120 | $export_data = array(); |
||
| 1121 | $post_ids = $this->personal_data_post_ids_by_email( $email, $per_page, $page ); |
||
| 1122 | |||
| 1123 | foreach ( $post_ids as $post_id ) { |
||
| 1124 | $post_fields = $this->get_parsed_field_contents_of_post( $post_id ); |
||
| 1125 | |||
| 1126 | if ( ! is_array( $post_fields ) || empty( $post_fields['_feedback_subject'] ) ) { |
||
| 1127 | continue; // Corrupt data. |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | $post_fields['_feedback_main_comment'] = $this->get_post_content_for_csv_export( $post_id ); |
||
| 1131 | $post_fields = $this->map_parsed_field_contents_of_post_to_field_names( $post_fields ); |
||
| 1132 | |||
| 1133 | if ( ! is_array( $post_fields ) || empty( $post_fields ) ) { |
||
| 1134 | continue; // No fields to export. |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | $post_meta = $this->get_post_meta_for_csv_export( $post_id ); |
||
| 1138 | $post_meta = is_array( $post_meta ) ? $post_meta : array(); |
||
| 1139 | |||
| 1140 | $post_export_data = array(); |
||
| 1141 | $post_data = array_merge( $post_fields, $post_meta ); |
||
| 1142 | ksort( $post_data ); |
||
| 1143 | |||
| 1144 | foreach ( $post_data as $post_data_key => $post_data_value ) { |
||
| 1145 | $post_export_data[] = array( |
||
| 1146 | 'name' => preg_replace( '/^[0-9]+_/', '', $post_data_key ), |
||
| 1147 | 'value' => $post_data_value, |
||
| 1148 | ); |
||
| 1149 | } |
||
| 1150 | |||
| 1151 | $export_data[] = array( |
||
| 1152 | 'group_id' => 'feedback', |
||
| 1153 | 'group_label' => __( 'Feedback', 'jetpack' ), |
||
| 1154 | 'item_id' => 'feedback-' . $post_id, |
||
| 1155 | 'data' => $post_export_data, |
||
| 1156 | ); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | return array( |
||
| 1160 | 'data' => $export_data, |
||
| 1161 | 'done' => count( $post_ids ) < $per_page, |
||
| 1162 | ); |
||
| 1163 | } |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Erases personal data. |
||
| 1167 | * |
||
| 1168 | * @since 6.1.1 |
||
| 1169 | * |
||
| 1170 | * @param string $email Email address. |
||
| 1171 | * @param int $page Page to erase. |
||
| 1172 | * |
||
| 1173 | * @return array Associative array with keys expected by core. |
||
| 1174 | */ |
||
| 1175 | public function personal_data_eraser( $email, $page = 1 ) { |
||
| 1176 | return $this->_internal_personal_data_eraser( $email, $page ); |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Internal method for erasing personal data. |
||
| 1181 | * |
||
| 1182 | * Allows us to have a different signature than core expects |
||
| 1183 | * while protecting against future core API changes. |
||
| 1184 | * |
||
| 1185 | * @internal |
||
| 1186 | * @since 6.5 |
||
| 1187 | * |
||
| 1188 | * @param string $email Email address. |
||
| 1189 | * @param int $page Page to erase. |
||
| 1190 | * @param int $per_page Number of feedbacks to process per page. Internal use only (testing) |
||
| 1191 | * |
||
| 1192 | * @return array Associative array with keys expected by core. |
||
| 1193 | */ |
||
| 1194 | public function _internal_personal_data_eraser( $email, $page = 1, $per_page = 250 ) { |
||
| 1195 | $removed = false; |
||
| 1196 | $retained = false; |
||
| 1197 | $messages = array(); |
||
| 1198 | $option_name = sprintf( '_jetpack_pde_feedback_%s', md5( $email ) ); |
||
| 1199 | $last_post_id = 1 === $page ? 0 : get_option( $option_name, 0 ); |
||
| 1200 | $post_ids = $this->personal_data_post_ids_by_email( $email, $per_page, $page, $last_post_id ); |
||
| 1201 | |||
| 1202 | foreach ( $post_ids as $post_id ) { |
||
| 1203 | /** |
||
| 1204 | * Filters whether to erase a particular Feedback post. |
||
| 1205 | * |
||
| 1206 | * @since 6.3.0 |
||
| 1207 | * |
||
| 1208 | * @param bool|string $prevention_message Whether to apply erase the Feedback post (bool). |
||
| 1209 | * Custom prevention message (string). Default true. |
||
| 1210 | * @param int $post_id Feedback post ID. |
||
| 1211 | */ |
||
| 1212 | $prevention_message = apply_filters( 'grunion_contact_form_delete_feedback_post', true, $post_id ); |
||
| 1213 | |||
| 1214 | if ( true !== $prevention_message ) { |
||
| 1215 | if ( $prevention_message && is_string( $prevention_message ) ) { |
||
| 1216 | $messages[] = esc_html( $prevention_message ); |
||
| 1217 | } else { |
||
| 1218 | $messages[] = sprintf( |
||
| 1219 | // translators: %d: Post ID. |
||
| 1220 | __( 'Feedback ID %d could not be removed at this time.', 'jetpack' ), |
||
| 1221 | $post_id |
||
| 1222 | ); |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | $retained = true; |
||
| 1226 | |||
| 1227 | continue; |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | if ( wp_delete_post( $post_id, true ) ) { |
||
| 1231 | $removed = true; |
||
| 1232 | } else { |
||
| 1233 | $retained = true; |
||
| 1234 | $messages[] = sprintf( |
||
| 1235 | // translators: %d: Post ID. |
||
| 1236 | __( 'Feedback ID %d could not be removed at this time.', 'jetpack' ), |
||
| 1237 | $post_id |
||
| 1238 | ); |
||
| 1239 | } |
||
| 1240 | } |
||
| 1241 | |||
| 1242 | $done = count( $post_ids ) < $per_page; |
||
| 1243 | |||
| 1244 | if ( $done ) { |
||
| 1245 | delete_option( $option_name ); |
||
| 1246 | } else { |
||
| 1247 | update_option( $option_name, (int) $post_id ); |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | return array( |
||
| 1251 | 'items_removed' => $removed, |
||
| 1252 | 'items_retained' => $retained, |
||
| 1253 | 'messages' => $messages, |
||
| 1254 | 'done' => $done, |
||
| 1255 | ); |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Queries personal data by email address. |
||
| 1260 | * |
||
| 1261 | * @since 6.1.1 |
||
| 1262 | * |
||
| 1263 | * @param string $email Email address. |
||
| 1264 | * @param int $per_page Post IDs per page. Default is `250`. |
||
| 1265 | * @param int $page Page to query. Default is `1`. |
||
| 1266 | * @param int $last_post_id Page to query. Default is `0`. If non-zero, used instead of $page. |
||
| 1267 | * |
||
| 1268 | * @return array An array of post IDs. |
||
| 1269 | */ |
||
| 1270 | public function personal_data_post_ids_by_email( $email, $per_page = 250, $page = 1, $last_post_id = 0 ) { |
||
| 1271 | add_filter( 'posts_search', array( $this, 'personal_data_search_filter' ) ); |
||
| 1272 | |||
| 1273 | $this->pde_last_post_id_erased = $last_post_id; |
||
| 1274 | $this->pde_email_address = $email; |
||
| 1275 | |||
| 1276 | $post_ids = get_posts( |
||
| 1277 | array( |
||
| 1278 | 'post_type' => 'feedback', |
||
| 1279 | 'post_status' => 'publish', |
||
| 1280 | // This search parameter gets overwritten in ->personal_data_search_filter() |
||
| 1281 | 's' => '..PDE..AUTHOR EMAIL:..PDE..', |
||
| 1282 | 'sentence' => true, |
||
| 1283 | 'order' => 'ASC', |
||
| 1284 | 'orderby' => 'ID', |
||
| 1285 | 'fields' => 'ids', |
||
| 1286 | 'posts_per_page' => $per_page, |
||
| 1287 | 'paged' => $last_post_id ? 1 : $page, |
||
| 1288 | 'suppress_filters' => false, |
||
| 1289 | ) |
||
| 1290 | ); |
||
| 1291 | |||
| 1292 | $this->pde_last_post_id_erased = 0; |
||
| 1293 | $this->pde_email_address = ''; |
||
| 1294 | |||
| 1295 | remove_filter( 'posts_search', array( $this, 'personal_data_search_filter' ) ); |
||
| 1296 | |||
| 1297 | return $post_ids; |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Filters searches by email address. |
||
| 1302 | * |
||
| 1303 | * @since 6.1.1 |
||
| 1304 | * |
||
| 1305 | * @param string $search SQL where clause. |
||
| 1306 | * |
||
| 1307 | * @return array Filtered SQL where clause. |
||
| 1308 | */ |
||
| 1309 | public function personal_data_search_filter( $search ) { |
||
| 1310 | global $wpdb; |
||
| 1311 | |||
| 1312 | /* |
||
| 1313 | * Limits search to `post_content` only, and we only match the |
||
| 1314 | * author's email address whenever it's on a line by itself. |
||
| 1315 | */ |
||
| 1316 | if ( $this->pde_email_address && false !== strpos( $search, '..PDE..AUTHOR EMAIL:..PDE..' ) ) { |
||
| 1317 | $search = $wpdb->prepare( |
||
| 1318 | " AND ( |
||
| 1319 | {$wpdb->posts}.post_content LIKE %s |
||
| 1320 | OR {$wpdb->posts}.post_content LIKE %s |
||
| 1321 | )", |
||
| 1322 | // `chr( 10 )` = `\n`, `chr( 13 )` = `\r` |
||
| 1323 | '%' . $wpdb->esc_like( chr( 10 ) . 'AUTHOR EMAIL: ' . $this->pde_email_address . chr( 10 ) ) . '%', |
||
| 1324 | '%' . $wpdb->esc_like( chr( 13 ) . 'AUTHOR EMAIL: ' . $this->pde_email_address . chr( 13 ) ) . '%' |
||
| 1325 | ); |
||
| 1326 | |||
| 1327 | if ( $this->pde_last_post_id_erased ) { |
||
| 1328 | $search .= $wpdb->prepare( " AND {$wpdb->posts}.ID > %d", $this->pde_last_post_id_erased ); |
||
| 1329 | } |
||
| 1330 | } |
||
| 1331 | |||
| 1332 | return $search; |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Prepares feedback post data for CSV export. |
||
| 1337 | * |
||
| 1338 | * @param array $post_ids Post IDs to fetch the data for. These need to be Feedback posts. |
||
| 1339 | * |
||
| 1340 | * @return array |
||
| 1341 | */ |
||
| 1342 | public function get_export_data_for_posts( $post_ids ) { |
||
| 1343 | |||
| 1344 | $posts_data = array(); |
||
| 1345 | $field_names = array(); |
||
| 1346 | $result = array(); |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Fetch posts and get the possible field names for later use |
||
| 1350 | */ |
||
| 1351 | foreach ( $post_ids as $post_id ) { |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Fetch post main data, because we need the subject and author data for the feedback form. |
||
| 1355 | */ |
||
| 1356 | $post_real_data = $this->get_parsed_field_contents_of_post( $post_id ); |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * If `$post_real_data` is not an array or there is no `_feedback_subject` set, |
||
| 1360 | * then something must be wrong with the feedback post. Skip it. |
||
| 1361 | */ |
||
| 1362 | if ( ! is_array( $post_real_data ) || ! isset( $post_real_data['_feedback_subject'] ) ) { |
||
| 1363 | continue; |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Fetch main post comment. This is from the default textarea fields. |
||
| 1368 | * If it is non-empty, then we add it to data, otherwise skip it. |
||
| 1369 | */ |
||
| 1370 | $post_comment_content = $this->get_post_content_for_csv_export( $post_id ); |
||
| 1371 | if ( ! empty( $post_comment_content ) ) { |
||
| 1372 | $post_real_data['_feedback_main_comment'] = $post_comment_content; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Map parsed fields to proper field names |
||
| 1377 | */ |
||
| 1378 | $mapped_fields = $this->map_parsed_field_contents_of_post_to_field_names( $post_real_data ); |
||
| 1379 | |||
| 1380 | /** |
||
| 1381 | * Fetch post meta data. |
||
| 1382 | */ |
||
| 1383 | $post_meta_data = $this->get_post_meta_for_csv_export( $post_id ); |
||
| 1384 | |||
| 1385 | /** |
||
| 1386 | * If `$post_meta_data` is not an array or if it is empty, then there is no |
||
| 1387 | * extra feedback to work with. Create an empty array. |
||
| 1388 | */ |
||
| 1389 | if ( ! is_array( $post_meta_data ) || empty( $post_meta_data ) ) { |
||
| 1390 | $post_meta_data = array(); |
||
| 1391 | } |
||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Prepend the feedback subject to the list of fields. |
||
| 1395 | */ |
||
| 1396 | $post_meta_data = array_merge( |
||
| 1397 | $mapped_fields, |
||
| 1398 | $post_meta_data |
||
| 1399 | ); |
||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Save post metadata for later usage. |
||
| 1403 | */ |
||
| 1404 | $posts_data[ $post_id ] = $post_meta_data; |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Save field names, so we can use them as header fields later in the CSV. |
||
| 1408 | */ |
||
| 1409 | $field_names = array_merge( $field_names, array_keys( $post_meta_data ) ); |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Make sure the field names are unique, because we don't want duplicate data. |
||
| 1414 | */ |
||
| 1415 | $field_names = array_unique( $field_names ); |
||
| 1416 | |||
| 1417 | /** |
||
| 1418 | * Sort the field names by the field id number |
||
| 1419 | */ |
||
| 1420 | sort( $field_names, SORT_NUMERIC ); |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * Loop through every post, which is essentially CSV row. |
||
| 1424 | */ |
||
| 1425 | foreach ( $posts_data as $post_id => $single_post_data ) { |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Go through all the possible fields and check if the field is available |
||
| 1429 | * in the current post. |
||
| 1430 | * |
||
| 1431 | * If it is - add the data as a value. |
||
| 1432 | * If it is not - add an empty string, which is just a placeholder in the CSV. |
||
| 1433 | */ |
||
| 1434 | foreach ( $field_names as $single_field_name ) { |
||
| 1435 | if ( |
||
| 1436 | isset( $single_post_data[ $single_field_name ] ) |
||
| 1437 | && ! empty( $single_post_data[ $single_field_name ] ) |
||
| 1438 | ) { |
||
| 1439 | $result[ $single_field_name ][] = trim( $single_post_data[ $single_field_name ] ); |
||
| 1440 | } else { |
||
| 1441 | $result[ $single_field_name ][] = ''; |
||
| 1442 | } |
||
| 1443 | } |
||
| 1444 | } |
||
| 1445 | |||
| 1446 | return $result; |
||
| 1447 | } |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * download as a csv a contact form or all of them in a csv file |
||
| 1451 | */ |
||
| 1452 | function download_feedback_as_csv() { |
||
| 1453 | if ( empty( $_POST['feedback_export_nonce'] ) ) { |
||
| 1454 | return; |
||
| 1455 | } |
||
| 1456 | |||
| 1457 | check_admin_referer( 'feedback_export', 'feedback_export_nonce' ); |
||
| 1458 | |||
| 1459 | if ( ! current_user_can( 'export' ) ) { |
||
| 1460 | return; |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | $args = array( |
||
| 1464 | 'posts_per_page' => -1, |
||
| 1465 | 'post_type' => 'feedback', |
||
| 1466 | 'post_status' => 'publish', |
||
| 1467 | 'order' => 'ASC', |
||
| 1468 | 'fields' => 'ids', |
||
| 1469 | 'suppress_filters' => false, |
||
| 1470 | ); |
||
| 1471 | |||
| 1472 | $filename = date( 'Y-m-d' ) . '-feedback-export.csv'; |
||
| 1473 | |||
| 1474 | // Check if we want to download all the feedbacks or just a certain contact form |
||
| 1475 | if ( ! empty( $_POST['post'] ) && $_POST['post'] !== 'all' ) { |
||
| 1476 | $args['post_parent'] = (int) $_POST['post']; |
||
| 1477 | $filename = date( 'Y-m-d' ) . '-' . str_replace( ' ', '-', get_the_title( (int) $_POST['post'] ) ) . '.csv'; |
||
| 1478 | } |
||
| 1479 | |||
| 1480 | $feedbacks = get_posts( $args ); |
||
| 1481 | |||
| 1482 | if ( empty( $feedbacks ) ) { |
||
| 1483 | return; |
||
| 1484 | } |
||
| 1485 | |||
| 1486 | $filename = sanitize_file_name( $filename ); |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Prepare data for export. |
||
| 1490 | */ |
||
| 1491 | $data = $this->get_export_data_for_posts( $feedbacks ); |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * If `$data` is empty, there's nothing we can do below. |
||
| 1495 | */ |
||
| 1496 | if ( ! is_array( $data ) || empty( $data ) ) { |
||
| 1497 | return; |
||
| 1498 | } |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Extract field names from `$data` for later use. |
||
| 1502 | */ |
||
| 1503 | $fields = array_keys( $data ); |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Count how many rows will be exported. |
||
| 1507 | */ |
||
| 1508 | $row_count = count( reset( $data ) ); |
||
| 1509 | |||
| 1510 | // Forces the download of the CSV instead of echoing |
||
| 1511 | header( 'Content-Disposition: attachment; filename=' . $filename ); |
||
| 1512 | header( 'Pragma: no-cache' ); |
||
| 1513 | header( 'Expires: 0' ); |
||
| 1514 | header( 'Content-Type: text/csv; charset=utf-8' ); |
||
| 1515 | |||
| 1516 | $output = fopen( 'php://output', 'w' ); |
||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * Print CSV headers |
||
| 1520 | */ |
||
| 1521 | fputcsv( $output, $fields ); |
||
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Print rows to the output. |
||
| 1525 | */ |
||
| 1526 | for ( $i = 0; $i < $row_count; $i ++ ) { |
||
| 1527 | |||
| 1528 | $current_row = array(); |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Put all the fields in `$current_row` array. |
||
| 1532 | */ |
||
| 1533 | foreach ( $fields as $single_field_name ) { |
||
| 1534 | $current_row[] = $this->esc_csv( $data[ $single_field_name ][ $i ] ); |
||
| 1535 | } |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * Output the complete CSV row |
||
| 1539 | */ |
||
| 1540 | fputcsv( $output, $current_row ); |
||
| 1541 | } |
||
| 1542 | |||
| 1543 | fclose( $output ); |
||
| 1544 | } |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Escape a string to be used in a CSV context |
||
| 1548 | * |
||
| 1549 | * Malicious input can inject formulas into CSV files, opening up the possibility for phishing attacks and |
||
| 1550 | * disclosure of sensitive information. |
||
| 1551 | * |
||
| 1552 | * Additionally, Excel exposes the ability to launch arbitrary commands through the DDE protocol. |
||
| 1553 | * |
||
| 1554 | * @see https://www.contextis.com/en/blog/comma-separated-vulnerabilities |
||
| 1555 | * |
||
| 1556 | * @param string $field |
||
| 1557 | * |
||
| 1558 | * @return string |
||
| 1559 | */ |
||
| 1560 | public function esc_csv( $field ) { |
||
| 1561 | $active_content_triggers = array( '=', '+', '-', '@' ); |
||
| 1562 | |||
| 1563 | if ( in_array( mb_substr( $field, 0, 1 ), $active_content_triggers, true ) ) { |
||
| 1564 | $field = "'" . $field; |
||
| 1565 | } |
||
| 1566 | |||
| 1567 | return $field; |
||
| 1568 | } |
||
| 1569 | |||
| 1570 | /** |
||
| 1571 | * Returns a string of HTML <option> items from an array of posts |
||
| 1572 | * |
||
| 1573 | * @return string a string of HTML <option> items |
||
| 1574 | */ |
||
| 1575 | protected function get_feedbacks_as_options() { |
||
| 1576 | $options = ''; |
||
| 1577 | |||
| 1578 | // Get the feedbacks' parents' post IDs |
||
| 1579 | $feedbacks = get_posts( |
||
| 1580 | array( |
||
| 1581 | 'fields' => 'id=>parent', |
||
| 1582 | 'posts_per_page' => 100000, |
||
| 1583 | 'post_type' => 'feedback', |
||
| 1584 | 'post_status' => 'publish', |
||
| 1585 | 'suppress_filters' => false, |
||
| 1586 | ) |
||
| 1587 | ); |
||
| 1588 | $parents = array_unique( array_values( $feedbacks ) ); |
||
| 1589 | |||
| 1590 | $posts = get_posts( |
||
| 1591 | array( |
||
| 1592 | 'orderby' => 'ID', |
||
| 1593 | 'posts_per_page' => 1000, |
||
| 1594 | 'post_type' => 'any', |
||
| 1595 | 'post__in' => array_values( $parents ), |
||
| 1596 | 'suppress_filters' => false, |
||
| 1597 | ) |
||
| 1598 | ); |
||
| 1599 | |||
| 1600 | // creates the string of <option> elements |
||
| 1601 | foreach ( $posts as $post ) { |
||
| 1602 | $options .= sprintf( '<option value="%s">%s</option>', esc_attr( $post->ID ), esc_html( $post->post_title ) ); |
||
| 1603 | } |
||
| 1604 | |||
| 1605 | return $options; |
||
| 1606 | } |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Get the names of all the form's fields |
||
| 1610 | * |
||
| 1611 | * @param array|int $posts the post we want the fields of |
||
| 1612 | * |
||
| 1613 | * @return array the array of fields |
||
| 1614 | * |
||
| 1615 | * @deprecated As this is no longer necessary as of the CSV export rewrite. - 2015-12-29 |
||
| 1616 | */ |
||
| 1617 | protected function get_field_names( $posts ) { |
||
| 1618 | $posts = (array) $posts; |
||
| 1619 | $all_fields = array(); |
||
| 1620 | |||
| 1621 | foreach ( $posts as $post ) { |
||
| 1622 | $fields = self::parse_fields_from_content( $post ); |
||
| 1623 | |||
| 1624 | if ( isset( $fields['_feedback_all_fields'] ) ) { |
||
| 1625 | $extra_fields = array_keys( $fields['_feedback_all_fields'] ); |
||
| 1626 | $all_fields = array_merge( $all_fields, $extra_fields ); |
||
| 1627 | } |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | $all_fields = array_unique( $all_fields ); |
||
| 1631 | return $all_fields; |
||
| 1632 | } |
||
| 1633 | |||
| 1634 | public static function parse_fields_from_content( $post_id ) { |
||
| 1635 | static $post_fields; |
||
| 1636 | |||
| 1637 | if ( ! is_array( $post_fields ) ) { |
||
| 1638 | $post_fields = array(); |
||
| 1639 | } |
||
| 1640 | |||
| 1641 | if ( isset( $post_fields[ $post_id ] ) ) { |
||
| 1642 | return $post_fields[ $post_id ]; |
||
| 1643 | } |
||
| 1644 | |||
| 1645 | $all_values = array(); |
||
| 1646 | $post_content = get_post_field( 'post_content', $post_id ); |
||
| 1647 | $content = explode( '<!--more-->', $post_content ); |
||
| 1648 | $lines = array(); |
||
| 1649 | |||
| 1650 | if ( count( $content ) > 1 ) { |
||
| 1651 | $content = str_ireplace( array( '<br />', ')</p>' ), '', $content[1] ); |
||
| 1652 | $one_line = preg_replace( '/\s+/', ' ', $content ); |
||
| 1653 | $one_line = preg_replace( '/.*Array \( (.*)\)/', '$1', $one_line ); |
||
| 1654 | |||
| 1655 | preg_match_all( '/\[([^\]]+)\] =\>\; ([^\[]+)/', $one_line, $matches ); |
||
| 1656 | |||
| 1657 | if ( count( $matches ) > 1 ) { |
||
| 1658 | $all_values = array_combine( array_map( 'trim', $matches[1] ), array_map( 'trim', $matches[2] ) ); |
||
| 1659 | } |
||
| 1660 | |||
| 1661 | $lines = array_filter( explode( "\n", $content ) ); |
||
| 1662 | } |
||
| 1663 | |||
| 1664 | $var_map = array( |
||
| 1665 | 'AUTHOR' => '_feedback_author', |
||
| 1666 | 'AUTHOR EMAIL' => '_feedback_author_email', |
||
| 1667 | 'AUTHOR URL' => '_feedback_author_url', |
||
| 1668 | 'SUBJECT' => '_feedback_subject', |
||
| 1669 | 'IP' => '_feedback_ip', |
||
| 1670 | ); |
||
| 1671 | |||
| 1672 | $fields = array(); |
||
| 1673 | |||
| 1674 | foreach ( $lines as $line ) { |
||
| 1675 | $vars = explode( ': ', $line, 2 ); |
||
| 1676 | if ( ! empty( $vars ) ) { |
||
| 1677 | if ( isset( $var_map[ $vars[0] ] ) ) { |
||
| 1678 | $fields[ $var_map[ $vars[0] ] ] = self::strip_tags( trim( $vars[1] ) ); |
||
| 1679 | } |
||
| 1680 | } |
||
| 1681 | } |
||
| 1682 | |||
| 1683 | $fields['_feedback_all_fields'] = $all_values; |
||
| 1684 | |||
| 1685 | $post_fields[ $post_id ] = $fields; |
||
| 1686 | |||
| 1687 | return $fields; |
||
| 1688 | } |
||
| 1689 | |||
| 1690 | /** |
||
| 1691 | * Creates a valid csv row from a post id |
||
| 1692 | * |
||
| 1693 | * @param int $post_id The id of the post |
||
| 1694 | * @param array $fields An array containing the names of all the fields of the csv |
||
| 1695 | * @return String The csv row |
||
| 1696 | * |
||
| 1697 | * @deprecated This is no longer needed, as of the CSV export rewrite. |
||
| 1698 | */ |
||
| 1699 | protected static function make_csv_row_from_feedback( $post_id, $fields ) { |
||
| 1700 | $content_fields = self::parse_fields_from_content( $post_id ); |
||
| 1701 | $all_fields = array(); |
||
| 1702 | |||
| 1703 | if ( isset( $content_fields['_feedback_all_fields'] ) ) { |
||
| 1704 | $all_fields = $content_fields['_feedback_all_fields']; |
||
| 1705 | } |
||
| 3893 |