grunion-contact-form.php 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086
  1. <?php
  2. /*
  3. Plugin Name: Grunion Contact Form
  4. Description: Add a contact form to any post, page or text widget. Emails will be sent to the post's author by default, or any email address you choose. As seen on WordPress.com.
  5. Plugin URI: http://automattic.com/#
  6. AUthor: Automattic, Inc.
  7. Author URI: http://automattic.com/
  8. Version: 2.4
  9. License: GPLv2 or later
  10. */
  11. define( 'GRUNION_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
  12. define( 'GRUNION_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  13. if ( is_admin() ) {
  14. require_once GRUNION_PLUGIN_DIR . 'admin.php';
  15. }
  16. add_action( 'rest_api_init', 'grunion_contact_form_require_endpoint' );
  17. function grunion_contact_form_require_endpoint() {
  18. require_once GRUNION_PLUGIN_DIR . 'class-grunion-contact-form-endpoint.php';
  19. }
  20. /**
  21. * Sets up various actions, filters, post types, post statuses, shortcodes.
  22. */
  23. class Grunion_Contact_Form_Plugin {
  24. /**
  25. * @var string The Widget ID of the widget currently being processed. Used to build the unique contact-form ID for forms embedded in widgets.
  26. */
  27. public $current_widget_id;
  28. static $using_contact_form_field = false;
  29. /**
  30. * @var int The last Feedback Post ID Erased as part of the Personal Data Eraser.
  31. * Helps with pagination.
  32. */
  33. private $pde_last_post_id_erased = 0;
  34. /**
  35. * @var string The email address for which we are deleting/exporting all feedbacks
  36. * as part of a Personal Data Eraser or Personal Data Exporter request.
  37. */
  38. private $pde_email_address = '';
  39. static function init() {
  40. static $instance = false;
  41. if ( ! $instance ) {
  42. $instance = new Grunion_Contact_Form_Plugin;
  43. // Schedule our daily cleanup
  44. add_action( 'wp_scheduled_delete', array( $instance, 'daily_akismet_meta_cleanup' ) );
  45. }
  46. return $instance;
  47. }
  48. /**
  49. * Runs daily to clean up spam detection metadata after 15 days. Keeps your DB squeaky clean.
  50. */
  51. public function daily_akismet_meta_cleanup() {
  52. global $wpdb;
  53. $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" );
  54. if ( empty( $feedback_ids ) ) {
  55. return;
  56. }
  57. /**
  58. * Fires right before deleting the _feedback_akismet_values post meta on $feedback_ids
  59. *
  60. * @module contact-form
  61. *
  62. * @since 6.1.0
  63. *
  64. * @param array $feedback_ids list of feedback post ID
  65. */
  66. do_action( 'jetpack_daily_akismet_meta_cleanup_before', $feedback_ids );
  67. foreach ( $feedback_ids as $feedback_id ) {
  68. delete_post_meta( $feedback_id, '_feedback_akismet_values' );
  69. }
  70. /**
  71. * Fires right after deleting the _feedback_akismet_values post meta on $feedback_ids
  72. *
  73. * @module contact-form
  74. *
  75. * @since 6.1.0
  76. *
  77. * @param array $feedback_ids list of feedback post ID
  78. */
  79. do_action( 'jetpack_daily_akismet_meta_cleanup_after', $feedback_ids );
  80. }
  81. /**
  82. * Strips HTML tags from input. Output is NOT HTML safe.
  83. *
  84. * @param mixed $data_with_tags
  85. * @return mixed
  86. */
  87. public static function strip_tags( $data_with_tags ) {
  88. if ( is_array( $data_with_tags ) ) {
  89. foreach ( $data_with_tags as $index => $value ) {
  90. $index = sanitize_text_field( strval( $index ) );
  91. $value = wp_kses( strval( $value ), array() );
  92. $value = str_replace( '&amp;', '&', $value ); // undo damage done by wp_kses_normalize_entities()
  93. $data_without_tags[ $index ] = $value;
  94. }
  95. } else {
  96. $data_without_tags = wp_kses( $data_with_tags, array() );
  97. $data_without_tags = str_replace( '&amp;', '&', $data_without_tags ); // undo damage done by wp_kses_normalize_entities()
  98. }
  99. return $data_without_tags;
  100. }
  101. function __construct() {
  102. $this->add_shortcode();
  103. // While generating the output of a text widget with a contact-form shortcode, we need to know its widget ID.
  104. add_action( 'dynamic_sidebar', array( $this, 'track_current_widget' ) );
  105. // Add a "widget" shortcode attribute to all contact-form shortcodes embedded in widgets
  106. add_filter( 'widget_text', array( $this, 'widget_atts' ), 0 );
  107. // If Text Widgets don't get shortcode processed, hack ours into place.
  108. if (
  109. version_compare( get_bloginfo( 'version' ), '4.9-z', '<=' )
  110. && ! has_filter( 'widget_text', 'do_shortcode' )
  111. ) {
  112. add_filter( 'widget_text', array( $this, 'widget_shortcode_hack' ), 5 );
  113. }
  114. // Akismet to the rescue
  115. if ( defined( 'AKISMET_VERSION' ) || function_exists( 'akismet_http_post' ) ) {
  116. add_filter( 'jetpack_contact_form_is_spam', array( $this, 'is_spam_akismet' ), 10, 2 );
  117. add_action( 'contact_form_akismet', array( $this, 'akismet_submit' ), 10, 2 );
  118. }
  119. add_action( 'loop_start', array( 'Grunion_Contact_Form', '_style_on' ) );
  120. add_action( 'wp_ajax_grunion-contact-form', array( $this, 'ajax_request' ) );
  121. add_action( 'wp_ajax_nopriv_grunion-contact-form', array( $this, 'ajax_request' ) );
  122. // GDPR: personal data exporter & eraser.
  123. add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ) );
  124. add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_personal_data_eraser' ) );
  125. // Export to CSV feature
  126. if ( is_admin() ) {
  127. add_action( 'admin_init', array( $this, 'download_feedback_as_csv' ) );
  128. add_action( 'admin_footer-edit.php', array( $this, 'export_form' ) );
  129. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  130. add_action( 'current_screen', array( $this, 'unread_count' ) );
  131. }
  132. // custom post type we'll use to keep copies of the feedback items
  133. register_post_type( 'feedback', array(
  134. 'labels' => array(
  135. 'name' => __( 'Feedback', 'jetpack' ),
  136. 'singular_name' => __( 'Feedback', 'jetpack' ),
  137. 'search_items' => __( 'Search Feedback', 'jetpack' ),
  138. 'not_found' => __( 'No feedback found', 'jetpack' ),
  139. 'not_found_in_trash' => __( 'No feedback found', 'jetpack' ),
  140. ),
  141. 'menu_icon' => 'dashicons-feedback',
  142. 'show_ui' => TRUE,
  143. 'show_in_admin_bar' => FALSE,
  144. 'public' => FALSE,
  145. 'rewrite' => FALSE,
  146. 'query_var' => FALSE,
  147. 'capability_type' => 'page',
  148. 'show_in_rest' => true,
  149. 'rest_controller_class' => 'Grunion_Contact_Form_Endpoint',
  150. 'capabilities' => array(
  151. 'create_posts' => false,
  152. 'publish_posts' => 'publish_pages',
  153. 'edit_posts' => 'edit_pages',
  154. 'edit_others_posts' => 'edit_others_pages',
  155. 'delete_posts' => 'delete_pages',
  156. 'delete_others_posts' => 'delete_others_pages',
  157. 'read_private_posts' => 'read_private_pages',
  158. 'edit_post' => 'edit_page',
  159. 'delete_post' => 'delete_page',
  160. 'read_post' => 'read_page',
  161. ),
  162. 'map_meta_cap' => true,
  163. ) );
  164. // Add to REST API post type whitelist
  165. add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_feedback_rest_api_type' ) );
  166. // Add "spam" as a post status
  167. register_post_status( 'spam', array(
  168. 'label' => 'Spam',
  169. 'public' => false,
  170. 'exclude_from_search' => true,
  171. 'show_in_admin_all_list' => false,
  172. 'label_count' => _n_noop( 'Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>', 'jetpack' ),
  173. 'protected' => true,
  174. '_builtin' => false,
  175. ) );
  176. // POST handler
  177. if (
  178. isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] )
  179. &&
  180. isset( $_POST['action'] ) && 'grunion-contact-form' == $_POST['action']
  181. &&
  182. isset( $_POST['contact-form-id'] )
  183. ) {
  184. add_action( 'template_redirect', array( $this, 'process_form_submission' ) );
  185. }
  186. /*
  187. Can be dequeued by placing the following in wp-content/themes/yourtheme/functions.php
  188. *
  189. * function remove_grunion_style() {
  190. * wp_deregister_style('grunion.css');
  191. * }
  192. * add_action('wp_print_styles', 'remove_grunion_style');
  193. */
  194. wp_register_style( 'grunion.css', GRUNION_PLUGIN_URL . 'css/grunion.css', array(), JETPACK__VERSION );
  195. wp_style_add_data( 'grunion.css', 'rtl', 'replace' );
  196. }
  197. /**
  198. * Add the 'Export' menu item as a submenu of Feedback.
  199. */
  200. public function admin_menu() {
  201. add_submenu_page(
  202. 'edit.php?post_type=feedback',
  203. __( 'Export feedback as CSV', 'jetpack' ),
  204. __( 'Export CSV', 'jetpack' ),
  205. 'export',
  206. 'feedback-export',
  207. array( $this, 'export_form' )
  208. );
  209. }
  210. /**
  211. * Add to REST API post type whitelist
  212. */
  213. function allow_feedback_rest_api_type( $post_types ) {
  214. $post_types[] = 'feedback';
  215. return $post_types;
  216. }
  217. /**
  218. * Display the count of new feedback entries received. It's reset when user visits the Feedback screen.
  219. *
  220. * @since 4.1.0
  221. *
  222. * @param object $screen Information about the current screen.
  223. */
  224. function unread_count( $screen ) {
  225. if ( isset( $screen->post_type ) && 'feedback' == $screen->post_type ) {
  226. update_option( 'feedback_unread_count', 0 );
  227. } else {
  228. global $menu;
  229. if ( isset( $menu ) && is_array( $menu ) && ! empty( $menu ) ) {
  230. foreach ( $menu as $index => $menu_item ) {
  231. if ( 'edit.php?post_type=feedback' == $menu_item[2] ) {
  232. $unread = get_option( 'feedback_unread_count', 0 );
  233. if ( $unread > 0 ) {
  234. $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>' : '';
  235. $menu[ $index ][0] .= $unread_count;
  236. }
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. /**
  244. * Handles all contact-form POST submissions
  245. *
  246. * Conditionally attached to `template_redirect`
  247. */
  248. function process_form_submission() {
  249. // Add a filter to replace tokens in the subject field with sanitized field values
  250. add_filter( 'contact_form_subject', array( $this, 'replace_tokens_with_input' ), 10, 2 );
  251. $id = stripslashes( $_POST['contact-form-id'] );
  252. $hash = isset( $_POST['contact-form-hash'] ) ? $_POST['contact-form-hash'] : null;
  253. $hash = preg_replace( '/[^\da-f]/i', '', $hash );
  254. if ( is_user_logged_in() ) {
  255. check_admin_referer( "contact-form_{$id}" );
  256. }
  257. $is_widget = 0 === strpos( $id, 'widget-' );
  258. $form = false;
  259. if ( $is_widget ) {
  260. // It's a form embedded in a text widget
  261. $this->current_widget_id = substr( $id, 7 ); // remove "widget-"
  262. $widget_type = implode( '-', array_slice( explode( '-', $this->current_widget_id ), 0, -1 ) ); // Remove trailing -#
  263. // Is the widget active?
  264. $sidebar = is_active_widget( false, $this->current_widget_id, $widget_type );
  265. // This is lame - no core API for getting a widget by ID
  266. $widget = isset( $GLOBALS['wp_registered_widgets'][ $this->current_widget_id ] ) ? $GLOBALS['wp_registered_widgets'][ $this->current_widget_id ] : false;
  267. if ( $sidebar && $widget && isset( $widget['callback'] ) ) {
  268. // prevent PHP notices by populating widget args
  269. $widget_args = array(
  270. 'before_widget' => '',
  271. 'after_widget' => '',
  272. 'before_title' => '',
  273. 'after_title' => '',
  274. );
  275. // This is lamer - no API for outputting a given widget by ID
  276. ob_start();
  277. // Process the widget to populate Grunion_Contact_Form::$last
  278. call_user_func( $widget['callback'], $widget_args, $widget['params'][0] );
  279. ob_end_clean();
  280. }
  281. } else {
  282. // It's a form embedded in a post
  283. $post = get_post( $id );
  284. // Process the content to populate Grunion_Contact_Form::$last
  285. /** This filter is already documented in core. wp-includes/post-template.php */
  286. apply_filters( 'the_content', $post->post_content );
  287. }
  288. $form = isset( Grunion_Contact_Form::$forms[ $hash ] ) ? Grunion_Contact_Form::$forms[ $hash ] : null;
  289. // No form may mean user is using do_shortcode, grab the form using the stored post meta
  290. if ( ! $form ) {
  291. // Get shortcode from post meta
  292. $shortcode = get_post_meta( $_POST['contact-form-id'], "_g_feedback_shortcode_{$hash}", true );
  293. // Format it
  294. if ( $shortcode != '' ) {
  295. // Get attributes from post meta.
  296. $parameters = '';
  297. $attributes = get_post_meta( $_POST['contact-form-id'], "_g_feedback_shortcode_atts_{$hash}", true );
  298. if ( ! empty( $attributes ) && is_array( $attributes ) ) {
  299. foreach( array_filter( $attributes ) as $param => $value ) {
  300. $parameters .= " $param=\"$value\"";
  301. }
  302. }
  303. $shortcode = '[contact-form' . $parameters . ']' . $shortcode . '[/contact-form]';
  304. do_shortcode( $shortcode );
  305. // Recreate form
  306. $form = Grunion_Contact_Form::$last;
  307. }
  308. if ( ! $form ) {
  309. return false;
  310. }
  311. }
  312. if ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) {
  313. return $form->errors;
  314. }
  315. // Process the form
  316. return $form->process_submission();
  317. }
  318. function ajax_request() {
  319. $submission_result = self::process_form_submission();
  320. if ( ! $submission_result ) {
  321. header( 'HTTP/1.1 500 Server Error', 500, true );
  322. echo '<div class="form-error"><ul class="form-errors"><li class="form-error-message">';
  323. esc_html_e( 'An error occurred. Please try again later.', 'jetpack' );
  324. echo '</li></ul></div>';
  325. } elseif ( is_wp_error( $submission_result ) ) {
  326. header( 'HTTP/1.1 400 Bad Request', 403, true );
  327. echo '<div class="form-error"><ul class="form-errors"><li class="form-error-message">';
  328. echo esc_html( $submission_result->get_error_message() );
  329. echo '</li></ul></div>';
  330. } else {
  331. echo '<h3>' . esc_html__( 'Message Sent', 'jetpack' ) . '</h3>' . $submission_result;
  332. }
  333. die;
  334. }
  335. /**
  336. * Ensure the post author is always zero for contact-form feedbacks
  337. * Attached to `wp_insert_post_data`
  338. *
  339. * @see Grunion_Contact_Form::process_submission()
  340. *
  341. * @param array $data the data to insert
  342. * @param array $postarr the data sent to wp_insert_post()
  343. * @return array The filtered $data to insert
  344. */
  345. function insert_feedback_filter( $data, $postarr ) {
  346. if ( $data['post_type'] == 'feedback' && $postarr['post_type'] == 'feedback' ) {
  347. $data['post_author'] = 0;
  348. }
  349. return $data;
  350. }
  351. /*
  352. * Adds our contact-form shortcode
  353. * The "child" contact-field shortcode is enabled as needed by the contact-form shortcode handler
  354. */
  355. function add_shortcode() {
  356. add_shortcode( 'contact-form', array( 'Grunion_Contact_Form', 'parse' ) );
  357. add_shortcode( 'contact-field', array( 'Grunion_Contact_Form', 'parse_contact_field' ) );
  358. }
  359. static function tokenize_label( $label ) {
  360. return '{' . trim( preg_replace( '#^\d+_#', '', $label ) ) . '}';
  361. }
  362. static function sanitize_value( $value ) {
  363. return preg_replace( '=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $value );
  364. }
  365. /**
  366. * Replaces tokens like {city} or {City} (case insensitive) with the value
  367. * of an input field of that name
  368. *
  369. * @param string $subject
  370. * @param array $field_values Array with field label => field value associations
  371. *
  372. * @return string The filtered $subject with the tokens replaced
  373. */
  374. function replace_tokens_with_input( $subject, $field_values ) {
  375. // Wrap labels into tokens (inside {})
  376. $wrapped_labels = array_map( array( 'Grunion_Contact_Form_Plugin', 'tokenize_label' ), array_keys( $field_values ) );
  377. // Sanitize all values
  378. $sanitized_values = array_map( array( 'Grunion_Contact_Form_Plugin', 'sanitize_value' ), array_values( $field_values ) );
  379. foreach ( $sanitized_values as $k => $sanitized_value ) {
  380. if ( is_array( $sanitized_value ) ) {
  381. $sanitized_values[ $k ] = implode( ', ', $sanitized_value );
  382. }
  383. }
  384. // Search for all valid tokens (based on existing fields) and replace with the field's value
  385. $subject = str_ireplace( $wrapped_labels, $sanitized_values, $subject );
  386. return $subject;
  387. }
  388. /**
  389. * Tracks the widget currently being processed.
  390. * Attached to `dynamic_sidebar`
  391. *
  392. * @see $current_widget_id
  393. *
  394. * @param array $widget The widget data
  395. */
  396. function track_current_widget( $widget ) {
  397. $this->current_widget_id = $widget['id'];
  398. }
  399. /**
  400. * Adds a "widget" attribute to every contact-form embedded in a text widget.
  401. * Used to tell the difference between post-embedded contact-forms and widget-embedded contact-forms
  402. * Attached to `widget_text`
  403. *
  404. * @param string $text The widget text
  405. * @return string The filtered widget text
  406. */
  407. function widget_atts( $text ) {
  408. Grunion_Contact_Form::style( true );
  409. return preg_replace( '/\[contact-form([^a-zA-Z_-])/', '[contact-form widget="' . $this->current_widget_id . '"\\1', $text );
  410. }
  411. /**
  412. * For sites where text widgets are not processed for shortcodes, we add this hack to process just our shortcode
  413. * Attached to `widget_text`
  414. *
  415. * @param string $text The widget text
  416. * @return string The contact-form filtered widget text
  417. */
  418. function widget_shortcode_hack( $text ) {
  419. if ( ! preg_match( '/\[contact-form([^a-zA-Z_-])/', $text ) ) {
  420. return $text;
  421. }
  422. $old = $GLOBALS['shortcode_tags'];
  423. remove_all_shortcodes();
  424. Grunion_Contact_Form_Plugin::$using_contact_form_field = true;
  425. $this->add_shortcode();
  426. $text = do_shortcode( $text );
  427. Grunion_Contact_Form_Plugin::$using_contact_form_field = false;
  428. $GLOBALS['shortcode_tags'] = $old;
  429. return $text;
  430. }
  431. /**
  432. * Populate an array with all values necessary to submit a NEW contact-form feedback to Akismet.
  433. * Note that this includes the current user_ip etc, so this should only be called when accepting a new item via $_POST
  434. *
  435. * @param array $form Contact form feedback array
  436. * @return array feedback array with additional data ready for submission to Akismet
  437. */
  438. function prepare_for_akismet( $form ) {
  439. $form['comment_type'] = 'contact_form';
  440. $form['user_ip'] = $_SERVER['REMOTE_ADDR'];
  441. $form['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  442. $form['referrer'] = $_SERVER['HTTP_REFERER'];
  443. $form['blog'] = get_option( 'home' );
  444. foreach ( $_SERVER as $key => $value ) {
  445. if ( ! is_string( $value ) ) {
  446. continue;
  447. }
  448. if ( in_array( $key, array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'HTTP_USER_AGENT', 'HTTP_REFERER' ) ) ) {
  449. // We don't care about cookies, and the UA and Referrer were caught above.
  450. continue;
  451. } elseif ( in_array( $key, array( 'REMOTE_ADDR', 'REQUEST_URI', 'DOCUMENT_URI' ) ) ) {
  452. // All three of these are relevant indicators and should be passed along.
  453. $form[ $key ] = $value;
  454. } elseif ( wp_startswith( $key, 'HTTP_' ) ) {
  455. // Any other HTTP header indicators.
  456. // `wp_startswith()` is a wpcom helper function and is included in Jetpack via `functions.compat.php`
  457. $form[ $key ] = $value;
  458. }
  459. }
  460. return $form;
  461. }
  462. /**
  463. * Submit contact-form data to Akismet to check for spam.
  464. * If you're accepting a new item via $_POST, run it Grunion_Contact_Form_Plugin::prepare_for_akismet() first
  465. * Attached to `jetpack_contact_form_is_spam`
  466. *
  467. * @param bool $is_spam
  468. * @param array $form
  469. * @return bool|WP_Error TRUE => spam, FALSE => not spam, WP_Error => stop processing entirely
  470. */
  471. function is_spam_akismet( $is_spam, $form = array() ) {
  472. global $akismet_api_host, $akismet_api_port;
  473. // The signature of this function changed from accepting just $form.
  474. // If something only sends an array, assume it's still using the old
  475. // signature and work around it.
  476. if ( empty( $form ) && is_array( $is_spam ) ) {
  477. $form = $is_spam;
  478. $is_spam = false;
  479. }
  480. // If a previous filter has alrady marked this as spam, trust that and move on.
  481. if ( $is_spam ) {
  482. return $is_spam;
  483. }
  484. if ( ! function_exists( 'akismet_http_post' ) && ! defined( 'AKISMET_VERSION' ) ) {
  485. return false;
  486. }
  487. $query_string = http_build_query( $form );
  488. if ( method_exists( 'Akismet', 'http_post' ) ) {
  489. $response = Akismet::http_post( $query_string, 'comment-check' );
  490. } else {
  491. $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  492. }
  493. $result = false;
  494. if ( isset( $response[0]['x-akismet-pro-tip'] ) && 'discard' === trim( $response[0]['x-akismet-pro-tip'] ) && get_option( 'akismet_strictness' ) === '1' ) {
  495. $result = new WP_Error( 'feedback-discarded', __( 'Feedback discarded.', 'jetpack' ) );
  496. } elseif ( isset( $response[1] ) && 'true' == trim( $response[1] ) ) { // 'true' is spam
  497. $result = true;
  498. }
  499. /**
  500. * Filter the results returned by Akismet for each submitted contact form.
  501. *
  502. * @module contact-form
  503. *
  504. * @since 1.3.1
  505. *
  506. * @param WP_Error|bool $result Is the submitted feedback spam.
  507. * @param array|bool $form Submitted feedback.
  508. */
  509. return apply_filters( 'contact_form_is_spam_akismet', $result, $form );
  510. }
  511. /**
  512. * Submit a feedback as either spam or ham
  513. *
  514. * @param string $as Either 'spam' or 'ham'.
  515. * @param array $form the contact-form data
  516. */
  517. function akismet_submit( $as, $form ) {
  518. global $akismet_api_host, $akismet_api_port;
  519. if ( ! in_array( $as, array( 'ham', 'spam' ) ) ) {
  520. return false;
  521. }
  522. $query_string = '';
  523. if ( is_array( $form ) ) {
  524. $query_string = http_build_query( $form );
  525. }
  526. if ( method_exists( 'Akismet', 'http_post' ) ) {
  527. $response = Akismet::http_post( $query_string, "submit-{$as}" );
  528. } else {
  529. $response = akismet_http_post( $query_string, $akismet_api_host, "/1.1/submit-{$as}", $akismet_api_port );
  530. }
  531. return trim( $response[1] );
  532. }
  533. /**
  534. * Prints the menu
  535. */
  536. function export_form() {
  537. $current_screen = get_current_screen();
  538. if ( ! in_array( $current_screen->id, array( 'edit-feedback', 'feedback_page_feedback-export' ) ) ) {
  539. return;
  540. }
  541. if ( ! current_user_can( 'export' ) ) {
  542. return;
  543. }
  544. // if there aren't any feedbacks, bail out
  545. if ( ! (int) wp_count_posts( 'feedback' )->publish ) {
  546. return;
  547. }
  548. ?>
  549. <div id="feedback-export" style="display:none">
  550. <h2><?php _e( 'Export feedback as CSV', 'jetpack' ) ?></h2>
  551. <div class="clear"></div>
  552. <form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post" class="form">
  553. <?php wp_nonce_field( 'feedback_export','feedback_export_nonce' ); ?>
  554. <input name="action" value="feedback_export" type="hidden">
  555. <label for="post"><?php _e( 'Select feedback to download', 'jetpack' ) ?></label>
  556. <select name="post">
  557. <option value="all"><?php esc_html_e( 'All posts', 'jetpack' ) ?></option>
  558. <?php echo $this->get_feedbacks_as_options() ?>
  559. </select>
  560. <br><br>
  561. <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Download', 'jetpack' ); ?>">
  562. </form>
  563. </div>
  564. <?php
  565. // There aren't any usable actions in core to output the "export feedback" form in the correct place,
  566. // so this inline JS moves it from the top of the page to the bottom.
  567. ?>
  568. <script type='text/javascript'>
  569. var menu = document.getElementById( 'feedback-export' ),
  570. wrapper = document.getElementsByClassName( 'wrap' )[0];
  571. <?php if ( 'edit-feedback' === $current_screen->id ) : ?>
  572. wrapper.appendChild(menu);
  573. <?php endif; ?>
  574. menu.style.display = 'block';
  575. </script>
  576. <?php
  577. }
  578. /**
  579. * Fetch post content for a post and extract just the comment.
  580. *
  581. * @param int $post_id The post id to fetch the content for.
  582. *
  583. * @return string Trimmed post comment.
  584. *
  585. * @codeCoverageIgnore
  586. */
  587. public function get_post_content_for_csv_export( $post_id ) {
  588. $post_content = get_post_field( 'post_content', $post_id );
  589. $content = explode( '<!--more-->', $post_content );
  590. return trim( $content[0] );
  591. }
  592. /**
  593. * Get `_feedback_extra_fields` field from post meta data.
  594. *
  595. * @param int $post_id Id of the post to fetch meta data for.
  596. *
  597. * @return mixed
  598. *
  599. * @codeCoverageIgnore - No need to be covered.
  600. */
  601. public function get_post_meta_for_csv_export( $post_id ) {
  602. return get_post_meta( $post_id, '_feedback_extra_fields', true );
  603. }
  604. /**
  605. * Get parsed feedback post fields.
  606. *
  607. * @param int $post_id Id of the post to fetch parsed contents for.
  608. *
  609. * @return array
  610. *
  611. * @codeCoverageIgnore - No need to be covered.
  612. */
  613. public function get_parsed_field_contents_of_post( $post_id ) {
  614. return self::parse_fields_from_content( $post_id );
  615. }
  616. /**
  617. * Properly maps fields that are missing from the post meta data
  618. * to names, that are similar to those of the post meta.
  619. *
  620. * @param array $parsed_post_content Parsed post content
  621. *
  622. * @see parse_fields_from_content for how the input data is generated.
  623. *
  624. * @return array Mapped fields.
  625. */
  626. public function map_parsed_field_contents_of_post_to_field_names( $parsed_post_content ) {
  627. $mapped_fields = array();
  628. $field_mapping = array(
  629. '_feedback_subject' => __( 'Contact Form', 'jetpack' ),
  630. '_feedback_author' => '1_Name',
  631. '_feedback_author_email' => '2_Email',
  632. '_feedback_author_url' => '3_Website',
  633. '_feedback_main_comment' => '4_Comment',
  634. );
  635. foreach ( $field_mapping as $parsed_field_name => $field_name ) {
  636. if (
  637. isset( $parsed_post_content[ $parsed_field_name ] )
  638. && ! empty( $parsed_post_content[ $parsed_field_name ] )
  639. ) {
  640. $mapped_fields[ $field_name ] = $parsed_post_content[ $parsed_field_name ];
  641. }
  642. }
  643. return $mapped_fields;
  644. }
  645. /**
  646. * Registers the personal data exporter.
  647. *
  648. * @since 6.1.1
  649. *
  650. * @param array $exporters An array of personal data exporters.
  651. *
  652. * @return array $exporters An array of personal data exporters.
  653. */
  654. public function register_personal_data_exporter( $exporters ) {
  655. $exporters['jetpack-feedback'] = array(
  656. 'exporter_friendly_name' => __( 'Feedback', 'jetpack' ),
  657. 'callback' => array( $this, 'personal_data_exporter' ),
  658. );
  659. return $exporters;
  660. }
  661. /**
  662. * Registers the personal data eraser.
  663. *
  664. * @since 6.1.1
  665. *
  666. * @param array $erasers An array of personal data erasers.
  667. *
  668. * @return array $erasers An array of personal data erasers.
  669. */
  670. public function register_personal_data_eraser( $erasers ) {
  671. $erasers['jetpack-feedback'] = array(
  672. 'eraser_friendly_name' => __( 'Feedback', 'jetpack' ),
  673. 'callback' => array( $this, 'personal_data_eraser' ),
  674. );
  675. return $erasers;
  676. }
  677. /**
  678. * Exports personal data.
  679. *
  680. * @since 6.1.1
  681. *
  682. * @param string $email Email address.
  683. * @param int $page Page to export.
  684. *
  685. * @return array $return Associative array with keys expected by core.
  686. */
  687. public function personal_data_exporter( $email, $page = 1 ) {
  688. return $this->_internal_personal_data_exporter( $email, $page );
  689. }
  690. /**
  691. * Internal method for exporting personal data.
  692. *
  693. * Allows us to have a different signature than core expects
  694. * while protecting against future core API changes.
  695. *
  696. * @internal
  697. * @since 6.5
  698. *
  699. * @param string $email Email address.
  700. * @param int $page Page to export.
  701. * @param int $per_page Number of feedbacks to process per page. Internal use only (testing)
  702. *
  703. * @return array Associative array with keys expected by core.
  704. */
  705. public function _internal_personal_data_exporter( $email, $page = 1, $per_page = 250 ) {
  706. $export_data = array();
  707. $post_ids = $this->personal_data_post_ids_by_email( $email, $per_page, $page );
  708. foreach ( $post_ids as $post_id ) {
  709. $post_fields = $this->get_parsed_field_contents_of_post( $post_id );
  710. if ( ! is_array( $post_fields ) || empty( $post_fields['_feedback_subject'] ) ) {
  711. continue; // Corrupt data.
  712. }
  713. $post_fields['_feedback_main_comment'] = $this->get_post_content_for_csv_export( $post_id );
  714. $post_fields = $this->map_parsed_field_contents_of_post_to_field_names( $post_fields );
  715. if ( ! is_array( $post_fields ) || empty( $post_fields ) ) {
  716. continue; // No fields to export.
  717. }
  718. $post_meta = $this->get_post_meta_for_csv_export( $post_id );
  719. $post_meta = is_array( $post_meta ) ? $post_meta : array();
  720. $post_export_data = array();
  721. $post_data = array_merge( $post_fields, $post_meta );
  722. ksort( $post_data );
  723. foreach ( $post_data as $post_data_key => $post_data_value ) {
  724. $post_export_data[] = array(
  725. 'name' => preg_replace( '/^[0-9]+_/', '', $post_data_key ),
  726. 'value' => $post_data_value,
  727. );
  728. }
  729. $export_data[] = array(
  730. 'group_id' => 'feedback',
  731. 'group_label' => __( 'Feedback', 'jetpack' ),
  732. 'item_id' => 'feedback-' . $post_id,
  733. 'data' => $post_export_data,
  734. );
  735. }
  736. return array(
  737. 'data' => $export_data,
  738. 'done' => count( $post_ids ) < $per_page,
  739. );
  740. }
  741. /**
  742. * Erases personal data.
  743. *
  744. * @since 6.1.1
  745. *
  746. * @param string $email Email address.
  747. * @param int $page Page to erase.
  748. *
  749. * @return array Associative array with keys expected by core.
  750. */
  751. public function personal_data_eraser( $email, $page = 1 ) {
  752. return $this->_internal_personal_data_eraser( $email, $page );
  753. }
  754. /**
  755. * Internal method for erasing personal data.
  756. *
  757. * Allows us to have a different signature than core expects
  758. * while protecting against future core API changes.
  759. *
  760. * @internal
  761. * @since 6.5
  762. *
  763. * @param string $email Email address.
  764. * @param int $page Page to erase.
  765. * @param int $per_page Number of feedbacks to process per page. Internal use only (testing)
  766. *
  767. * @return array Associative array with keys expected by core.
  768. */
  769. public function _internal_personal_data_eraser( $email, $page = 1, $per_page = 250 ) {
  770. $removed = false;
  771. $retained = false;
  772. $messages = array();
  773. $option_name = sprintf( '_jetpack_pde_feedback_%s', md5( $email ) );
  774. $last_post_id = 1 === $page ? 0 : get_option( $option_name, 0 );
  775. $post_ids = $this->personal_data_post_ids_by_email( $email, $per_page, $page, $last_post_id );
  776. foreach ( $post_ids as $post_id ) {
  777. /**
  778. * Filters whether to erase a particular Feedback post.
  779. *
  780. * @since 6.3.0
  781. *
  782. * @param bool|string $prevention_message Whether to apply erase the Feedback post (bool).
  783. * Custom prevention message (string). Default true.
  784. * @param int $post_id Feedback post ID.
  785. */
  786. $prevention_message = apply_filters( 'grunion_contact_form_delete_feedback_post', true, $post_id );
  787. if ( true !== $prevention_message ) {
  788. if ( $prevention_message && is_string( $prevention_message ) ) {
  789. $messages[] = esc_html( $prevention_message );
  790. } else {
  791. $messages[] = sprintf(
  792. // translators: %d: Post ID.
  793. __( 'Feedback ID %d could not be removed at this time.', 'jetpack' ),
  794. $post_id
  795. );
  796. }
  797. $retained = true;
  798. continue;
  799. }
  800. if ( wp_delete_post( $post_id, true ) ) {
  801. $removed = true;
  802. } else {
  803. $retained = true;
  804. $messages[] = sprintf(
  805. // translators: %d: Post ID.
  806. __( 'Feedback ID %d could not be removed at this time.', 'jetpack' ),
  807. $post_id
  808. );
  809. }
  810. }
  811. $done = count( $post_ids ) < $per_page;
  812. if ( $done ) {
  813. delete_option( $option_name );
  814. } else {
  815. update_option( $option_name, (int) $post_id );
  816. }
  817. return array(
  818. 'items_removed' => $removed,
  819. 'items_retained' => $retained,
  820. 'messages' => $messages,
  821. 'done' => $done,
  822. );
  823. }
  824. /**
  825. * Queries personal data by email address.
  826. *
  827. * @since 6.1.1
  828. *
  829. * @param string $email Email address.
  830. * @param int $per_page Post IDs per page. Default is `250`.
  831. * @param int $page Page to query. Default is `1`.
  832. * @param int $last_post_id Page to query. Default is `0`. If non-zero, used instead of $page.
  833. *
  834. * @return array An array of post IDs.
  835. */
  836. public function personal_data_post_ids_by_email( $email, $per_page = 250, $page = 1, $last_post_id = 0 ) {
  837. add_filter( 'posts_search', array( $this, 'personal_data_search_filter' ) );
  838. $this->pde_last_post_id_erased = $last_post_id;
  839. $this->pde_email_address = $email;
  840. $post_ids = get_posts( array(
  841. 'post_type' => 'feedback',
  842. 'post_status' => 'publish',
  843. // This search parameter gets overwritten in ->personal_data_search_filter()
  844. 's' => '..PDE..AUTHOR EMAIL:..PDE..',
  845. 'sentence' => true,
  846. 'order' => 'ASC',
  847. 'orderby' => 'ID',
  848. 'fields' => 'ids',
  849. 'posts_per_page' => $per_page,
  850. 'paged' => $last_post_id ? 1 : $page,
  851. 'suppress_filters' => false,
  852. ) );
  853. $this->pde_last_post_id_erased = 0;
  854. $this->pde_email_address = '';
  855. remove_filter( 'posts_search', array( $this, 'personal_data_search_filter' ) );
  856. return $post_ids;
  857. }
  858. /**
  859. * Filters searches by email address.
  860. *
  861. * @since 6.1.1
  862. *
  863. * @param string $search SQL where clause.
  864. *
  865. * @return array Filtered SQL where clause.
  866. */
  867. public function personal_data_search_filter( $search ) {
  868. global $wpdb;
  869. /*
  870. * Limits search to `post_content` only, and we only match the
  871. * author's email address whenever it's on a line by itself.
  872. */
  873. if ( $this->pde_email_address && false !== strpos( $search, '..PDE..AUTHOR EMAIL:..PDE..' ) ) {
  874. $search = $wpdb->prepare(
  875. " AND (
  876. {$wpdb->posts}.post_content LIKE %s
  877. OR {$wpdb->posts}.post_content LIKE %s
  878. )",
  879. // `chr( 10 )` = `\n`, `chr( 13 )` = `\r`
  880. '%' . $wpdb->esc_like( chr( 10 ) . 'AUTHOR EMAIL: ' . $this->pde_email_address . chr( 10 ) ) . '%',
  881. '%' . $wpdb->esc_like( chr( 13 ) . 'AUTHOR EMAIL: ' . $this->pde_email_address . chr( 13 ) ) . '%'
  882. );
  883. if ( $this->pde_last_post_id_erased ) {
  884. $search .= $wpdb->prepare( " AND {$wpdb->posts}.ID > %d", $this->pde_last_post_id_erased );
  885. }
  886. }
  887. return $search;
  888. }
  889. /**
  890. * Prepares feedback post data for CSV export.
  891. *
  892. * @param array $post_ids Post IDs to fetch the data for. These need to be Feedback posts.
  893. *
  894. * @return array
  895. */
  896. public function get_export_data_for_posts( $post_ids ) {
  897. $posts_data = array();
  898. $field_names = array();
  899. $result = array();
  900. /**
  901. * Fetch posts and get the possible field names for later use
  902. */
  903. foreach ( $post_ids as $post_id ) {
  904. /**
  905. * Fetch post main data, because we need the subject and author data for the feedback form.
  906. */
  907. $post_real_data = $this->get_parsed_field_contents_of_post( $post_id );
  908. /**
  909. * If `$post_real_data` is not an array or there is no `_feedback_subject` set,
  910. * then something must be wrong with the feedback post. Skip it.
  911. */
  912. if ( ! is_array( $post_real_data ) || ! isset( $post_real_data['_feedback_subject'] ) ) {
  913. continue;
  914. }
  915. /**
  916. * Fetch main post comment. This is from the default textarea fields.
  917. * If it is non-empty, then we add it to data, otherwise skip it.
  918. */
  919. $post_comment_content = $this->get_post_content_for_csv_export( $post_id );
  920. if ( ! empty( $post_comment_content ) ) {
  921. $post_real_data['_feedback_main_comment'] = $post_comment_content;
  922. }
  923. /**
  924. * Map parsed fields to proper field names
  925. */
  926. $mapped_fields = $this->map_parsed_field_contents_of_post_to_field_names( $post_real_data );
  927. /**
  928. * Fetch post meta data.
  929. */
  930. $post_meta_data = $this->get_post_meta_for_csv_export( $post_id );
  931. /**
  932. * If `$post_meta_data` is not an array or if it is empty, then there is no
  933. * extra feedback to work with. Create an empty array.
  934. */
  935. if ( ! is_array( $post_meta_data ) || empty( $post_meta_data ) ) {
  936. $post_meta_data = array();
  937. }
  938. /**
  939. * Prepend the feedback subject to the list of fields.
  940. */
  941. $post_meta_data = array_merge(
  942. $mapped_fields,
  943. $post_meta_data
  944. );
  945. /**
  946. * Save post metadata for later usage.
  947. */
  948. $posts_data[ $post_id ] = $post_meta_data;
  949. /**
  950. * Save field names, so we can use them as header fields later in the CSV.
  951. */
  952. $field_names = array_merge( $field_names, array_keys( $post_meta_data ) );
  953. }
  954. /**
  955. * Make sure the field names are unique, because we don't want duplicate data.
  956. */
  957. $field_names = array_unique( $field_names );
  958. /**
  959. * Sort the field names by the field id number
  960. */
  961. sort( $field_names, SORT_NUMERIC );
  962. /**
  963. * Loop through every post, which is essentially CSV row.
  964. */
  965. foreach ( $posts_data as $post_id => $single_post_data ) {
  966. /**
  967. * Go through all the possible fields and check if the field is available
  968. * in the current post.
  969. *
  970. * If it is - add the data as a value.
  971. * If it is not - add an empty string, which is just a placeholder in the CSV.
  972. */
  973. foreach ( $field_names as $single_field_name ) {
  974. if (
  975. isset( $single_post_data[ $single_field_name ] )
  976. && ! empty( $single_post_data[ $single_field_name ] )
  977. ) {
  978. $result[ $single_field_name ][] = trim( $single_post_data[ $single_field_name ] );
  979. } else {
  980. $result[ $single_field_name ][] = '';
  981. }
  982. }
  983. }
  984. return $result;
  985. }
  986. /**
  987. * download as a csv a contact form or all of them in a csv file
  988. */
  989. function download_feedback_as_csv() {
  990. if ( empty( $_POST['feedback_export_nonce'] ) ) {
  991. return;
  992. }
  993. check_admin_referer( 'feedback_export', 'feedback_export_nonce' );
  994. if ( ! current_user_can( 'export' ) ) {
  995. return;
  996. }
  997. $args = array(
  998. 'posts_per_page' => -1,
  999. 'post_type' => 'feedback',
  1000. 'post_status' => 'publish',
  1001. 'order' => 'ASC',
  1002. 'fields' => 'ids',
  1003. 'suppress_filters' => false,
  1004. );
  1005. $filename = date( 'Y-m-d' ) . '-feedback-export.csv';
  1006. // Check if we want to download all the feedbacks or just a certain contact form
  1007. if ( ! empty( $_POST['post'] ) && $_POST['post'] !== 'all' ) {
  1008. $args['post_parent'] = (int) $_POST['post'];
  1009. $filename = date( 'Y-m-d' ) . '-' . str_replace( '&nbsp;', '-', get_the_title( (int) $_POST['post'] ) ) . '.csv';
  1010. }
  1011. $feedbacks = get_posts( $args );
  1012. if ( empty( $feedbacks ) ) {
  1013. return;
  1014. }
  1015. $filename = sanitize_file_name( $filename );
  1016. /**
  1017. * Prepare data for export.
  1018. */
  1019. $data = $this->get_export_data_for_posts( $feedbacks );
  1020. /**
  1021. * If `$data` is empty, there's nothing we can do below.
  1022. */
  1023. if ( ! is_array( $data ) || empty( $data ) ) {
  1024. return;
  1025. }
  1026. /**
  1027. * Extract field names from `$data` for later use.
  1028. */
  1029. $fields = array_keys( $data );
  1030. /**
  1031. * Count how many rows will be exported.
  1032. */
  1033. $row_count = count( reset( $data ) );
  1034. // Forces the download of the CSV instead of echoing
  1035. header( 'Content-Disposition: attachment; filename=' . $filename );
  1036. header( 'Pragma: no-cache' );
  1037. header( 'Expires: 0' );
  1038. header( 'Content-Type: text/csv; charset=utf-8' );
  1039. $output = fopen( 'php://output', 'w' );
  1040. /**
  1041. * Print CSV headers
  1042. */
  1043. fputcsv( $output, $fields );
  1044. /**
  1045. * Print rows to the output.
  1046. */
  1047. for ( $i = 0; $i < $row_count; $i ++ ) {
  1048. $current_row = array();
  1049. /**
  1050. * Put all the fields in `$current_row` array.
  1051. */
  1052. foreach ( $fields as $single_field_name ) {
  1053. $current_row[] = $this->esc_csv( $data[ $single_field_name ][ $i ] );
  1054. }
  1055. /**
  1056. * Output the complete CSV row
  1057. */
  1058. fputcsv( $output, $current_row );
  1059. }
  1060. fclose( $output );
  1061. }
  1062. /**
  1063. * Escape a string to be used in a CSV context
  1064. *
  1065. * Malicious input can inject formulas into CSV files, opening up the possibility for phishing attacks and
  1066. * disclosure of sensitive information.
  1067. *
  1068. * Additionally, Excel exposes the ability to launch arbitrary commands through the DDE protocol.
  1069. *
  1070. * @see http://www.contextis.com/resources/blog/comma-separated-vulnerabilities/
  1071. *
  1072. * @param string $field
  1073. *
  1074. * @return string
  1075. */
  1076. public function esc_csv( $field ) {
  1077. $active_content_triggers = array( '=', '+', '-', '@' );
  1078. if ( in_array( mb_substr( $field, 0, 1 ), $active_content_triggers, true ) ) {
  1079. $field = "'" . $field;
  1080. }
  1081. return $field;
  1082. }
  1083. /**
  1084. * Returns a string of HTML <option> items from an array of posts
  1085. *
  1086. * @return string a string of HTML <option> items
  1087. */
  1088. protected function get_feedbacks_as_options() {
  1089. $options = '';
  1090. // Get the feedbacks' parents' post IDs
  1091. $feedbacks = get_posts( array(
  1092. 'fields' => 'id=>parent',
  1093. 'posts_per_page' => 100000,
  1094. 'post_type' => 'feedback',
  1095. 'post_status' => 'publish',
  1096. 'suppress_filters' => false,
  1097. ) );
  1098. $parents = array_unique( array_values( $feedbacks ) );
  1099. $posts = get_posts( array(
  1100. 'orderby' => 'ID',
  1101. 'posts_per_page' => 1000,
  1102. 'post_type' => 'any',
  1103. 'post__in' => array_values( $parents ),
  1104. 'suppress_filters' => false,
  1105. ) );
  1106. // creates the string of <option> elements
  1107. foreach ( $posts as $post ) {
  1108. $options .= sprintf( '<option value="%s">%s</option>', esc_attr( $post->ID ), esc_html( $post->post_title ) );
  1109. }
  1110. return $options;
  1111. }
  1112. /**
  1113. * Get the names of all the form's fields
  1114. *
  1115. * @param array|int $posts the post we want the fields of
  1116. *
  1117. * @return array the array of fields
  1118. *
  1119. * @deprecated As this is no longer necessary as of the CSV export rewrite. - 2015-12-29
  1120. */
  1121. protected function get_field_names( $posts ) {
  1122. $posts = (array) $posts;
  1123. $all_fields = array();
  1124. foreach ( $posts as $post ) {
  1125. $fields = self::parse_fields_from_content( $post );
  1126. if ( isset( $fields['_feedback_all_fields'] ) ) {
  1127. $extra_fields = array_keys( $fields['_feedback_all_fields'] );
  1128. $all_fields = array_merge( $all_fields, $extra_fields );
  1129. }
  1130. }
  1131. $all_fields = array_unique( $all_fields );
  1132. return $all_fields;
  1133. }
  1134. public static function parse_fields_from_content( $post_id ) {
  1135. static $post_fields;
  1136. if ( ! is_array( $post_fields ) ) {
  1137. $post_fields = array();
  1138. }
  1139. if ( isset( $post_fields[ $post_id ] ) ) {
  1140. return $post_fields[ $post_id ];
  1141. }
  1142. $all_values = array();
  1143. $post_content = get_post_field( 'post_content', $post_id );
  1144. $content = explode( '<!--more-->', $post_content );
  1145. $lines = array();
  1146. if ( count( $content ) > 1 ) {
  1147. $content = str_ireplace( array( '<br />', ')</p>' ), '', $content[1] );
  1148. $one_line = preg_replace( '/\s+/', ' ', $content );
  1149. $one_line = preg_replace( '/.*Array \( (.*)\)/', '$1', $one_line );
  1150. preg_match_all( '/\[([^\]]+)\] =\&gt\; ([^\[]+)/', $one_line, $matches );
  1151. if ( count( $matches ) > 1 ) {
  1152. $all_values = array_combine( array_map( 'trim', $matches[1] ), array_map( 'trim', $matches[2] ) );
  1153. }
  1154. $lines = array_filter( explode( "\n", $content ) );
  1155. }
  1156. $var_map = array(
  1157. 'AUTHOR' => '_feedback_author',
  1158. 'AUTHOR EMAIL' => '_feedback_author_email',
  1159. 'AUTHOR URL' => '_feedback_author_url',
  1160. 'SUBJECT' => '_feedback_subject',
  1161. 'IP' => '_feedback_ip',
  1162. );
  1163. $fields = array();
  1164. foreach ( $lines as $line ) {
  1165. $vars = explode( ': ', $line, 2 );
  1166. if ( ! empty( $vars ) ) {
  1167. if ( isset( $var_map[ $vars[0] ] ) ) {
  1168. $fields[ $var_map[ $vars[0] ] ] = self::strip_tags( trim( $vars[1] ) );
  1169. }
  1170. }
  1171. }
  1172. $fields['_feedback_all_fields'] = $all_values;
  1173. $post_fields[ $post_id ] = $fields;
  1174. return $fields;
  1175. }
  1176. /**
  1177. * Creates a valid csv row from a post id
  1178. *
  1179. * @param int $post_id The id of the post
  1180. * @param array $fields An array containing the names of all the fields of the csv
  1181. * @return String The csv row
  1182. *
  1183. * @deprecated This is no longer needed, as of the CSV export rewrite.
  1184. */
  1185. protected static function make_csv_row_from_feedback( $post_id, $fields ) {
  1186. $content_fields = self::parse_fields_from_content( $post_id );
  1187. $all_fields = array();
  1188. if ( isset( $content_fields['_feedback_all_fields'] ) ) {
  1189. $all_fields = $content_fields['_feedback_all_fields'];
  1190. }
  1191. // Overwrite the parsed content with the content we stored in post_meta in a better format.
  1192. $extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', true );
  1193. foreach ( $extra_fields as $extra_field => $extra_value ) {
  1194. $all_fields[ $extra_field ] = $extra_value;
  1195. }
  1196. // The first element in all of the exports will be the subject
  1197. $row_items[] = $content_fields['_feedback_subject'];
  1198. // Loop the fields array in order to fill the $row_items array correctly
  1199. foreach ( $fields as $field ) {
  1200. if ( $field === __( 'Contact Form', 'jetpack' ) ) { // the first field will ever be the contact form, so we can continue
  1201. continue;
  1202. } elseif ( array_key_exists( $field, $all_fields ) ) {
  1203. $row_items[] = $all_fields[ $field ];
  1204. } else { $row_items[] = '';
  1205. }
  1206. }
  1207. return $row_items;
  1208. }
  1209. public static function get_ip_address() {
  1210. return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
  1211. }
  1212. }
  1213. /**
  1214. * Generic shortcode class.
  1215. * Does nothing other than store structured data and output the shortcode as a string
  1216. *
  1217. * Not very general - specific to Grunion.
  1218. */
  1219. class Crunion_Contact_Form_Shortcode {
  1220. /**
  1221. * @var string the name of the shortcode: [$shortcode_name /]
  1222. */
  1223. public $shortcode_name;
  1224. /**
  1225. * @var array key => value pairs for the shortcode's attributes: [$shortcode_name key="value" ... /]
  1226. */
  1227. public $attributes;
  1228. /**
  1229. * @var array key => value pair for attribute defaults
  1230. */
  1231. public $defaults = array();
  1232. /**
  1233. * @var null|string Null for selfclosing shortcodes. Hhe inner content of otherwise: [$shortcode_name]$content[/$shortcode_name]
  1234. */
  1235. public $content;
  1236. /**
  1237. * @var array Associative array of inner "child" shortcodes equivalent to the $content: [$shortcode_name][child 1/][child 2/][/$shortcode_name]
  1238. */
  1239. public $fields;
  1240. /**
  1241. * @var null|string The HTML of the parsed inner "child" shortcodes". Null for selfclosing shortcodes.
  1242. */
  1243. public $body;
  1244. /**
  1245. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
  1246. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
  1247. */
  1248. function __construct( $attributes, $content = null ) {
  1249. $this->attributes = $this->unesc_attr( $attributes );
  1250. if ( is_array( $content ) ) {
  1251. $string_content = '';
  1252. foreach ( $content as $field ) {
  1253. $string_content .= (string) $field;
  1254. }
  1255. $this->content = $string_content;
  1256. } else {
  1257. $this->content = $content;
  1258. }
  1259. $this->parse_content( $this->content );
  1260. }
  1261. /**
  1262. * Processes the shortcode's inner content for "child" shortcodes
  1263. *
  1264. * @param string $content The shortcode's inner content: [shortcode]$content[/shortcode]
  1265. */
  1266. function parse_content( $content ) {
  1267. if ( is_null( $content ) ) {
  1268. $this->body = null;
  1269. }
  1270. $this->body = do_shortcode( $content );
  1271. }
  1272. /**
  1273. * Returns the value of the requested attribute.
  1274. *
  1275. * @param string $key The attribute to retrieve
  1276. * @return mixed
  1277. */
  1278. function get_attribute( $key ) {
  1279. return isset( $this->attributes[ $key ] ) ? $this->attributes[ $key ] : null;
  1280. }
  1281. function esc_attr( $value ) {
  1282. if ( is_array( $value ) ) {
  1283. return array_map( array( $this, 'esc_attr' ), $value );
  1284. }
  1285. $value = Grunion_Contact_Form_Plugin::strip_tags( $value );
  1286. $value = _wp_specialchars( $value, ENT_QUOTES, false, true );
  1287. // Shortcode attributes can't contain "]"
  1288. $value = str_replace( ']', '', $value );
  1289. $value = str_replace( ',', '&#x002c;', $value ); // store commas encoded
  1290. $value = strtr( $value, array( '%' => '%25', '&' => '%26' ) );
  1291. // shortcode_parse_atts() does stripcslashes()
  1292. $value = addslashes( $value );
  1293. return $value;
  1294. }
  1295. function unesc_attr( $value ) {
  1296. if ( is_array( $value ) ) {
  1297. return array_map( array( $this, 'unesc_attr' ), $value );
  1298. }
  1299. // For back-compat with old Grunion encoding
  1300. // Also, unencode commas
  1301. $value = strtr( $value, array( '%26' => '&', '%25' => '%' ) );
  1302. $value = preg_replace( array( '/&#x0*22;/i', '/&#x0*27;/i', '/&#x0*26;/i', '/&#x0*2c;/i' ), array( '"', "'", '&', ',' ), $value );
  1303. $value = htmlspecialchars_decode( $value, ENT_QUOTES );
  1304. $value = Grunion_Contact_Form_Plugin::strip_tags( $value );
  1305. return $value;
  1306. }
  1307. /**
  1308. * Generates the shortcode
  1309. */
  1310. function __toString() {
  1311. $r = "[{$this->shortcode_name} ";
  1312. foreach ( $this->attributes as $key => $value ) {
  1313. if ( ! $value ) {
  1314. continue;
  1315. }
  1316. if ( isset( $this->defaults[ $key ] ) && $this->defaults[ $key ] == $value ) {
  1317. continue;
  1318. }
  1319. if ( 'id' == $key ) {
  1320. continue;
  1321. }
  1322. $value = $this->esc_attr( $value );
  1323. if ( is_array( $value ) ) {
  1324. $value = join( ',', $value );
  1325. }
  1326. if ( false === strpos( $value, "'" ) ) {
  1327. $value = "'$value'";
  1328. } elseif ( false === strpos( $value, '"' ) ) {
  1329. $value = '"' . $value . '"';
  1330. } else {
  1331. // Shortcodes can't contain both '"' and "'". Strip one.
  1332. $value = str_replace( "'", '', $value );
  1333. $value = "'$value'";
  1334. }
  1335. $r .= "{$key}={$value} ";
  1336. }
  1337. $r = rtrim( $r );
  1338. if ( $this->fields ) {
  1339. $r .= ']';
  1340. foreach ( $this->fields as $field ) {
  1341. $r .= (string) $field;
  1342. }
  1343. $r .= "[/{$this->shortcode_name}]";
  1344. } else {
  1345. $r .= '/]';
  1346. }
  1347. return $r;
  1348. }
  1349. }
  1350. /**
  1351. * Class for the contact-form shortcode.
  1352. * Parses shortcode to output the contact form as HTML
  1353. * Sends email and stores the contact form response (a.k.a. "feedback")
  1354. */
  1355. class Grunion_Contact_Form extends Crunion_Contact_Form_Shortcode {
  1356. public $shortcode_name = 'contact-form';
  1357. /**
  1358. * @var WP_Error stores form submission errors
  1359. */
  1360. public $errors;
  1361. /**
  1362. * @var string The SHA1 hash of the attributes that comprise the form.
  1363. */
  1364. public $hash;
  1365. /**
  1366. * @var Grunion_Contact_Form The most recent (inclusive) contact-form shortcode processed
  1367. */
  1368. static $last;
  1369. /**
  1370. * @var Whatever form we are currently looking at. If processed, will become $last
  1371. */
  1372. static $current_form;
  1373. /**
  1374. * @var array All found forms, indexed by hash.
  1375. */
  1376. static $forms = array();
  1377. /**
  1378. * @var bool Whether to print the grunion.css style when processing the contact-form shortcode
  1379. */
  1380. static $style = false;
  1381. function __construct( $attributes, $content = null ) {
  1382. global $post;
  1383. $this->hash = sha1( json_encode( $attributes ) . $content );
  1384. self::$forms[ $this->hash ] = $this;
  1385. // Set up the default subject and recipient for this form
  1386. $default_to = '';
  1387. $default_subject = '[' . get_option( 'blogname' ) . ']';
  1388. if ( ! isset( $attributes ) || ! is_array( $attributes ) ) {
  1389. $attributes = array();
  1390. }
  1391. if ( ! empty( $attributes['widget'] ) && $attributes['widget'] ) {
  1392. $default_to .= get_option( 'admin_email' );
  1393. $attributes['id'] = 'widget-' . $attributes['widget'];
  1394. $default_subject = sprintf( _x( '%1$s Sidebar', '%1$s = blog name', 'jetpack' ), $default_subject );
  1395. } elseif ( $post ) {
  1396. $attributes['id'] = $post->ID;
  1397. $default_subject = sprintf( _x( '%1$s %2$s', '%1$s = blog name, %2$s = post title', 'jetpack' ), $default_subject, Grunion_Contact_Form_Plugin::strip_tags( $post->post_title ) );
  1398. $post_author = get_userdata( $post->post_author );
  1399. $default_to .= $post_author->user_email;
  1400. }
  1401. // Keep reference to $this for parsing form fields
  1402. self::$current_form = $this;
  1403. $this->defaults = array(
  1404. 'to' => $default_to,
  1405. 'subject' => $default_subject,
  1406. 'show_subject' => 'no', // only used in back-compat mode
  1407. 'widget' => 0, // Not exposed to the user. Works with Grunion_Contact_Form_Plugin::widget_atts()
  1408. 'id' => null, // Not exposed to the user. Set above.
  1409. 'submit_button_text' => __( 'Submit &#187;', 'jetpack' ),
  1410. );
  1411. $attributes = shortcode_atts( $this->defaults, $attributes, 'contact-form' );
  1412. // We only enable the contact-field shortcode temporarily while processing the contact-form shortcode
  1413. Grunion_Contact_Form_Plugin::$using_contact_form_field = true;
  1414. parent::__construct( $attributes, $content );
  1415. // There were no fields in the contact form. The form was probably just [contact-form /]. Build a default form.
  1416. if ( empty( $this->fields ) ) {
  1417. // same as the original Grunion v1 form
  1418. $default_form = '
  1419. [contact-field label="' . __( 'Name', 'jetpack' ) . '" type="name" required="true" /]
  1420. [contact-field label="' . __( 'Email', 'jetpack' ) . '" type="email" required="true" /]
  1421. [contact-field label="' . __( 'Website', 'jetpack' ) . '" type="url" /]';
  1422. if ( 'yes' == strtolower( $this->get_attribute( 'show_subject' ) ) ) {
  1423. $default_form .= '
  1424. [contact-field label="' . __( 'Subject', 'jetpack' ) . '" type="subject" /]';
  1425. }
  1426. $default_form .= '
  1427. [contact-field label="' . __( 'Message', 'jetpack' ) . '" type="textarea" /]';
  1428. $this->parse_content( $default_form );
  1429. // Store the shortcode
  1430. $this->store_shortcode( $default_form, $attributes, $this->hash );
  1431. } else {
  1432. // Store the shortcode
  1433. $this->store_shortcode( $content, $attributes, $this->hash );
  1434. }
  1435. // $this->body and $this->fields have been setup. We no longer need the contact-field shortcode.
  1436. Grunion_Contact_Form_Plugin::$using_contact_form_field = false;
  1437. }
  1438. /**
  1439. * Store shortcode content for recall later
  1440. * - used to receate shortcode when user uses do_shortcode
  1441. *
  1442. * @param string $content
  1443. * @param array $attributes
  1444. * @param string $hash
  1445. */
  1446. static function store_shortcode( $content = null, $attributes = null, $hash = null ) {
  1447. if ( $content != null and isset( $attributes['id'] ) ) {
  1448. if ( empty( $hash ) ) {
  1449. $hash = sha1( json_encode( $attributes ) . $content );
  1450. }
  1451. $shortcode_meta = get_post_meta( $attributes['id'], "_g_feedback_shortcode_{$hash}", true );
  1452. if ( $shortcode_meta != '' or $shortcode_meta != $content ) {
  1453. update_post_meta( $attributes['id'], "_g_feedback_shortcode_{$hash}", $content );
  1454. // Save attributes to post_meta for later use. They're not available later in do_shortcode situations.
  1455. update_post_meta( $attributes['id'], "_g_feedback_shortcode_atts_{$hash}", $attributes );
  1456. }
  1457. }
  1458. }
  1459. /**
  1460. * Toggle for printing the grunion.css stylesheet
  1461. *
  1462. * @param bool $style
  1463. */
  1464. static function style( $style ) {
  1465. $previous_style = self::$style;
  1466. self::$style = (bool) $style;
  1467. return $previous_style;
  1468. }
  1469. /**
  1470. * Turn on printing of grunion.css stylesheet
  1471. *
  1472. * @see ::style()
  1473. * @internal
  1474. * @param bool $style
  1475. */
  1476. static function _style_on() {
  1477. return self::style( true );
  1478. }
  1479. /**
  1480. * The contact-form shortcode processor
  1481. *
  1482. * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  1483. * @param string|null $content The shortcode's inner content: [contact-form]$content[/contact-form]
  1484. * @return string HTML for the concat form.
  1485. */
  1486. static function parse( $attributes, $content ) {
  1487. require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
  1488. if ( Jetpack_Sync_Settings::is_syncing() ) {
  1489. return '';
  1490. }
  1491. // Create a new Grunion_Contact_Form object (this class)
  1492. $form = new Grunion_Contact_Form( $attributes, $content );
  1493. $id = $form->get_attribute( 'id' );
  1494. if ( ! $id ) { // something terrible has happened
  1495. return '[contact-form]';
  1496. }
  1497. if ( is_feed() ) {
  1498. return '[contact-form]';
  1499. }
  1500. self::$last = $form;
  1501. // Enqueue the grunion.css stylesheet if self::$style allows it
  1502. if ( self::$style && ( empty( $_REQUEST['action'] ) || $_REQUEST['action'] != 'grunion_shortcode_to_json' ) ) {
  1503. // Enqueue the style here instead of printing it, because if some other plugin has run the_post()+rewind_posts(),
  1504. // (like VideoPress does), the style tag gets "printed" the first time and discarded, leaving the contact form unstyled.
  1505. // when WordPress does the real loop.
  1506. wp_enqueue_style( 'grunion.css' );
  1507. }
  1508. $r = '';
  1509. $r .= "<div id='contact-form-$id'>\n";
  1510. if ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) {
  1511. // There are errors. Display them
  1512. $r .= "<div class='form-error'>\n<h3>" . __( 'Error!', 'jetpack' ) . "</h3>\n<ul class='form-errors'>\n";
  1513. foreach ( $form->errors->get_error_messages() as $message ) {
  1514. $r .= "\t<li class='form-error-message'>" . esc_html( $message ) . "</li>\n";
  1515. }
  1516. $r .= "</ul>\n</div>\n\n";
  1517. }
  1518. if ( isset( $_GET['contact-form-id'] )
  1519. && $_GET['contact-form-id'] == self::$last->get_attribute( 'id' )
  1520. && isset( $_GET['contact-form-sent'], $_GET['contact-form-hash'] )
  1521. && hash_equals( $form->hash, $_GET['contact-form-hash'] ) ) { // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  1522. // The contact form was submitted. Show the success message/results
  1523. $feedback_id = (int) $_GET['contact-form-sent'];
  1524. $back_url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', '_wpnonce' ) );
  1525. $r_success_message =
  1526. '<h3>' . __( 'Message Sent', 'jetpack' ) .
  1527. ' (<a href="' . esc_url( $back_url ) . '">' . esc_html__( 'go back', 'jetpack' ) . '</a>)' .
  1528. "</h3>\n\n";
  1529. // Don't show the feedback details unless the nonce matches
  1530. if ( $feedback_id && wp_verify_nonce( stripslashes( $_GET['_wpnonce'] ), "contact-form-sent-{$feedback_id}" ) ) {
  1531. $r_success_message .= self::success_message( $feedback_id, $form );
  1532. }
  1533. /**
  1534. * Filter the message returned after a successful contact form submission.
  1535. *
  1536. * @module contact-form
  1537. *
  1538. * @since 1.3.1
  1539. *
  1540. * @param string $r_success_message Success message.
  1541. */
  1542. $r .= apply_filters( 'grunion_contact_form_success_message', $r_success_message );
  1543. } else {
  1544. // Nothing special - show the normal contact form
  1545. if ( $form->get_attribute( 'widget' ) ) {
  1546. // Submit form to the current URL
  1547. $url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', 'action', '_wpnonce' ) );
  1548. } else {
  1549. // Submit form to the post permalink
  1550. $url = get_permalink();
  1551. }
  1552. // For SSL/TLS page. See RFC 3986 Section 4.2
  1553. $url = set_url_scheme( $url );
  1554. // May eventually want to send this to admin-post.php...
  1555. /**
  1556. * Filter the contact form action URL.
  1557. *
  1558. * @module contact-form
  1559. *
  1560. * @since 1.3.1
  1561. *
  1562. * @param string $contact_form_id Contact form post URL.
  1563. * @param $post $GLOBALS['post'] Post global variable.
  1564. * @param int $id Contact Form ID.
  1565. */
  1566. $url = apply_filters( 'grunion_contact_form_form_action', "{$url}#contact-form-{$id}", $GLOBALS['post'], $id );
  1567. $r .= "<form action='" . esc_url( $url ) . "' method='post' class='contact-form commentsblock'>\n";
  1568. $r .= $form->body;
  1569. $r .= "\t<p class='contact-submit'>\n";
  1570. $r .= "\t\t<input type='submit' value='" . esc_attr( $form->get_attribute( 'submit_button_text' ) ) . "' class='pushbutton-wide'/>\n";
  1571. if ( is_user_logged_in() ) {
  1572. $r .= "\t\t" . wp_nonce_field( 'contact-form_' . $id, '_wpnonce', true, false ) . "\n"; // nonce and referer
  1573. }
  1574. $r .= "\t\t<input type='hidden' name='contact-form-id' value='$id' />\n";
  1575. $r .= "\t\t<input type='hidden' name='action' value='grunion-contact-form' />\n";
  1576. $r .= "\t\t<input type='hidden' name='contact-form-hash' value='" . esc_attr( $form->hash ) . "' />\n";
  1577. $r .= "\t</p>\n";
  1578. $r .= "</form>\n";
  1579. }
  1580. $r .= '</div>';
  1581. return $r;
  1582. }
  1583. /**
  1584. * Returns a success message to be returned if the form is sent via AJAX.
  1585. *
  1586. * @param int $feedback_id
  1587. * @param object Grunion_Contact_Form $form
  1588. *
  1589. * @return string $message
  1590. */
  1591. static function success_message( $feedback_id, $form ) {
  1592. return wp_kses(
  1593. '<blockquote class="contact-form-submission">'
  1594. . '<p>' . join( self::get_compiled_form( $feedback_id, $form ), '</p><p>' ) . '</p>'
  1595. . '</blockquote>',
  1596. array( 'br' => array(), 'blockquote' => array( 'class' => array() ), 'p' => array() )
  1597. );
  1598. }
  1599. /**
  1600. * Returns a compiled form with labels and values in a form of an array
  1601. * of lines.
  1602. *
  1603. * @param int $feedback_id
  1604. * @param object Grunion_Contact_Form $form
  1605. *
  1606. * @return array $lines
  1607. */
  1608. static function get_compiled_form( $feedback_id, $form ) {
  1609. $feedback = get_post( $feedback_id );
  1610. $field_ids = $form->get_field_ids();
  1611. $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $feedback_id );
  1612. // Maps field_ids to post_meta keys
  1613. $field_value_map = array(
  1614. 'name' => 'author',
  1615. 'email' => 'author_email',
  1616. 'url' => 'author_url',
  1617. 'subject' => 'subject',
  1618. 'textarea' => false, // not a post_meta key. This is stored in post_content
  1619. );
  1620. $compiled_form = array();
  1621. // "Standard" field whitelist
  1622. foreach ( $field_value_map as $type => $meta_key ) {
  1623. if ( isset( $field_ids[ $type ] ) ) {
  1624. $field = $form->fields[ $field_ids[ $type ] ];
  1625. if ( $meta_key ) {
  1626. if ( isset( $content_fields[ "_feedback_{$meta_key}" ] ) ) {
  1627. $value = $content_fields[ "_feedback_{$meta_key}" ];
  1628. }
  1629. } else {
  1630. // The feedback content is stored as the first "half" of post_content
  1631. $value = $feedback->post_content;
  1632. list( $value ) = explode( '<!--more-->', $value );
  1633. $value = trim( $value );
  1634. }
  1635. $field_index = array_search( $field_ids[ $type ], $field_ids['all'] );
  1636. $compiled_form[ $field_index ] = sprintf(
  1637. '<b>%1$s:</b> %2$s<br /><br />',
  1638. wp_kses( $field->get_attribute( 'label' ), array() ),
  1639. nl2br( wp_kses( $value, array() ) )
  1640. );
  1641. }
  1642. }
  1643. // "Non-standard" fields
  1644. if ( $field_ids['extra'] ) {
  1645. // array indexed by field label (not field id)
  1646. $extra_fields = get_post_meta( $feedback_id, '_feedback_extra_fields', true );
  1647. /**
  1648. * Only get data for the compiled form if `$extra_fields` is a valid and non-empty array.
  1649. */
  1650. if ( is_array( $extra_fields ) && ! empty( $extra_fields ) ) {
  1651. $extra_field_keys = array_keys( $extra_fields );
  1652. $i = 0;
  1653. foreach ( $field_ids['extra'] as $field_id ) {
  1654. $field = $form->fields[ $field_id ];
  1655. $field_index = array_search( $field_id, $field_ids['all'] );
  1656. $label = $field->get_attribute( 'label' );
  1657. $compiled_form[ $field_index ] = sprintf(
  1658. '<b>%1$s:</b> %2$s<br /><br />',
  1659. wp_kses( $label, array() ),
  1660. nl2br( wp_kses( $extra_fields[ $extra_field_keys[ $i ] ], array() ) )
  1661. );
  1662. $i++;
  1663. }
  1664. }
  1665. }
  1666. // Sorting lines by the field index
  1667. ksort( $compiled_form );
  1668. return $compiled_form;
  1669. }
  1670. /**
  1671. * The contact-field shortcode processor
  1672. * We use an object method here instead of a static Grunion_Contact_Form_Field class method to parse contact-field shortcodes so that we can tie them to the contact-form object.
  1673. *
  1674. * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  1675. * @param string|null $content The shortcode's inner content: [contact-field]$content[/contact-field]
  1676. * @return HTML for the contact form field
  1677. */
  1678. static function parse_contact_field( $attributes, $content ) {
  1679. // Don't try to parse contact form fields if not inside a contact form
  1680. if ( ! Grunion_Contact_Form_Plugin::$using_contact_form_field ) {
  1681. $att_strs = array();
  1682. foreach ( $attributes as $att => $val ) {
  1683. if ( is_numeric( $att ) ) { // Is a valueless attribute
  1684. $att_strs[] = esc_html( $val );
  1685. } elseif ( isset( $val ) ) { // A regular attr - value pair
  1686. $att_strs[] = esc_html( $att ) . '=\'' . esc_html( $val ) . '\'';
  1687. }
  1688. }
  1689. $html = '[contact-field ' . implode( ' ', $att_strs );
  1690. if ( isset( $content ) && ! empty( $content ) ) { // If there is content, let's add a closing tag
  1691. $html .= ']' . esc_html( $content ) . '[/contact-field]';
  1692. } else { // Otherwise let's add a closing slash in the first tag
  1693. $html .= '/]';
  1694. }
  1695. return $html;
  1696. }
  1697. $form = Grunion_Contact_Form::$current_form;
  1698. $field = new Grunion_Contact_Form_Field( $attributes, $content, $form );
  1699. $field_id = $field->get_attribute( 'id' );
  1700. if ( $field_id ) {
  1701. $form->fields[ $field_id ] = $field;
  1702. } else {
  1703. $form->fields[] = $field;
  1704. }
  1705. if (
  1706. isset( $_POST['action'] ) && 'grunion-contact-form' === $_POST['action']
  1707. &&
  1708. isset( $_POST['contact-form-id'] ) && $form->get_attribute( 'id' ) == $_POST['contact-form-id']
  1709. &&
  1710. isset( $_POST['contact-form-hash'] ) && hash_equals( $form->hash, $_POST['contact-form-hash'] ) // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  1711. ) {
  1712. // If we're processing a POST submission for this contact form, validate the field value so we can show errors as necessary.
  1713. $field->validate();
  1714. }
  1715. // Output HTML
  1716. return $field->render();
  1717. }
  1718. /**
  1719. * Loops through $this->fields to generate a (structured) list of field IDs.
  1720. *
  1721. * Important: Currently the whitelisted fields are defined as follows:
  1722. * `name`, `email`, `url`, `subject`, `textarea`
  1723. *
  1724. * If you need to add new fields to the Contact Form, please don't add them
  1725. * to the whitelisted fields and leave them as extra fields.
  1726. *
  1727. * The reasoning behind this is that both the admin Feedback view and the CSV
  1728. * export will not include any fields that are added to the list of
  1729. * whitelisted fields without taking proper care to add them to all the
  1730. * other places where they accessed/used/saved.
  1731. *
  1732. * The safest way to add new fields is to add them to the dropdown and the
  1733. * HTML list ( @see Grunion_Contact_Form_Field::render ) and don't add them
  1734. * to the list of whitelisted fields. This way they will become a part of the
  1735. * `extra fields` which are saved in the post meta and will be properly
  1736. * handled by the admin Feedback view and the CSV Export without any extra
  1737. * work.
  1738. *
  1739. * If there is need to add a field to the whitelisted fields, then please
  1740. * take proper care to add logic to handle the field in the following places:
  1741. *
  1742. * - Below in the switch statement - so the field is recognized as whitelisted.
  1743. *
  1744. * - Grunion_Contact_Form::process_submission - validation and logic.
  1745. *
  1746. * - Grunion_Contact_Form::process_submission - add the field as an additional
  1747. * field in the `post_content` when saving the feedback content.
  1748. *
  1749. * - Grunion_Contact_Form_Plugin::parse_fields_from_content - add mapping
  1750. * for the field, defined in the above method.
  1751. *
  1752. * - Grunion_Contact_Form_Plugin::map_parsed_field_contents_of_post_to_field_names -
  1753. * add mapping of the field for the CSV Export. Otherwise it will be missing
  1754. * from the exported data.
  1755. *
  1756. * - admin.php / grunion_manage_post_columns - add the field to the render logic.
  1757. * Otherwise it will be missing from the admin Feedback view.
  1758. *
  1759. * @return array
  1760. */
  1761. function get_field_ids() {
  1762. $field_ids = array(
  1763. 'all' => array(), // array of all field_ids
  1764. 'extra' => array(), // array of all non-whitelisted field IDs
  1765. // Whitelisted "standard" field IDs:
  1766. // 'email' => field_id,
  1767. // 'name' => field_id,
  1768. // 'url' => field_id,
  1769. // 'subject' => field_id,
  1770. // 'textarea' => field_id,
  1771. );
  1772. foreach ( $this->fields as $id => $field ) {
  1773. $field_ids['all'][] = $id;
  1774. $type = $field->get_attribute( 'type' );
  1775. if ( isset( $field_ids[ $type ] ) ) {
  1776. // This type of field is already present in our whitelist of "standard" fields for this form
  1777. // Put it in extra
  1778. $field_ids['extra'][] = $id;
  1779. continue;
  1780. }
  1781. /**
  1782. * See method description before modifying the switch cases.
  1783. */
  1784. switch ( $type ) {
  1785. case 'email' :
  1786. case 'name' :
  1787. case 'url' :
  1788. case 'subject' :
  1789. case 'textarea' :
  1790. $field_ids[ $type ] = $id;
  1791. break;
  1792. default :
  1793. // Put everything else in extra
  1794. $field_ids['extra'][] = $id;
  1795. }
  1796. }
  1797. return $field_ids;
  1798. }
  1799. /**
  1800. * Process the contact form's POST submission
  1801. * Stores feedback. Sends email.
  1802. */
  1803. function process_submission() {
  1804. global $post;
  1805. $plugin = Grunion_Contact_Form_Plugin::init();
  1806. $id = $this->get_attribute( 'id' );
  1807. $to = $this->get_attribute( 'to' );
  1808. $widget = $this->get_attribute( 'widget' );
  1809. $contact_form_subject = $this->get_attribute( 'subject' );
  1810. $to = str_replace( ' ', '', $to );
  1811. $emails = explode( ',', $to );
  1812. $valid_emails = array();
  1813. foreach ( (array) $emails as $email ) {
  1814. if ( ! is_email( $email ) ) {
  1815. continue;
  1816. }
  1817. if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $email ) ) {
  1818. continue;
  1819. }
  1820. $valid_emails[] = $email;
  1821. }
  1822. // No one to send it to, which means none of the "to" attributes are valid emails.
  1823. // Use default email instead.
  1824. if ( ! $valid_emails ) {
  1825. $valid_emails = $this->defaults['to'];
  1826. }
  1827. $to = $valid_emails;
  1828. // Last ditch effort to set a recipient if somehow none have been set.
  1829. if ( empty( $to ) ) {
  1830. $to = get_option( 'admin_email' );
  1831. }
  1832. // Make sure we're processing the form we think we're processing... probably a redundant check.
  1833. if ( $widget ) {
  1834. if ( 'widget-' . $widget != $_POST['contact-form-id'] ) {
  1835. return false;
  1836. }
  1837. } else {
  1838. if ( $post->ID != $_POST['contact-form-id'] ) {
  1839. return false;
  1840. }
  1841. }
  1842. $field_ids = $this->get_field_ids();
  1843. // Initialize all these "standard" fields to null
  1844. $comment_author_email = $comment_author_email_label = // v
  1845. $comment_author = $comment_author_label = // v
  1846. $comment_author_url = $comment_author_url_label = // v
  1847. $comment_content = $comment_content_label = null;
  1848. // For each of the "standard" fields, grab their field label and value.
  1849. if ( isset( $field_ids['name'] ) ) {
  1850. $field = $this->fields[ $field_ids['name'] ];
  1851. $comment_author = Grunion_Contact_Form_Plugin::strip_tags(
  1852. stripslashes(
  1853. /** This filter is already documented in core/wp-includes/comment-functions.php */
  1854. apply_filters( 'pre_comment_author_name', addslashes( $field->value ) )
  1855. )
  1856. );
  1857. $comment_author_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  1858. }
  1859. if ( isset( $field_ids['email'] ) ) {
  1860. $field = $this->fields[ $field_ids['email'] ];
  1861. $comment_author_email = Grunion_Contact_Form_Plugin::strip_tags(
  1862. stripslashes(
  1863. /** This filter is already documented in core/wp-includes/comment-functions.php */
  1864. apply_filters( 'pre_comment_author_email', addslashes( $field->value ) )
  1865. )
  1866. );
  1867. $comment_author_email_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  1868. }
  1869. if ( isset( $field_ids['url'] ) ) {
  1870. $field = $this->fields[ $field_ids['url'] ];
  1871. $comment_author_url = Grunion_Contact_Form_Plugin::strip_tags(
  1872. stripslashes(
  1873. /** This filter is already documented in core/wp-includes/comment-functions.php */
  1874. apply_filters( 'pre_comment_author_url', addslashes( $field->value ) )
  1875. )
  1876. );
  1877. if ( 'http://' == $comment_author_url ) {
  1878. $comment_author_url = '';
  1879. }
  1880. $comment_author_url_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  1881. }
  1882. if ( isset( $field_ids['textarea'] ) ) {
  1883. $field = $this->fields[ $field_ids['textarea'] ];
  1884. $comment_content = trim( Grunion_Contact_Form_Plugin::strip_tags( $field->value ) );
  1885. $comment_content_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  1886. }
  1887. if ( isset( $field_ids['subject'] ) ) {
  1888. $field = $this->fields[ $field_ids['subject'] ];
  1889. if ( $field->value ) {
  1890. $contact_form_subject = Grunion_Contact_Form_Plugin::strip_tags( $field->value );
  1891. }
  1892. }
  1893. $all_values = $extra_values = array();
  1894. $i = 1; // Prefix counter for stored metadata
  1895. // For all fields, grab label and value
  1896. foreach ( $field_ids['all'] as $field_id ) {
  1897. $field = $this->fields[ $field_id ];
  1898. $label = $i . '_' . $field->get_attribute( 'label' );
  1899. $value = $field->value;
  1900. $all_values[ $label ] = $value;
  1901. $i++; // Increment prefix counter for the next field
  1902. }
  1903. // For the "non-standard" fields, grab label and value
  1904. // Extra fields have their prefix starting from count( $all_values ) + 1
  1905. foreach ( $field_ids['extra'] as $field_id ) {
  1906. $field = $this->fields[ $field_id ];
  1907. $label = $i . '_' . $field->get_attribute( 'label' );
  1908. $value = $field->value;
  1909. if ( is_array( $value ) ) {
  1910. $value = implode( ', ', $value );
  1911. }
  1912. $extra_values[ $label ] = $value;
  1913. $i++; // Increment prefix counter for the next extra field
  1914. }
  1915. $contact_form_subject = trim( $contact_form_subject );
  1916. $comment_author_IP = Grunion_Contact_Form_Plugin::get_ip_address();
  1917. $vars = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'contact_form_subject', 'comment_author_IP' );
  1918. foreach ( $vars as $var ) {
  1919. $$var = str_replace( array( "\n", "\r" ), '', $$var );
  1920. }
  1921. // Ensure that Akismet gets all of the relevant information from the contact form,
  1922. // not just the textarea field and predetermined subject.
  1923. $akismet_vars = compact( $vars );
  1924. $akismet_vars['comment_content'] = $comment_content;
  1925. foreach ( array_merge( $field_ids['all'], $field_ids['extra'] ) as $field_id ) {
  1926. $field = $this->fields[ $field_id ];
  1927. // Skip any fields that are just a choice from a pre-defined list. They wouldn't have any value
  1928. // from a spam-filtering point of view.
  1929. if ( in_array( $field->get_attribute( 'type' ), array( 'select', 'checkbox', 'checkbox-multiple', 'radio' ) ) ) {
  1930. continue;
  1931. }
  1932. // Normalize the label into a slug.
  1933. $field_slug = trim( // Strip all leading/trailing dashes.
  1934. preg_replace( // Normalize everything to a-z0-9_-
  1935. '/[^a-z0-9_]+/',
  1936. '-',
  1937. strtolower( $field->get_attribute( 'label' ) ) // Lowercase
  1938. ),
  1939. '-'
  1940. );
  1941. $field_value = ( is_array( $field->value ) ) ? trim( implode( ', ', $field->value ) ) : trim( $field->value );
  1942. // Skip any values that are already in the array we're sending.
  1943. if ( $field_value && in_array( $field_value, $akismet_vars ) ) {
  1944. continue;
  1945. }
  1946. $akismet_vars[ 'contact_form_field_' . $field_slug ] = $field_value;
  1947. }
  1948. $spam = '';
  1949. $akismet_values = $plugin->prepare_for_akismet( $akismet_vars );
  1950. // Is it spam?
  1951. /** This filter is already documented in modules/contact-form/admin.php */
  1952. $is_spam = apply_filters( 'jetpack_contact_form_is_spam', false, $akismet_values );
  1953. if ( is_wp_error( $is_spam ) ) { // WP_Error to abort
  1954. return $is_spam; // abort
  1955. } elseif ( $is_spam === true ) { // TRUE to flag a spam
  1956. $spam = '***SPAM*** ';
  1957. }
  1958. if ( ! $comment_author ) {
  1959. $comment_author = $comment_author_email;
  1960. }
  1961. /**
  1962. * Filter the email where a submitted feedback is sent.
  1963. *
  1964. * @module contact-form
  1965. *
  1966. * @since 1.3.1
  1967. *
  1968. * @param string|array $to Array of valid email addresses, or single email address.
  1969. */
  1970. $to = (array) apply_filters( 'contact_form_to', $to );
  1971. $reply_to_addr = $to[0]; // get just the address part before the name part is added
  1972. foreach ( $to as $to_key => $to_value ) {
  1973. $to[ $to_key ] = Grunion_Contact_Form_Plugin::strip_tags( $to_value );
  1974. $to[ $to_key ] = self::add_name_to_address( $to_value );
  1975. }
  1976. $blog_url = parse_url( site_url() );
  1977. $from_email_addr = 'wordpress@' . $blog_url['host'];
  1978. if ( ! empty( $comment_author_email ) ) {
  1979. $reply_to_addr = $comment_author_email;
  1980. }
  1981. $headers = 'From: "' . $comment_author . '" <' . $from_email_addr . ">\r\n" .
  1982. 'Reply-To: "' . $comment_author . '" <' . $reply_to_addr . ">\r\n";
  1983. // Build feedback reference
  1984. $feedback_time = current_time( 'mysql' );
  1985. $feedback_title = "{$comment_author} - {$feedback_time}";
  1986. $feedback_id = md5( $feedback_title );
  1987. $all_values = array_merge( $all_values, array(
  1988. 'entry_title' => the_title_attribute( 'echo=0' ),
  1989. 'entry_permalink' => esc_url( get_permalink( get_the_ID() ) ),
  1990. 'feedback_id' => $feedback_id,
  1991. ) );
  1992. /** This filter is already documented in modules/contact-form/admin.php */
  1993. $subject = apply_filters( 'contact_form_subject', $contact_form_subject, $all_values );
  1994. $url = $widget ? home_url( '/' ) : get_permalink( $post->ID );
  1995. $date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' );
  1996. $date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) );
  1997. $time = date_i18n( $date_time_format, current_time( 'timestamp' ) );
  1998. // keep a copy of the feedback as a custom post type
  1999. $feedback_status = $is_spam === true ? 'spam' : 'publish';
  2000. foreach ( (array) $akismet_values as $av_key => $av_value ) {
  2001. $akismet_values[ $av_key ] = Grunion_Contact_Form_Plugin::strip_tags( $av_value );
  2002. }
  2003. foreach ( (array) $all_values as $all_key => $all_value ) {
  2004. $all_values[ $all_key ] = Grunion_Contact_Form_Plugin::strip_tags( $all_value );
  2005. }
  2006. foreach ( (array) $extra_values as $ev_key => $ev_value ) {
  2007. $extra_values[ $ev_key ] = Grunion_Contact_Form_Plugin::strip_tags( $ev_value );
  2008. }
  2009. /*
  2010. We need to make sure that the post author is always zero for contact
  2011. * form submissions. This prevents export/import from trying to create
  2012. * new users based on form submissions from people who were logged in
  2013. * at the time.
  2014. *
  2015. * Unfortunately wp_insert_post() tries very hard to make sure the post
  2016. * author gets the currently logged in user id. That is how we ended up
  2017. * with this work around. */
  2018. add_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );
  2019. $post_id = wp_insert_post( array(
  2020. 'post_date' => addslashes( $feedback_time ),
  2021. 'post_type' => 'feedback',
  2022. 'post_status' => addslashes( $feedback_status ),
  2023. 'post_parent' => (int) $post->ID,
  2024. 'post_title' => addslashes( wp_kses( $feedback_title, array() ) ),
  2025. 'post_content' => addslashes( wp_kses( $comment_content . "\n<!--more-->\n" . "AUTHOR: {$comment_author}\nAUTHOR EMAIL: {$comment_author_email}\nAUTHOR URL: {$comment_author_url}\nSUBJECT: {$subject}\nIP: {$comment_author_IP}\n" . @print_r( $all_values, true ), array() ) ), // so that search will pick up this data
  2026. 'post_name' => $feedback_id,
  2027. ) );
  2028. // once insert has finished we don't need this filter any more
  2029. remove_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10 );
  2030. update_post_meta( $post_id, '_feedback_extra_fields', $this->addslashes_deep( $extra_values ) );
  2031. if ( 'publish' == $feedback_status ) {
  2032. // Increase count of unread feedback.
  2033. $unread = get_option( 'feedback_unread_count', 0 ) + 1;
  2034. update_option( 'feedback_unread_count', $unread );
  2035. }
  2036. if ( defined( 'AKISMET_VERSION' ) ) {
  2037. update_post_meta( $post_id, '_feedback_akismet_values', $this->addslashes_deep( $akismet_values ) );
  2038. }
  2039. $message = self::get_compiled_form( $post_id, $this );
  2040. array_push(
  2041. $message,
  2042. "<br />",
  2043. '<hr />',
  2044. __( 'Time:', 'jetpack' ) . ' ' . $time . '<br />',
  2045. __( 'IP Address:', 'jetpack' ) . ' ' . $comment_author_IP . '<br />',
  2046. __( 'Contact Form URL:', 'jetpack' ) . ' ' . $url . '<br />'
  2047. );
  2048. if ( is_user_logged_in() ) {
  2049. array_push(
  2050. $message,
  2051. sprintf(
  2052. '<p>' . __( 'Sent by a verified %s user.', 'jetpack' ) . '</p>',
  2053. isset( $GLOBALS['current_site']->site_name ) && $GLOBALS['current_site']->site_name ?
  2054. $GLOBALS['current_site']->site_name : '"' . get_option( 'blogname' ) . '"'
  2055. )
  2056. );
  2057. } else {
  2058. array_push( $message, '<p>' . __( 'Sent by an unverified visitor to your site.', 'jetpack' ) . '</p>' );
  2059. }
  2060. $message = join( $message, '' );
  2061. /**
  2062. * Filters the message sent via email after a successful form submission.
  2063. *
  2064. * @module contact-form
  2065. *
  2066. * @since 1.3.1
  2067. *
  2068. * @param string $message Feedback email message.
  2069. */
  2070. $message = apply_filters( 'contact_form_message', $message );
  2071. // This is called after `contact_form_message`, in order to preserve back-compat
  2072. $message = self::wrap_message_in_html_tags( $message );
  2073. update_post_meta( $post_id, '_feedback_email', $this->addslashes_deep( compact( 'to', 'message' ) ) );
  2074. /**
  2075. * Fires right before the contact form message is sent via email to
  2076. * the recipient specified in the contact form.
  2077. *
  2078. * @module contact-form
  2079. *
  2080. * @since 1.3.1
  2081. *
  2082. * @param integer $post_id Post contact form lives on
  2083. * @param array $all_values Contact form fields
  2084. * @param array $extra_values Contact form fields not included in $all_values
  2085. */
  2086. do_action( 'grunion_pre_message_sent', $post_id, $all_values, $extra_values );
  2087. // schedule deletes of old spam feedbacks
  2088. if ( ! wp_next_scheduled( 'grunion_scheduled_delete' ) ) {
  2089. wp_schedule_event( time() + 250, 'daily', 'grunion_scheduled_delete' );
  2090. }
  2091. if (
  2092. $is_spam !== true &&
  2093. /**
  2094. * Filter to choose whether an email should be sent after each successful contact form submission.
  2095. *
  2096. * @module contact-form
  2097. *
  2098. * @since 2.6.0
  2099. *
  2100. * @param bool true Should an email be sent after a form submission. Default to true.
  2101. * @param int $post_id Post ID.
  2102. */
  2103. true === apply_filters( 'grunion_should_send_email', true, $post_id )
  2104. ) {
  2105. self::wp_mail( $to, "{$spam}{$subject}", $message, $headers );
  2106. } elseif (
  2107. true === $is_spam &&
  2108. /**
  2109. * Choose whether an email should be sent for each spam contact form submission.
  2110. *
  2111. * @module contact-form
  2112. *
  2113. * @since 1.3.1
  2114. *
  2115. * @param bool false Should an email be sent after a spam form submission. Default to false.
  2116. */
  2117. apply_filters( 'grunion_still_email_spam', false ) == true
  2118. ) { // don't send spam by default. Filterable.
  2119. self::wp_mail( $to, "{$spam}{$subject}", $message, $headers );
  2120. }
  2121. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  2122. return self::success_message( $post_id, $this );
  2123. }
  2124. $redirect = wp_get_referer();
  2125. if ( ! $redirect ) { // wp_get_referer() returns false if the referer is the same as the current page
  2126. $redirect = $_SERVER['REQUEST_URI'];
  2127. }
  2128. $redirect = add_query_arg( urlencode_deep( array(
  2129. 'contact-form-id' => $id,
  2130. 'contact-form-sent' => $post_id,
  2131. 'contact-form-hash' => $this->hash,
  2132. '_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :(
  2133. ) ), $redirect );
  2134. /**
  2135. * Filter the URL where the reader is redirected after submitting a form.
  2136. *
  2137. * @module contact-form
  2138. *
  2139. * @since 1.9.0
  2140. *
  2141. * @param string $redirect Post submission URL.
  2142. * @param int $id Contact Form ID.
  2143. * @param int $post_id Post ID.
  2144. */
  2145. $redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id );
  2146. wp_safe_redirect( $redirect );
  2147. exit;
  2148. }
  2149. /**
  2150. * Wrapper for wp_mail() that enables HTML messages with text alternatives
  2151. *
  2152. * @param string|array $to Array or comma-separated list of email addresses to send message.
  2153. * @param string $subject Email subject.
  2154. * @param string $message Message contents.
  2155. * @param string|array $headers Optional. Additional headers.
  2156. * @param string|array $attachments Optional. Files to attach.
  2157. *
  2158. * @return bool Whether the email contents were sent successfully.
  2159. */
  2160. public static function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  2161. add_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
  2162. add_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );
  2163. $result = wp_mail( $to, $subject, $message, $headers, $attachments );
  2164. remove_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
  2165. remove_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );
  2166. return $result;
  2167. }
  2168. /**
  2169. * Add a display name part to an email address
  2170. *
  2171. * SpamAssassin doesn't like addresses in HTML messages that are missing display names (e.g., `foo@bar.org`
  2172. * instead of `"Foo Bar" <foo@bar.org>`.
  2173. *
  2174. * @param string $address
  2175. *
  2176. * @return string
  2177. */
  2178. function add_name_to_address( $address ) {
  2179. // If it's just the address, without a display name
  2180. if ( is_email( $address ) ) {
  2181. $address_parts = explode( '@', $address );
  2182. $address = sprintf( '"%s" <%s>', $address_parts[0], $address );
  2183. }
  2184. return $address;
  2185. }
  2186. /**
  2187. * Get the content type that should be assigned to outbound emails
  2188. *
  2189. * @return string
  2190. */
  2191. static function get_mail_content_type() {
  2192. return 'text/html';
  2193. }
  2194. /**
  2195. * Wrap a message body with the appropriate in HTML tags
  2196. *
  2197. * This helps to ensure correct parsing by clients, and also helps avoid triggering spam filtering rules
  2198. *
  2199. * @param string $body
  2200. *
  2201. * @return string
  2202. */
  2203. static function wrap_message_in_html_tags( $body ) {
  2204. // Don't do anything if the message was already wrapped in HTML tags
  2205. // That could have be done by a plugin via filters
  2206. if ( false !== strpos( $body, '<html' ) ) {
  2207. return $body;
  2208. }
  2209. $html_message = sprintf(
  2210. // The tabs are just here so that the raw code is correctly formatted for developers
  2211. // They're removed so that they don't affect the final message sent to users
  2212. str_replace( "\t", '',
  2213. "<!doctype html>
  2214. <html xmlns=\"http://www.w3.org/1999/xhtml\">
  2215. <body>
  2216. %s
  2217. </body>
  2218. </html>"
  2219. ),
  2220. $body
  2221. );
  2222. return $html_message;
  2223. }
  2224. /**
  2225. * Add a plain-text alternative part to an outbound email
  2226. *
  2227. * This makes the message more accessible to mail clients that aren't HTML-aware, and decreases the likelihood
  2228. * that the message will be flagged as spam.
  2229. *
  2230. * @param PHPMailer $phpmailer
  2231. */
  2232. static function add_plain_text_alternative( $phpmailer ) {
  2233. // Add an extra break so that the extra space above the <p> is preserved after the <p> is stripped out
  2234. $alt_body = str_replace( '<p>', '<p><br />', $phpmailer->Body );
  2235. // Convert <br> to \n breaks, to preserve the space between lines that we want to keep
  2236. $alt_body = str_replace( array( '<br>', '<br />' ), "\n", $alt_body );
  2237. // Convert <hr> to an plain-text equivalent, to preserve the integrity of the message
  2238. $alt_body = str_replace( array( "<hr>", "<hr />" ), "----\n", $alt_body );
  2239. // Trim the plain text message to remove the \n breaks that were after <doctype>, <html>, and <body>
  2240. $phpmailer->AltBody = trim( strip_tags( $alt_body ) );
  2241. }
  2242. function addslashes_deep( $value ) {
  2243. if ( is_array( $value ) ) {
  2244. return array_map( array( $this, 'addslashes_deep' ), $value );
  2245. } elseif ( is_object( $value ) ) {
  2246. $vars = get_object_vars( $value );
  2247. foreach ( $vars as $key => $data ) {
  2248. $value->{$key} = $this->addslashes_deep( $data );
  2249. }
  2250. return $value;
  2251. }
  2252. return addslashes( $value );
  2253. }
  2254. }
  2255. /**
  2256. * Class for the contact-field shortcode.
  2257. * Parses shortcode to output the contact form field as HTML.
  2258. * Validates input.
  2259. */
  2260. class Grunion_Contact_Form_Field extends Crunion_Contact_Form_Shortcode {
  2261. public $shortcode_name = 'contact-field';
  2262. /**
  2263. * @var Grunion_Contact_Form parent form
  2264. */
  2265. public $form;
  2266. /**
  2267. * @var string default or POSTed value
  2268. */
  2269. public $value;
  2270. /**
  2271. * @var bool Is the input invalid?
  2272. */
  2273. public $error = false;
  2274. /**
  2275. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
  2276. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
  2277. * @param Grunion_Contact_Form $form The parent form
  2278. */
  2279. function __construct( $attributes, $content = null, $form = null ) {
  2280. $attributes = shortcode_atts( array(
  2281. 'label' => null,
  2282. 'type' => 'text',
  2283. 'required' => false,
  2284. 'options' => array(),
  2285. 'id' => null,
  2286. 'default' => null,
  2287. 'values' => null,
  2288. 'placeholder' => null,
  2289. 'class' => null,
  2290. ), $attributes, 'contact-field' );
  2291. // special default for subject field
  2292. if ( 'subject' == $attributes['type'] && is_null( $attributes['default'] ) && ! is_null( $form ) ) {
  2293. $attributes['default'] = $form->get_attribute( 'subject' );
  2294. }
  2295. // allow required=1 or required=true
  2296. if ( '1' == $attributes['required'] || 'true' == strtolower( $attributes['required'] ) ) {
  2297. $attributes['required'] = true;
  2298. } else { $attributes['required'] = false;
  2299. }
  2300. // parse out comma-separated options list (for selects, radios, and checkbox-multiples)
  2301. if ( ! empty( $attributes['options'] ) && is_string( $attributes['options'] ) ) {
  2302. $attributes['options'] = array_map( 'trim', explode( ',', $attributes['options'] ) );
  2303. if ( ! empty( $attributes['values'] ) && is_string( $attributes['values'] ) ) {
  2304. $attributes['values'] = array_map( 'trim', explode( ',', $attributes['values'] ) );
  2305. }
  2306. }
  2307. if ( $form ) {
  2308. // make a unique field ID based on the label, with an incrementing number if needed to avoid clashes
  2309. $form_id = $form->get_attribute( 'id' );
  2310. $id = isset( $attributes['id'] ) ? $attributes['id'] : false;
  2311. $unescaped_label = $this->unesc_attr( $attributes['label'] );
  2312. $unescaped_label = str_replace( '%', '-', $unescaped_label ); // jQuery doesn't like % in IDs?
  2313. $unescaped_label = preg_replace( '/[^a-zA-Z0-9.-_:]/', '', $unescaped_label );
  2314. if ( empty( $id ) ) {
  2315. $id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label );
  2316. $i = 0;
  2317. $max_tries = 99;
  2318. while ( isset( $form->fields[ $id ] ) ) {
  2319. $i++;
  2320. $id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label . '-' . $i );
  2321. if ( $i > $max_tries ) {
  2322. break;
  2323. }
  2324. }
  2325. }
  2326. $attributes['id'] = $id;
  2327. }
  2328. parent::__construct( $attributes, $content );
  2329. // Store parent form
  2330. $this->form = $form;
  2331. }
  2332. /**
  2333. * This field's input is invalid. Flag as invalid and add an error to the parent form
  2334. *
  2335. * @param string $message The error message to display on the form.
  2336. */
  2337. function add_error( $message ) {
  2338. $this->is_error = true;
  2339. if ( ! is_wp_error( $this->form->errors ) ) {
  2340. $this->form->errors = new WP_Error;
  2341. }
  2342. $this->form->errors->add( $this->get_attribute( 'id' ), $message );
  2343. }
  2344. /**
  2345. * Is the field input invalid?
  2346. *
  2347. * @see $error
  2348. *
  2349. * @return bool
  2350. */
  2351. function is_error() {
  2352. return $this->error;
  2353. }
  2354. /**
  2355. * Validates the form input
  2356. */
  2357. function validate() {
  2358. // If it's not required, there's nothing to validate
  2359. if ( ! $this->get_attribute( 'required' ) ) {
  2360. return;
  2361. }
  2362. $field_id = $this->get_attribute( 'id' );
  2363. $field_type = $this->get_attribute( 'type' );
  2364. $field_label = $this->get_attribute( 'label' );
  2365. if ( isset( $_POST[ $field_id ] ) ) {
  2366. if ( is_array( $_POST[ $field_id ] ) ) {
  2367. $field_value = array_map( 'stripslashes', $_POST[ $field_id ] );
  2368. } else {
  2369. $field_value = stripslashes( $_POST[ $field_id ] );
  2370. }
  2371. } else {
  2372. $field_value = '';
  2373. }
  2374. switch ( $field_type ) {
  2375. case 'email' :
  2376. // Make sure the email address is valid
  2377. if ( ! is_email( $field_value ) ) {
  2378. /* translators: %s is the name of a form field */
  2379. $this->add_error( sprintf( __( '%s requires a valid email address', 'jetpack' ), $field_label ) );
  2380. }
  2381. break;
  2382. case 'checkbox-multiple' :
  2383. // Check that there is at least one option selected
  2384. if ( empty( $field_value ) ) {
  2385. /* translators: %s is the name of a form field */
  2386. $this->add_error( sprintf( __( '%s requires at least one selection', 'jetpack' ), $field_label ) );
  2387. }
  2388. break;
  2389. default :
  2390. // Just check for presence of any text
  2391. if ( ! strlen( trim( $field_value ) ) ) {
  2392. /* translators: %s is the name of a form field */
  2393. $this->add_error( sprintf( __( '%s is required', 'jetpack' ), $field_label ) );
  2394. }
  2395. }
  2396. }
  2397. /**
  2398. * Check the default value for options field
  2399. *
  2400. * @param string value
  2401. * @param int index
  2402. * @param string default value
  2403. *
  2404. * @return string
  2405. */
  2406. public function get_option_value( $value, $index, $options ) {
  2407. if ( empty( $value[ $index ] ) ) {
  2408. return $options;
  2409. }
  2410. return $value[ $index ];
  2411. }
  2412. /**
  2413. * Outputs the HTML for this form field
  2414. *
  2415. * @return string HTML
  2416. */
  2417. function render() {
  2418. global $current_user, $user_identity;
  2419. $r = '';
  2420. $field_id = $this->get_attribute( 'id' );
  2421. $field_type = $this->get_attribute( 'type' );
  2422. $field_label = $this->get_attribute( 'label' );
  2423. $field_required = $this->get_attribute( 'required' );
  2424. $placeholder = $this->get_attribute( 'placeholder' );
  2425. $class = 'date' === $field_type ? 'jp-contact-form-date' : $this->get_attribute( 'class' );
  2426. $field_placeholder = ( ! empty( $placeholder ) ) ? "placeholder='" . esc_attr( $placeholder ) . "'" : '';
  2427. $field_class = "class='" . trim( esc_attr( $field_type ) . ' ' . esc_attr( $class ) ) . "' ";
  2428. if ( isset( $_POST[ $field_id ] ) ) {
  2429. if ( is_array( $_POST[ $field_id ] ) ) {
  2430. $this->value = array_map( 'stripslashes', $_POST[ $field_id ] );
  2431. } else {
  2432. $this->value = stripslashes( (string) $_POST[ $field_id ] );
  2433. }
  2434. } elseif ( isset( $_GET[ $field_id ] ) ) {
  2435. $this->value = stripslashes( (string) $_GET[ $field_id ] );
  2436. } elseif (
  2437. is_user_logged_in() &&
  2438. ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
  2439. /**
  2440. * Allow third-party tools to prefill the contact form with the user's details when they're logged in.
  2441. *
  2442. * @module contact-form
  2443. *
  2444. * @since 3.2.0
  2445. *
  2446. * @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false.
  2447. */
  2448. true === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
  2449. )
  2450. ) {
  2451. // Special defaults for logged-in users
  2452. switch ( $this->get_attribute( 'type' ) ) {
  2453. case 'email' :
  2454. $this->value = $current_user->data->user_email;
  2455. break;
  2456. case 'name' :
  2457. $this->value = $user_identity;
  2458. break;
  2459. case 'url' :
  2460. $this->value = $current_user->data->user_url;
  2461. break;
  2462. default :
  2463. $this->value = $this->get_attribute( 'default' );
  2464. }
  2465. } else {
  2466. $this->value = $this->get_attribute( 'default' );
  2467. }
  2468. $field_value = Grunion_Contact_Form_Plugin::strip_tags( $this->value );
  2469. $field_label = Grunion_Contact_Form_Plugin::strip_tags( $field_label );
  2470. /**
  2471. * Filter the Contact Form required field text
  2472. *
  2473. * @module contact-form
  2474. *
  2475. * @since 3.8.0
  2476. *
  2477. * @param string $var Required field text. Default is "(required)".
  2478. */
  2479. $required_field_text = esc_html( apply_filters( 'jetpack_required_field_text', __( '(required)', 'jetpack' ) ) );
  2480. switch ( $field_type ) {
  2481. case 'email' :
  2482. $r .= "\n<div>\n";
  2483. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label email" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2484. $r .= "\t\t<input type='email' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' " . $field_class . $field_placeholder . ' ' . ( $field_required ? "required aria-required='true'" : '' ) . "/>\n";
  2485. $r .= "\t</div>\n";
  2486. break;
  2487. case 'telephone' :
  2488. $r .= "\n<div>\n";
  2489. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label telephone" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2490. $r .= "\t\t<input type='tel' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' " . $field_class . $field_placeholder . "/>\n";
  2491. break;
  2492. case 'url' :
  2493. $r .= "\n<div>\n";
  2494. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label url" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2495. $r .= "\t\t<input type='url' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' " . $field_class . $field_placeholder . ' ' . ( $field_required ? "required aria-required='true'" : '' ) . "/>\n";
  2496. $r .= "\t</div>\n";
  2497. break;
  2498. case 'textarea' :
  2499. $r .= "\n<div>\n";
  2500. $r .= "\t\t<label for='contact-form-comment-" . esc_attr( $field_id ) . "' class='grunion-field-label textarea" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2501. $r .= "\t\t<textarea name='" . esc_attr( $field_id ) . "' id='contact-form-comment-" . esc_attr( $field_id ) . "' rows='20' " . $field_class . $field_placeholder . ' ' . ( $field_required ? "required aria-required='true'" : '' ) . '>' . esc_textarea( $field_value ) . "</textarea>\n";
  2502. $r .= "\t</div>\n";
  2503. break;
  2504. case 'radio' :
  2505. $r .= "\t<div><label class='grunion-field-label" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2506. foreach ( $this->get_attribute( 'options' ) as $optionIndex => $option ) {
  2507. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  2508. $r .= "\t\t<label class='grunion-radio-label radio" . ( $this->is_error() ? ' form-error' : '' ) . "'>";
  2509. $r .= "<input type='radio' name='" . esc_attr( $field_id ) . "' value='" . esc_attr( $this->get_option_value( $this->get_attribute( 'values' ), $optionIndex, $option ) ) . "' " . $field_class . checked( $option, $field_value, false ) . ' ' . ( $field_required ? "required aria-required='true'" : '' ) . '/> ';
  2510. $r .= esc_html( $option ) . "</label>\n";
  2511. $r .= "\t\t<div class='clear-form'></div>\n";
  2512. }
  2513. $r .= "\t\t</div>\n";
  2514. break;
  2515. case 'checkbox' :
  2516. $r .= "\t<div>\n";
  2517. $r .= "\t\t<label class='grunion-field-label checkbox" . ( $this->is_error() ? ' form-error' : '' ) . "'>\n";
  2518. $r .= "\t\t<input type='checkbox' name='" . esc_attr( $field_id ) . "' value='" . esc_attr__( 'Yes', 'jetpack' ) . "' " . $field_class . checked( (bool) $field_value, true, false ) . ' ' . ( $field_required ? "required aria-required='true'" : '' ) . "/> \n";
  2519. $r .= "\t\t" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2520. $r .= "\t\t<div class='clear-form'></div>\n";
  2521. $r .= "\t</div>\n";
  2522. break;
  2523. case 'checkbox-multiple' :
  2524. $r .= "\t<div><label class='grunion-field-label" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2525. foreach ( $this->get_attribute( 'options' ) as $optionIndex => $option ) {
  2526. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  2527. $r .= "\t\t<label class='grunion-checkbox-multiple-label checkbox-multiple" . ( $this->is_error() ? ' form-error' : '' ) . "'>";
  2528. $r .= "<input type='checkbox' name='" . esc_attr( $field_id ) . "[]' value='" . esc_attr( $this->get_option_value( $this->get_attribute( 'values' ), $optionIndex, $option ) ) . "' " . $field_class . checked( in_array( $option, (array) $field_value ), true, false ) . ' /> ';
  2529. $r .= esc_html( $option ) . "</label>\n";
  2530. $r .= "\t\t<div class='clear-form'></div>\n";
  2531. }
  2532. $r .= "\t\t</div>\n";
  2533. break;
  2534. case 'select' :
  2535. $r .= "\n<div>\n";
  2536. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label select" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2537. $r .= "\t<select name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' " . $field_class . ( $field_required ? "required aria-required='true'" : '' ) . ">\n";
  2538. foreach ( $this->get_attribute( 'options' ) as $optionIndex => $option ) {
  2539. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  2540. $r .= "\t\t<option" . selected( $option, $field_value, false ) . " value='" . esc_attr( $this->get_option_value( $this->get_attribute( 'values' ), $optionIndex, $option ) ) . "'>" . esc_html( $option ) . "</option>\n";
  2541. }
  2542. $r .= "\t</select>\n";
  2543. $r .= "\t</div>\n";
  2544. break;
  2545. case 'date' :
  2546. $r .= "\n<div>\n";
  2547. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label " . esc_attr( $field_type ) . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2548. $r .= "\t\t<input type='text' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' " . $field_class . ( $field_required ? "required aria-required='true'" : '' ) . "/>\n";
  2549. $r .= "\t</div>\n";
  2550. wp_enqueue_script(
  2551. 'grunion-frontend',
  2552. Jetpack::get_file_url_for_environment(
  2553. '_inc/build/contact-form/js/grunion-frontend.min.js',
  2554. 'modules/contact-form/js/grunion-frontend.js'
  2555. ),
  2556. array( 'jquery', 'jquery-ui-datepicker' )
  2557. );
  2558. wp_enqueue_style( 'jp-jquery-ui-datepicker', plugins_url( 'css/jquery-ui-datepicker.css', __FILE__ ), array( 'dashicons' ), '1.0' );
  2559. // Using Core's built-in datepicker localization routine
  2560. wp_localize_jquery_ui_datepicker();
  2561. break;
  2562. default : // text field
  2563. // note that any unknown types will produce a text input, so we can use arbitrary type names to handle
  2564. // input fields like name, email, url that require special validation or handling at POST
  2565. $r .= "\n<div>\n";
  2566. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label " . esc_attr( $field_type ) . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . $required_field_text . '</span>' : '' ) . "</label>\n";
  2567. $r .= "\t\t<input type='text' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' " . $field_class . $field_placeholder . ' ' . ( $field_required ? "required aria-required='true'" : '' ) . "/>\n";
  2568. $r .= "\t</div>\n";
  2569. }
  2570. /**
  2571. * Filter the HTML of the Contact Form.
  2572. *
  2573. * @module contact-form
  2574. *
  2575. * @since 2.6.0
  2576. *
  2577. * @param string $r Contact Form HTML output.
  2578. * @param string $field_label Field label.
  2579. * @param int|null $id Post ID.
  2580. */
  2581. return apply_filters( 'grunion_contact_form_field_html', $r, $field_label, ( in_the_loop() ? get_the_ID() : null ) );
  2582. }
  2583. }
  2584. add_action( 'init', array( 'Grunion_Contact_Form_Plugin', 'init' ) );
  2585. add_action( 'grunion_scheduled_delete', 'grunion_delete_old_spam' );
  2586. /**
  2587. * Deletes old spam feedbacks to keep the posts table size under control
  2588. */
  2589. function grunion_delete_old_spam() {
  2590. global $wpdb;
  2591. $grunion_delete_limit = 100;
  2592. $now_gmt = current_time( 'mysql', 1 );
  2593. $sql = $wpdb->prepare( "
  2594. SELECT `ID`
  2595. FROM $wpdb->posts
  2596. WHERE DATE_SUB( %s, INTERVAL 15 DAY ) > `post_date_gmt`
  2597. AND `post_type` = 'feedback'
  2598. AND `post_status` = 'spam'
  2599. LIMIT %d
  2600. ", $now_gmt, $grunion_delete_limit );
  2601. $post_ids = $wpdb->get_col( $sql );
  2602. foreach ( (array) $post_ids as $post_id ) {
  2603. // force a full delete, skip the trash
  2604. wp_delete_post( $post_id, true );
  2605. }
  2606. if (
  2607. /**
  2608. * Filter if the module run OPTIMIZE TABLE on the core WP tables.
  2609. *
  2610. * @module contact-form
  2611. *
  2612. * @since 1.3.1
  2613. * @since 6.4.0 Set to false by default.
  2614. *
  2615. * @param bool $filter Should Jetpack optimize the table, defaults to false.
  2616. */
  2617. apply_filters( 'grunion_optimize_table', false )
  2618. ) {
  2619. $wpdb->query( "OPTIMIZE TABLE $wpdb->posts" );
  2620. }
  2621. // if we hit the max then schedule another run
  2622. if ( count( $post_ids ) >= $grunion_delete_limit ) {
  2623. wp_schedule_single_event( time() + 700, 'grunion_scheduled_delete' );
  2624. }
  2625. }