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