Açıklama Yok

grunion-contact-form.php 128KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Grunion Contact Form
  4. * Add a contact form to any post, page or text widget.
  5. * Emails will be sent to the post's author by default, or any email address you choose.
  6. *
  7. * @package automattic/jetpack
  8. */
  9. use Automattic\Jetpack\Assets;
  10. use Automattic\Jetpack\Blocks;
  11. use Automattic\Jetpack\Sync\Settings;
  12. define( 'GRUNION_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
  13. define( 'GRUNION_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  14. if ( is_admin() ) {
  15. require_once GRUNION_PLUGIN_DIR . 'admin.php';
  16. }
  17. add_action( 'rest_api_init', 'grunion_contact_form_require_endpoint' );
  18. function grunion_contact_form_require_endpoint() {
  19. require_once GRUNION_PLUGIN_DIR . 'class-grunion-contact-form-endpoint.php';
  20. }
  21. /**
  22. * Sets up various actions, filters, post types, post statuses, shortcodes.
  23. */
  24. class Grunion_Contact_Form_Plugin {
  25. /**
  26. * @var string The Widget ID of the widget currently being processed. Used to build the unique contact-form ID for forms embedded in widgets.
  27. */
  28. public $current_widget_id;
  29. static $using_contact_form_field = false;
  30. /**
  31. * @var int The last Feedback Post ID Erased as part of the Personal Data Eraser.
  32. * Helps with pagination.
  33. */
  34. private $pde_last_post_id_erased = 0;
  35. /**
  36. * @var string The email address for which we are deleting/exporting all feedbacks
  37. * as part of a Personal Data Eraser or Personal Data Exporter request.
  38. */
  39. private $pde_email_address = '';
  40. static function init() {
  41. static $instance = false;
  42. if ( ! $instance ) {
  43. $instance = new Grunion_Contact_Form_Plugin();
  44. // Schedule our daily cleanup
  45. add_action( 'wp_scheduled_delete', array( $instance, 'daily_akismet_meta_cleanup' ) );
  46. }
  47. return $instance;
  48. }
  49. /**
  50. * Runs daily to clean up spam detection metadata after 15 days. Keeps your DB squeaky clean.
  51. */
  52. public function daily_akismet_meta_cleanup() {
  53. global $wpdb;
  54. $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" );
  55. if ( empty( $feedback_ids ) ) {
  56. return;
  57. }
  58. /**
  59. * Fires right before deleting the _feedback_akismet_values post meta on $feedback_ids
  60. *
  61. * @module contact-form
  62. *
  63. * @since 6.1.0
  64. *
  65. * @param array $feedback_ids list of feedback post ID
  66. */
  67. do_action( 'jetpack_daily_akismet_meta_cleanup_before', $feedback_ids );
  68. foreach ( $feedback_ids as $feedback_id ) {
  69. delete_post_meta( $feedback_id, '_feedback_akismet_values' );
  70. }
  71. /**
  72. * Fires right after deleting the _feedback_akismet_values post meta on $feedback_ids
  73. *
  74. * @module contact-form
  75. *
  76. * @since 6.1.0
  77. *
  78. * @param array $feedback_ids list of feedback post ID
  79. */
  80. do_action( 'jetpack_daily_akismet_meta_cleanup_after', $feedback_ids );
  81. }
  82. /**
  83. * Strips HTML tags from input. Output is NOT HTML safe.
  84. *
  85. * @param mixed $data_with_tags
  86. * @return mixed
  87. */
  88. public static function strip_tags( $data_with_tags ) {
  89. if ( is_array( $data_with_tags ) ) {
  90. foreach ( $data_with_tags as $index => $value ) {
  91. $index = sanitize_text_field( (string) $index );
  92. $value = wp_kses( (string) $value, array() );
  93. $value = str_replace( '&amp;', '&', $value ); // undo damage done by wp_kses_normalize_entities()
  94. $data_without_tags[ $index ] = $value;
  95. }
  96. } else {
  97. $data_without_tags = wp_kses( $data_with_tags, array() );
  98. $data_without_tags = str_replace( '&amp;', '&', $data_without_tags ); // undo damage done by wp_kses_normalize_entities()
  99. }
  100. return $data_without_tags;
  101. }
  102. /**
  103. * Class uses singleton pattern; use Grunion_Contact_Form_Plugin::init() to initialize.
  104. */
  105. protected function __construct() {
  106. $this->add_shortcode();
  107. // While generating the output of a text widget with a contact-form shortcode, we need to know its widget ID.
  108. add_action( 'dynamic_sidebar', array( $this, 'track_current_widget' ) );
  109. // Add a "widget" shortcode attribute to all contact-form shortcodes embedded in widgets
  110. add_filter( 'widget_text', array( $this, 'widget_atts' ), 0 );
  111. // If Text Widgets don't get shortcode processed, hack ours into place.
  112. if (
  113. version_compare( get_bloginfo( 'version' ), '4.9-z', '<=' )
  114. && ! has_filter( 'widget_text', 'do_shortcode' )
  115. ) {
  116. add_filter( 'widget_text', array( $this, 'widget_shortcode_hack' ), 5 );
  117. }
  118. add_filter( 'jetpack_contact_form_is_spam', array( $this, 'is_spam_blocklist' ), 10, 2 );
  119. add_filter( 'jetpack_contact_form_in_comment_disallowed_list', array( $this, 'is_in_disallowed_list' ), 10, 2 );
  120. // Akismet to the rescue
  121. if ( defined( 'AKISMET_VERSION' ) || function_exists( 'akismet_http_post' ) ) {
  122. add_filter( 'jetpack_contact_form_is_spam', array( $this, 'is_spam_akismet' ), 10, 2 );
  123. add_action( 'contact_form_akismet', array( $this, 'akismet_submit' ), 10, 2 );
  124. }
  125. add_action( 'loop_start', array( 'Grunion_Contact_Form', '_style_on' ) );
  126. add_action( 'pre_amp_render_post', array( 'Grunion_Contact_Form', '_style_on' ) );
  127. add_action( 'wp_ajax_grunion-contact-form', array( $this, 'ajax_request' ) );
  128. add_action( 'wp_ajax_nopriv_grunion-contact-form', array( $this, 'ajax_request' ) );
  129. // GDPR: personal data exporter & eraser.
  130. add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ) );
  131. add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_personal_data_eraser' ) );
  132. // Export to CSV feature
  133. if ( is_admin() ) {
  134. add_action( 'admin_init', array( $this, 'download_feedback_as_csv' ) );
  135. add_action( 'admin_footer-edit.php', array( $this, 'export_form' ) );
  136. }
  137. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  138. add_action( 'current_screen', array( $this, 'unread_count' ) );
  139. add_filter( 'use_block_editor_for_post_type', array( $this, 'use_block_editor_for_post_type' ), 10, 2 );
  140. // custom post type we'll use to keep copies of the feedback items
  141. register_post_type(
  142. 'feedback', array(
  143. 'labels' => array(
  144. 'name' => __( 'Form Responses', 'jetpack' ),
  145. 'singular_name' => __( 'Form Responses', 'jetpack' ),
  146. 'search_items' => __( 'Search Responses', 'jetpack' ),
  147. 'not_found' => __( 'No responses found', 'jetpack' ),
  148. 'not_found_in_trash' => __( 'No responses found', 'jetpack' ),
  149. ),
  150. 'menu_icon' => 'dashicons-feedback',
  151. 'show_ui' => true,
  152. 'show_in_menu' => false,
  153. 'show_in_admin_bar' => false,
  154. 'public' => false,
  155. 'rewrite' => false,
  156. 'query_var' => false,
  157. 'capability_type' => 'page',
  158. 'show_in_rest' => true,
  159. 'rest_controller_class' => 'Grunion_Contact_Form_Endpoint',
  160. 'capabilities' => array(
  161. 'create_posts' => 'do_not_allow',
  162. 'publish_posts' => 'publish_pages',
  163. 'edit_posts' => 'edit_pages',
  164. 'edit_others_posts' => 'edit_others_pages',
  165. 'delete_posts' => 'delete_pages',
  166. 'delete_others_posts' => 'delete_others_pages',
  167. 'read_private_posts' => 'read_private_pages',
  168. 'edit_post' => 'edit_page',
  169. 'delete_post' => 'delete_page',
  170. 'read_post' => 'read_page',
  171. ),
  172. 'map_meta_cap' => true,
  173. )
  174. );
  175. // Add to REST API post type allowed list.
  176. add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_feedback_rest_api_type' ) );
  177. // Add "spam" as a post status
  178. register_post_status(
  179. 'spam', array(
  180. 'label' => 'Spam',
  181. 'public' => false,
  182. 'exclude_from_search' => true,
  183. 'show_in_admin_all_list' => false,
  184. 'label_count' => _n_noop( 'Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>', 'jetpack' ),
  185. 'protected' => true,
  186. '_builtin' => false,
  187. )
  188. );
  189. // POST handler
  190. if (
  191. isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] )
  192. &&
  193. isset( $_POST['action'] ) && 'grunion-contact-form' == $_POST['action']
  194. &&
  195. isset( $_POST['contact-form-id'] )
  196. ) {
  197. add_action( 'template_redirect', array( $this, 'process_form_submission' ) );
  198. }
  199. /*
  200. Can be dequeued by placing the following in wp-content/themes/yourtheme/functions.php
  201. *
  202. * function remove_grunion_style() {
  203. * wp_deregister_style('grunion.css');
  204. * }
  205. * add_action('wp_print_styles', 'remove_grunion_style');
  206. */
  207. wp_register_style( 'grunion.css', GRUNION_PLUGIN_URL . 'css/grunion.css', array(), JETPACK__VERSION );
  208. wp_style_add_data( 'grunion.css', 'rtl', 'replace' );
  209. self::register_contact_form_blocks();
  210. }
  211. private static function register_contact_form_blocks() {
  212. Blocks::jetpack_register_block(
  213. 'jetpack/contact-form',
  214. array(
  215. 'render_callback' => array( __CLASS__, 'gutenblock_render_form' ),
  216. )
  217. );
  218. // Field render methods.
  219. Blocks::jetpack_register_block(
  220. 'jetpack/field-text',
  221. array(
  222. 'parent' => array( 'jetpack/contact-form' ),
  223. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_text' ),
  224. )
  225. );
  226. Blocks::jetpack_register_block(
  227. 'jetpack/field-name',
  228. array(
  229. 'parent' => array( 'jetpack/contact-form' ),
  230. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_name' ),
  231. )
  232. );
  233. Blocks::jetpack_register_block(
  234. 'jetpack/field-email',
  235. array(
  236. 'parent' => array( 'jetpack/contact-form' ),
  237. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_email' ),
  238. )
  239. );
  240. Blocks::jetpack_register_block(
  241. 'jetpack/field-url',
  242. array(
  243. 'parent' => array( 'jetpack/contact-form' ),
  244. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_url' ),
  245. )
  246. );
  247. Blocks::jetpack_register_block(
  248. 'jetpack/field-date',
  249. array(
  250. 'parent' => array( 'jetpack/contact-form' ),
  251. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_date' ),
  252. )
  253. );
  254. Blocks::jetpack_register_block(
  255. 'jetpack/field-telephone',
  256. array(
  257. 'parent' => array( 'jetpack/contact-form' ),
  258. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_telephone' ),
  259. )
  260. );
  261. Blocks::jetpack_register_block(
  262. 'jetpack/field-textarea',
  263. array(
  264. 'parent' => array( 'jetpack/contact-form' ),
  265. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_textarea' ),
  266. )
  267. );
  268. Blocks::jetpack_register_block(
  269. 'jetpack/field-checkbox',
  270. array(
  271. 'parent' => array( 'jetpack/contact-form' ),
  272. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_checkbox' ),
  273. )
  274. );
  275. Blocks::jetpack_register_block(
  276. 'jetpack/field-checkbox-multiple',
  277. array(
  278. 'parent' => array( 'jetpack/contact-form' ),
  279. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_checkbox_multiple' ),
  280. )
  281. );
  282. Blocks::jetpack_register_block(
  283. 'jetpack/field-radio',
  284. array(
  285. 'parent' => array( 'jetpack/contact-form' ),
  286. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_radio' ),
  287. )
  288. );
  289. Blocks::jetpack_register_block(
  290. 'jetpack/field-select',
  291. array(
  292. 'parent' => array( 'jetpack/contact-form' ),
  293. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_select' ),
  294. )
  295. );
  296. Blocks::jetpack_register_block(
  297. 'jetpack/field-consent',
  298. array(
  299. 'parent' => array( 'jetpack/contact-form' ),
  300. 'render_callback' => array( __CLASS__, 'gutenblock_render_field_consent' ),
  301. )
  302. );
  303. }
  304. public static function gutenblock_render_form( $atts, $content ) {
  305. // Render fallback in other contexts than frontend (i.e. feed, emails, API, etc.), unless the form is being submitted.
  306. if ( ! jetpack_is_frontend() && ! isset( $_POST['contact-form-id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
  307. return sprintf(
  308. '<div class="%1$s"><a href="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a></div>',
  309. esc_attr( Blocks::classes( 'contact-form', $atts ) ),
  310. esc_url( get_the_permalink() ),
  311. esc_html__( 'Submit a form.', 'jetpack' )
  312. );
  313. }
  314. return Grunion_Contact_Form::parse( $atts, do_blocks( $content ) );
  315. }
  316. public static function block_attributes_to_shortcode_attributes( $atts, $type ) {
  317. $atts['type'] = $type;
  318. if ( isset( $atts['className'] ) ) {
  319. $atts['class'] = $atts['className'];
  320. unset( $atts['className'] );
  321. }
  322. if ( isset( $atts['defaultValue'] ) ) {
  323. $atts['default'] = $atts['defaultValue'];
  324. unset( $atts['defaultValue'] );
  325. }
  326. return $atts;
  327. }
  328. public static function gutenblock_render_field_text( $atts, $content ) {
  329. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'text' );
  330. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  331. }
  332. public static function gutenblock_render_field_name( $atts, $content ) {
  333. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'name' );
  334. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  335. }
  336. public static function gutenblock_render_field_email( $atts, $content ) {
  337. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'email' );
  338. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  339. }
  340. public static function gutenblock_render_field_url( $atts, $content ) {
  341. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'url' );
  342. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  343. }
  344. public static function gutenblock_render_field_date( $atts, $content ) {
  345. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'date' );
  346. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  347. }
  348. public static function gutenblock_render_field_telephone( $atts, $content ) {
  349. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'telephone' );
  350. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  351. }
  352. public static function gutenblock_render_field_textarea( $atts, $content ) {
  353. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'textarea' );
  354. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  355. }
  356. public static function gutenblock_render_field_checkbox( $atts, $content ) {
  357. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'checkbox' );
  358. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  359. }
  360. public static function gutenblock_render_field_checkbox_multiple( $atts, $content ) {
  361. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'checkbox-multiple' );
  362. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  363. }
  364. public static function gutenblock_render_field_radio( $atts, $content ) {
  365. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'radio' );
  366. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  367. }
  368. public static function gutenblock_render_field_select( $atts, $content ) {
  369. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'select' );
  370. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  371. }
  372. /**
  373. * Render the consent field.
  374. *
  375. * @param string $atts consent attributes.
  376. * @param string $content html content.
  377. */
  378. public static function gutenblock_render_field_consent( $atts, $content ) {
  379. $atts = self::block_attributes_to_shortcode_attributes( $atts, 'consent' );
  380. if ( ! isset( $atts['implicitConsentMessage'] ) ) {
  381. $atts['implicitConsentMessage'] = __( "By submitting your information, you're giving us permission to email you. You may unsubscribe at any time.", 'jetpack' );
  382. }
  383. if ( ! isset( $atts['explicitConsentMessage'] ) ) {
  384. $atts['explicitConsentMessage'] = __( 'Can we send you an email from time to time?', 'jetpack' );
  385. }
  386. return Grunion_Contact_Form::parse_contact_field( $atts, $content );
  387. }
  388. /**
  389. * Add the 'Form Responses' menu item as a submenu of Feedback.
  390. */
  391. public function admin_menu() {
  392. $slug = 'feedback';
  393. add_menu_page(
  394. __( 'Feedback', 'jetpack' ),
  395. __( 'Feedback', 'jetpack' ),
  396. 'edit_pages',
  397. $slug,
  398. null,
  399. 'dashicons-feedback',
  400. 45
  401. );
  402. add_submenu_page(
  403. $slug,
  404. __( 'Form Responses', 'jetpack' ),
  405. __( 'Form Responses', 'jetpack' ),
  406. 'edit_pages',
  407. 'edit.php?post_type=feedback',
  408. null,
  409. 0
  410. );
  411. remove_submenu_page(
  412. $slug,
  413. $slug
  414. );
  415. }
  416. /**
  417. * Add to REST API post type allowed list.
  418. */
  419. function allow_feedback_rest_api_type( $post_types ) {
  420. $post_types[] = 'feedback';
  421. return $post_types;
  422. }
  423. /**
  424. * Display the count of new feedback entries received. It's reset when user visits the Feedback screen.
  425. *
  426. * @since 4.1.0
  427. *
  428. * @param object $screen Information about the current screen.
  429. */
  430. function unread_count( $screen ) {
  431. if ( isset( $screen->post_type ) && 'feedback' == $screen->post_type ) {
  432. update_option( 'feedback_unread_count', 0 );
  433. } else {
  434. global $submenu;
  435. if ( isset( $submenu['feedback'] ) && is_array( $submenu['feedback'] ) && ! empty( $submenu['feedback'] ) ) {
  436. foreach ( $submenu['feedback'] as $index => $menu_item ) {
  437. if ( 'edit.php?post_type=feedback' == $menu_item[2] ) {
  438. $unread = get_option( 'feedback_unread_count', 0 );
  439. if ( $unread > 0 ) {
  440. $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>' : '';
  441. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  442. $submenu['feedback'][ $index ][0] .= $unread_count;
  443. }
  444. break;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. /**
  451. * Handles all contact-form POST submissions
  452. *
  453. * Conditionally attached to `template_redirect`
  454. */
  455. function process_form_submission() {
  456. // Add a filter to replace tokens in the subject field with sanitized field values
  457. add_filter( 'contact_form_subject', array( $this, 'replace_tokens_with_input' ), 10, 2 );
  458. $id = stripslashes( $_POST['contact-form-id'] );
  459. $hash = isset( $_POST['contact-form-hash'] ) ? $_POST['contact-form-hash'] : '';
  460. $hash = preg_replace( '/[^\da-f]/i', '', $hash );
  461. if ( ! is_string( $id ) || ! is_string( $hash ) ) {
  462. return false;
  463. }
  464. if ( is_user_logged_in() ) {
  465. check_admin_referer( "contact-form_{$id}" );
  466. }
  467. $is_widget = 0 === strpos( $id, 'widget-' );
  468. $form = false;
  469. if ( $is_widget ) {
  470. // It's a form embedded in a text widget
  471. $this->current_widget_id = substr( $id, 7 ); // remove "widget-"
  472. $widget_type = implode( '-', array_slice( explode( '-', $this->current_widget_id ), 0, -1 ) ); // Remove trailing -#
  473. // Is the widget active?
  474. $sidebar = is_active_widget( false, $this->current_widget_id, $widget_type );
  475. // This is lame - no core API for getting a widget by ID
  476. $widget = isset( $GLOBALS['wp_registered_widgets'][ $this->current_widget_id ] ) ? $GLOBALS['wp_registered_widgets'][ $this->current_widget_id ] : false;
  477. if ( $sidebar && $widget && isset( $widget['callback'] ) ) {
  478. // prevent PHP notices by populating widget args
  479. $widget_args = array(
  480. 'before_widget' => '',
  481. 'after_widget' => '',
  482. 'before_title' => '',
  483. 'after_title' => '',
  484. );
  485. // This is lamer - no API for outputting a given widget by ID
  486. ob_start();
  487. // Process the widget to populate Grunion_Contact_Form::$last
  488. call_user_func( $widget['callback'], $widget_args, $widget['params'][0] );
  489. ob_end_clean();
  490. }
  491. } else {
  492. // It's a form embedded in a post
  493. $post = get_post( $id );
  494. // Process the content to populate Grunion_Contact_Form::$last
  495. if ( $post ) {
  496. /** This filter is already documented in core. wp-includes/post-template.php */
  497. apply_filters( 'the_content', $post->post_content );
  498. }
  499. }
  500. $form = isset( Grunion_Contact_Form::$forms[ $hash ] ) ? Grunion_Contact_Form::$forms[ $hash ] : null;
  501. // No form may mean user is using do_shortcode, grab the form using the stored post meta
  502. if ( ! $form && is_numeric( $id ) && $hash ) {
  503. // Get shortcode from post meta
  504. $shortcode = get_post_meta( $id, "_g_feedback_shortcode_{$hash}", true );
  505. // Format it
  506. if ( $shortcode != '' ) {
  507. // Get attributes from post meta.
  508. $parameters = '';
  509. $attributes = get_post_meta( $id, "_g_feedback_shortcode_atts_{$hash}", true );
  510. if ( ! empty( $attributes ) && is_array( $attributes ) ) {
  511. foreach ( array_filter( $attributes ) as $param => $value ) {
  512. $parameters .= " $param=\"$value\"";
  513. }
  514. }
  515. $shortcode = '[contact-form' . $parameters . ']' . $shortcode . '[/contact-form]';
  516. do_shortcode( $shortcode );
  517. // Recreate form
  518. $form = Grunion_Contact_Form::$last;
  519. }
  520. }
  521. if ( ! $form ) {
  522. return false;
  523. }
  524. if ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) {
  525. return $form->errors;
  526. }
  527. // Process the form
  528. return $form->process_submission();
  529. }
  530. function ajax_request() {
  531. $submission_result = self::process_form_submission();
  532. if ( ! $submission_result ) {
  533. header( 'HTTP/1.1 500 Server Error', 500, true );
  534. echo '<div class="form-error"><ul class="form-errors"><li class="form-error-message">';
  535. esc_html_e( 'An error occurred. Please try again later.', 'jetpack' );
  536. echo '</li></ul></div>';
  537. } elseif ( is_wp_error( $submission_result ) ) {
  538. header( 'HTTP/1.1 400 Bad Request', 403, true );
  539. echo '<div class="form-error"><ul class="form-errors"><li class="form-error-message">';
  540. echo esc_html( $submission_result->get_error_message() );
  541. echo '</li></ul></div>';
  542. } else {
  543. echo '<h3>' . esc_html__( 'Message Sent', 'jetpack' ) . '</h3>' . $submission_result;
  544. }
  545. die;
  546. }
  547. /**
  548. * Ensure the post author is always zero for contact-form feedbacks
  549. * Attached to `wp_insert_post_data`
  550. *
  551. * @see Grunion_Contact_Form::process_submission()
  552. *
  553. * @param array $data the data to insert
  554. * @param array $postarr the data sent to wp_insert_post()
  555. * @return array The filtered $data to insert
  556. */
  557. function insert_feedback_filter( $data, $postarr ) {
  558. if ( $data['post_type'] == 'feedback' && $postarr['post_type'] == 'feedback' ) {
  559. $data['post_author'] = 0;
  560. }
  561. return $data;
  562. }
  563. /*
  564. * Adds our contact-form shortcode
  565. * The "child" contact-field shortcode is enabled as needed by the contact-form shortcode handler
  566. */
  567. function add_shortcode() {
  568. add_shortcode( 'contact-form', array( 'Grunion_Contact_Form', 'parse' ) );
  569. add_shortcode( 'contact-field', array( 'Grunion_Contact_Form', 'parse_contact_field' ) );
  570. }
  571. static function tokenize_label( $label ) {
  572. return '{' . trim( preg_replace( '#^\d+_#', '', $label ) ) . '}';
  573. }
  574. static function sanitize_value( $value ) {
  575. return preg_replace( '=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $value );
  576. }
  577. /**
  578. * Replaces tokens like {city} or {City} (case insensitive) with the value
  579. * of an input field of that name
  580. *
  581. * @param string $subject
  582. * @param array $field_values Array with field label => field value associations
  583. *
  584. * @return string The filtered $subject with the tokens replaced
  585. */
  586. function replace_tokens_with_input( $subject, $field_values ) {
  587. // Wrap labels into tokens (inside {})
  588. $wrapped_labels = array_map( array( 'Grunion_Contact_Form_Plugin', 'tokenize_label' ), array_keys( $field_values ) );
  589. // Sanitize all values
  590. $sanitized_values = array_map( array( 'Grunion_Contact_Form_Plugin', 'sanitize_value' ), array_values( $field_values ) );
  591. foreach ( $sanitized_values as $k => $sanitized_value ) {
  592. if ( is_array( $sanitized_value ) ) {
  593. $sanitized_values[ $k ] = implode( ', ', $sanitized_value );
  594. }
  595. }
  596. // Search for all valid tokens (based on existing fields) and replace with the field's value
  597. $subject = str_ireplace( $wrapped_labels, $sanitized_values, $subject );
  598. return $subject;
  599. }
  600. /**
  601. * Tracks the widget currently being processed.
  602. * Attached to `dynamic_sidebar`
  603. *
  604. * @see $current_widget_id
  605. *
  606. * @param array $widget The widget data
  607. */
  608. function track_current_widget( $widget ) {
  609. $this->current_widget_id = $widget['id'];
  610. }
  611. /**
  612. * Adds a "widget" attribute to every contact-form embedded in a text widget.
  613. * Used to tell the difference between post-embedded contact-forms and widget-embedded contact-forms
  614. * Attached to `widget_text`
  615. *
  616. * @param string $text The widget text
  617. * @return string The filtered widget text
  618. */
  619. function widget_atts( $text ) {
  620. Grunion_Contact_Form::style( true );
  621. return preg_replace( '/\[contact-form([^a-zA-Z_-])/', '[contact-form widget="' . $this->current_widget_id . '"\\1', $text );
  622. }
  623. /**
  624. * For sites where text widgets are not processed for shortcodes, we add this hack to process just our shortcode
  625. * Attached to `widget_text`
  626. *
  627. * @param string $text The widget text
  628. * @return string The contact-form filtered widget text
  629. */
  630. function widget_shortcode_hack( $text ) {
  631. if ( ! preg_match( '/\[contact-form([^a-zA-Z_-])/', $text ) ) {
  632. return $text;
  633. }
  634. $old = $GLOBALS['shortcode_tags'];
  635. remove_all_shortcodes();
  636. Grunion_Contact_Form_Plugin::$using_contact_form_field = true;
  637. $this->add_shortcode();
  638. $text = do_shortcode( $text );
  639. Grunion_Contact_Form_Plugin::$using_contact_form_field = false;
  640. $GLOBALS['shortcode_tags'] = $old;
  641. return $text;
  642. }
  643. /**
  644. * Check if a submission matches the Comment Blocklist.
  645. * The Comment Blocklist is a means to moderate discussion, and contact
  646. * forms are 1:1 discussion forums, ripe for abuse by users who are being
  647. * removed from the public discussion.
  648. * Attached to `jetpack_contact_form_is_spam`
  649. *
  650. * @param bool $is_spam
  651. * @param array $form
  652. * @return bool TRUE => spam, FALSE => not spam
  653. */
  654. public function is_spam_blocklist( $is_spam, $form = array() ) {
  655. if ( $is_spam ) {
  656. return $is_spam;
  657. }
  658. return $this->is_in_disallowed_list( false, $form );
  659. }
  660. /**
  661. * Check if a submission matches the comment disallowed list.
  662. * Attached to `jetpack_contact_form_in_comment_disallowed_list`.
  663. *
  664. * @param boolean $in_disallowed_list Whether the feedback is in the disallowed list.
  665. * @param array $form The form array.
  666. * @return bool Returns true if the form submission matches the disallowed list and false if it doesn't.
  667. */
  668. public function is_in_disallowed_list( $in_disallowed_list, $form = array() ) {
  669. if ( $in_disallowed_list ) {
  670. return $in_disallowed_list;
  671. }
  672. if (
  673. wp_check_comment_disallowed_list(
  674. $form['comment_author'],
  675. $form['comment_author_email'],
  676. $form['comment_author_url'],
  677. $form['comment_content'],
  678. $form['user_ip'],
  679. $form['user_agent']
  680. )
  681. ) {
  682. return true;
  683. }
  684. return false;
  685. }
  686. /**
  687. * Populate an array with all values necessary to submit a NEW contact-form feedback to Akismet.
  688. * Note that this includes the current user_ip etc, so this should only be called when accepting a new item via $_POST
  689. *
  690. * @param array $form Contact form feedback array
  691. * @return array feedback array with additional data ready for submission to Akismet
  692. */
  693. function prepare_for_akismet( $form ) {
  694. $form['comment_type'] = 'contact_form';
  695. $form['user_ip'] = $_SERVER['REMOTE_ADDR'];
  696. $form['user_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
  697. $form['referrer'] = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
  698. $form['blog'] = get_option( 'home' );
  699. foreach ( $_SERVER as $key => $value ) {
  700. if ( ! is_string( $value ) ) {
  701. continue;
  702. }
  703. if ( in_array( $key, array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'HTTP_USER_AGENT', 'HTTP_REFERER' ) ) ) {
  704. // We don't care about cookies, and the UA and Referrer were caught above.
  705. continue;
  706. } elseif ( in_array( $key, array( 'REMOTE_ADDR', 'REQUEST_URI', 'DOCUMENT_URI' ) ) ) {
  707. // All three of these are relevant indicators and should be passed along.
  708. $form[ $key ] = $value;
  709. } elseif ( wp_startswith( $key, 'HTTP_' ) ) {
  710. // Any other HTTP header indicators.
  711. // `wp_startswith()` is a wpcom helper function and is included in Jetpack via `functions.compat.php`
  712. $form[ $key ] = $value;
  713. }
  714. }
  715. /**
  716. * Filter the values that are sent to Akismet for the spam check.
  717. *
  718. * @module contact-form
  719. *
  720. * @since 10.2.0
  721. *
  722. * @param array $form The form values being sent to Akismet.
  723. */
  724. return apply_filters( 'jetpack_contact_form_akismet_values', $form );
  725. }
  726. /**
  727. * Submit contact-form data to Akismet to check for spam.
  728. * If you're accepting a new item via $_POST, run it Grunion_Contact_Form_Plugin::prepare_for_akismet() first
  729. * Attached to `jetpack_contact_form_is_spam`
  730. *
  731. * @param bool $is_spam
  732. * @param array $form
  733. * @return bool|WP_Error TRUE => spam, FALSE => not spam, WP_Error => stop processing entirely
  734. */
  735. function is_spam_akismet( $is_spam, $form = array() ) {
  736. global $akismet_api_host, $akismet_api_port;
  737. // The signature of this function changed from accepting just $form.
  738. // If something only sends an array, assume it's still using the old
  739. // signature and work around it.
  740. if ( empty( $form ) && is_array( $is_spam ) ) {
  741. $form = $is_spam;
  742. $is_spam = false;
  743. }
  744. // If a previous filter has alrady marked this as spam, trust that and move on.
  745. if ( $is_spam ) {
  746. return $is_spam;
  747. }
  748. if ( ! function_exists( 'akismet_http_post' ) && ! defined( 'AKISMET_VERSION' ) ) {
  749. return false;
  750. }
  751. $query_string = http_build_query( $form );
  752. if ( method_exists( 'Akismet', 'http_post' ) ) {
  753. $response = Akismet::http_post( $query_string, 'comment-check' );
  754. } else {
  755. $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  756. }
  757. $result = false;
  758. if ( isset( $response[0]['x-akismet-pro-tip'] ) && 'discard' === trim( $response[0]['x-akismet-pro-tip'] ) && get_option( 'akismet_strictness' ) === '1' ) {
  759. $result = new WP_Error( 'feedback-discarded', __( 'Feedback discarded.', 'jetpack' ) );
  760. } elseif ( isset( $response[1] ) && 'true' == trim( $response[1] ) ) { // 'true' is spam
  761. $result = true;
  762. }
  763. /**
  764. * Filter the results returned by Akismet for each submitted contact form.
  765. *
  766. * @module contact-form
  767. *
  768. * @since 1.3.1
  769. *
  770. * @param WP_Error|bool $result Is the submitted feedback spam.
  771. * @param array|bool $form Submitted feedback.
  772. */
  773. return apply_filters( 'contact_form_is_spam_akismet', $result, $form );
  774. }
  775. /**
  776. * Submit a feedback as either spam or ham
  777. *
  778. * @param string $as Either 'spam' or 'ham'.
  779. * @param array $form the contact-form data
  780. */
  781. function akismet_submit( $as, $form ) {
  782. global $akismet_api_host, $akismet_api_port;
  783. if ( ! in_array( $as, array( 'ham', 'spam' ) ) ) {
  784. return false;
  785. }
  786. $query_string = '';
  787. if ( is_array( $form ) ) {
  788. $query_string = http_build_query( $form );
  789. }
  790. if ( method_exists( 'Akismet', 'http_post' ) ) {
  791. $response = Akismet::http_post( $query_string, "submit-{$as}" );
  792. } else {
  793. $response = akismet_http_post( $query_string, $akismet_api_host, "/1.1/submit-{$as}", $akismet_api_port );
  794. }
  795. return trim( $response[1] );
  796. }
  797. /**
  798. * Prints the menu
  799. */
  800. function export_form() {
  801. $current_screen = get_current_screen();
  802. if ( ! in_array( $current_screen->id, array( 'edit-feedback', 'feedback_page_feedback-export' ) ) ) {
  803. return;
  804. }
  805. if ( ! current_user_can( 'export' ) ) {
  806. return;
  807. }
  808. // if there aren't any feedbacks, bail out
  809. if ( ! (int) wp_count_posts( 'feedback' )->publish ) {
  810. return;
  811. }
  812. ?>
  813. <div id="feedback-export" style="display:none">
  814. <h2><?php esc_html_e( 'Export responses as CSV', 'jetpack' ); ?></h2>
  815. <div class="clear"></div>
  816. <form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post" class="form">
  817. <?php wp_nonce_field( 'feedback_export', 'feedback_export_nonce' ); ?>
  818. <input name="action" value="feedback_export" type="hidden">
  819. <label for="post"><?php esc_html_e( 'Select responses to download', 'jetpack' ); ?></label>
  820. <select name="post">
  821. <option value="all"><?php esc_html_e( 'All posts', 'jetpack' ); ?></option>
  822. <?php echo $this->get_feedbacks_as_options(); ?>
  823. </select>
  824. <br><br>
  825. <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Download', 'jetpack' ); ?>">
  826. </form>
  827. </div>
  828. <?php
  829. // There aren't any usable actions in core to output the "export feedback" form in the correct place,
  830. // so this inline JS moves it from the top of the page to the bottom.
  831. ?>
  832. <script type='text/javascript'>
  833. var menu = document.getElementById( 'feedback-export' ),
  834. wrapper = document.getElementsByClassName( 'wrap' )[0];
  835. <?php if ( 'edit-feedback' === $current_screen->id ) : ?>
  836. wrapper.appendChild(menu);
  837. <?php endif; ?>
  838. menu.style.display = 'block';
  839. </script>
  840. <?php
  841. }
  842. /**
  843. * Fetch post content for a post and extract just the comment.
  844. *
  845. * @param int $post_id The post id to fetch the content for.
  846. *
  847. * @return string Trimmed post comment.
  848. *
  849. * @codeCoverageIgnore
  850. */
  851. public function get_post_content_for_csv_export( $post_id ) {
  852. $post_content = get_post_field( 'post_content', $post_id );
  853. $content = explode( '<!--more-->', $post_content );
  854. return trim( $content[0] );
  855. }
  856. /**
  857. * Get `_feedback_extra_fields` field from post meta data.
  858. *
  859. * @param int $post_id Id of the post to fetch meta data for.
  860. *
  861. * @return mixed
  862. */
  863. public function get_post_meta_for_csv_export( $post_id ) {
  864. $md = get_post_meta( $post_id, '_feedback_extra_fields', true );
  865. $md['feedback_date'] = get_the_date( DATE_RFC3339, $post_id );
  866. $content_fields = self::parse_fields_from_content( $post_id );
  867. $md['feedback_ip'] = ( isset( $content_fields['_feedback_ip'] ) ) ? $content_fields['_feedback_ip'] : 0;
  868. // add the email_marketing_consent to the post meta.
  869. $md['email_marketing_consent'] = 0;
  870. if ( isset( $content_fields['_feedback_all_fields'] ) ) {
  871. $all_fields = $content_fields['_feedback_all_fields'];
  872. // check if the email_marketing_consent field exists.
  873. if ( isset( $all_fields['email_marketing_consent'] ) ) {
  874. $md['email_marketing_consent'] = $all_fields['email_marketing_consent'];
  875. }
  876. }
  877. return $md;
  878. }
  879. /**
  880. * Get parsed feedback post fields.
  881. *
  882. * @param int $post_id Id of the post to fetch parsed contents for.
  883. *
  884. * @return array
  885. *
  886. * @codeCoverageIgnore - No need to be covered.
  887. */
  888. public function get_parsed_field_contents_of_post( $post_id ) {
  889. return self::parse_fields_from_content( $post_id );
  890. }
  891. /**
  892. * Properly maps fields that are missing from the post meta data
  893. * to names, that are similar to those of the post meta.
  894. *
  895. * @param array $parsed_post_content Parsed post content
  896. *
  897. * @see parse_fields_from_content for how the input data is generated.
  898. *
  899. * @return array Mapped fields.
  900. */
  901. public function map_parsed_field_contents_of_post_to_field_names( $parsed_post_content ) {
  902. $mapped_fields = array();
  903. $field_mapping = array(
  904. '_feedback_subject' => __( 'Contact Form', 'jetpack' ),
  905. '_feedback_author' => '1_Name',
  906. '_feedback_author_email' => '2_Email',
  907. '_feedback_author_url' => '3_Website',
  908. '_feedback_main_comment' => '4_Comment',
  909. '_feedback_author_ip' => '5_IP',
  910. );
  911. foreach ( $field_mapping as $parsed_field_name => $field_name ) {
  912. if (
  913. isset( $parsed_post_content[ $parsed_field_name ] )
  914. && ! empty( $parsed_post_content[ $parsed_field_name ] )
  915. ) {
  916. $mapped_fields[ $field_name ] = $parsed_post_content[ $parsed_field_name ];
  917. }
  918. }
  919. return $mapped_fields;
  920. }
  921. /**
  922. * Registers the personal data exporter.
  923. *
  924. * @since 6.1.1
  925. *
  926. * @param array $exporters An array of personal data exporters.
  927. *
  928. * @return array $exporters An array of personal data exporters.
  929. */
  930. public function register_personal_data_exporter( $exporters ) {
  931. $exporters['jetpack-feedback'] = array(
  932. 'exporter_friendly_name' => __( 'Feedback', 'jetpack' ),
  933. 'callback' => array( $this, 'personal_data_exporter' ),
  934. );
  935. return $exporters;
  936. }
  937. /**
  938. * Registers the personal data eraser.
  939. *
  940. * @since 6.1.1
  941. *
  942. * @param array $erasers An array of personal data erasers.
  943. *
  944. * @return array $erasers An array of personal data erasers.
  945. */
  946. public function register_personal_data_eraser( $erasers ) {
  947. $erasers['jetpack-feedback'] = array(
  948. 'eraser_friendly_name' => __( 'Feedback', 'jetpack' ),
  949. 'callback' => array( $this, 'personal_data_eraser' ),
  950. );
  951. return $erasers;
  952. }
  953. /**
  954. * Exports personal data.
  955. *
  956. * @since 6.1.1
  957. *
  958. * @param string $email Email address.
  959. * @param int $page Page to export.
  960. *
  961. * @return array $return Associative array with keys expected by core.
  962. */
  963. public function personal_data_exporter( $email, $page = 1 ) {
  964. return $this->_internal_personal_data_exporter( $email, $page );
  965. }
  966. /**
  967. * Internal method for exporting personal data.
  968. *
  969. * Allows us to have a different signature than core expects
  970. * while protecting against future core API changes.
  971. *
  972. * @internal
  973. * @since 6.5
  974. *
  975. * @param string $email Email address.
  976. * @param int $page Page to export.
  977. * @param int $per_page Number of feedbacks to process per page. Internal use only (testing)
  978. *
  979. * @return array Associative array with keys expected by core.
  980. */
  981. public function _internal_personal_data_exporter( $email, $page = 1, $per_page = 250 ) {
  982. $export_data = array();
  983. $post_ids = $this->personal_data_post_ids_by_email( $email, $per_page, $page );
  984. foreach ( $post_ids as $post_id ) {
  985. $post_fields = $this->get_parsed_field_contents_of_post( $post_id );
  986. if ( ! is_array( $post_fields ) || empty( $post_fields['_feedback_subject'] ) ) {
  987. continue; // Corrupt data.
  988. }
  989. $post_fields['_feedback_main_comment'] = $this->get_post_content_for_csv_export( $post_id );
  990. $post_fields = $this->map_parsed_field_contents_of_post_to_field_names( $post_fields );
  991. if ( ! is_array( $post_fields ) || empty( $post_fields ) ) {
  992. continue; // No fields to export.
  993. }
  994. $post_meta = $this->get_post_meta_for_csv_export( $post_id );
  995. $post_meta = is_array( $post_meta ) ? $post_meta : array();
  996. $post_export_data = array();
  997. $post_data = array_merge( $post_fields, $post_meta );
  998. ksort( $post_data );
  999. foreach ( $post_data as $post_data_key => $post_data_value ) {
  1000. $post_export_data[] = array(
  1001. 'name' => preg_replace( '/^[0-9]+_/', '', $post_data_key ),
  1002. 'value' => $post_data_value,
  1003. );
  1004. }
  1005. $export_data[] = array(
  1006. 'group_id' => 'feedback',
  1007. 'group_label' => __( 'Feedback', 'jetpack' ),
  1008. 'item_id' => 'feedback-' . $post_id,
  1009. 'data' => $post_export_data,
  1010. );
  1011. }
  1012. return array(
  1013. 'data' => $export_data,
  1014. 'done' => count( $post_ids ) < $per_page,
  1015. );
  1016. }
  1017. /**
  1018. * Erases personal data.
  1019. *
  1020. * @since 6.1.1
  1021. *
  1022. * @param string $email Email address.
  1023. * @param int $page Page to erase.
  1024. *
  1025. * @return array Associative array with keys expected by core.
  1026. */
  1027. public function personal_data_eraser( $email, $page = 1 ) {
  1028. return $this->_internal_personal_data_eraser( $email, $page );
  1029. }
  1030. /**
  1031. * Internal method for erasing personal data.
  1032. *
  1033. * Allows us to have a different signature than core expects
  1034. * while protecting against future core API changes.
  1035. *
  1036. * @internal
  1037. * @since 6.5
  1038. *
  1039. * @param string $email Email address.
  1040. * @param int $page Page to erase.
  1041. * @param int $per_page Number of feedbacks to process per page. Internal use only (testing)
  1042. *
  1043. * @return array Associative array with keys expected by core.
  1044. */
  1045. public function _internal_personal_data_eraser( $email, $page = 1, $per_page = 250 ) {
  1046. $removed = false;
  1047. $retained = false;
  1048. $messages = array();
  1049. $option_name = sprintf( '_jetpack_pde_feedback_%s', md5( $email ) );
  1050. $last_post_id = 1 === $page ? 0 : get_option( $option_name, 0 );
  1051. $post_ids = $this->personal_data_post_ids_by_email( $email, $per_page, $page, $last_post_id );
  1052. foreach ( $post_ids as $post_id ) {
  1053. /**
  1054. * Filters whether to erase a particular Feedback post.
  1055. *
  1056. * @since 6.3.0
  1057. *
  1058. * @param bool|string $prevention_message Whether to apply erase the Feedback post (bool).
  1059. * Custom prevention message (string). Default true.
  1060. * @param int $post_id Feedback post ID.
  1061. */
  1062. $prevention_message = apply_filters( 'grunion_contact_form_delete_feedback_post', true, $post_id );
  1063. if ( true !== $prevention_message ) {
  1064. if ( $prevention_message && is_string( $prevention_message ) ) {
  1065. $messages[] = esc_html( $prevention_message );
  1066. } else {
  1067. $messages[] = sprintf(
  1068. // translators: %d: Post ID.
  1069. __( 'Feedback ID %d could not be removed at this time.', 'jetpack' ),
  1070. $post_id
  1071. );
  1072. }
  1073. $retained = true;
  1074. continue;
  1075. }
  1076. if ( wp_delete_post( $post_id, true ) ) {
  1077. $removed = true;
  1078. } else {
  1079. $retained = true;
  1080. $messages[] = sprintf(
  1081. // translators: %d: Post ID.
  1082. __( 'Feedback ID %d could not be removed at this time.', 'jetpack' ),
  1083. $post_id
  1084. );
  1085. }
  1086. }
  1087. $done = count( $post_ids ) < $per_page;
  1088. if ( $done ) {
  1089. delete_option( $option_name );
  1090. } else {
  1091. update_option( $option_name, (int) $post_id );
  1092. }
  1093. return array(
  1094. 'items_removed' => $removed,
  1095. 'items_retained' => $retained,
  1096. 'messages' => $messages,
  1097. 'done' => $done,
  1098. );
  1099. }
  1100. /**
  1101. * Queries personal data by email address.
  1102. *
  1103. * @since 6.1.1
  1104. *
  1105. * @param string $email Email address.
  1106. * @param int $per_page Post IDs per page. Default is `250`.
  1107. * @param int $page Page to query. Default is `1`.
  1108. * @param int $last_post_id Page to query. Default is `0`. If non-zero, used instead of $page.
  1109. *
  1110. * @return array An array of post IDs.
  1111. */
  1112. public function personal_data_post_ids_by_email( $email, $per_page = 250, $page = 1, $last_post_id = 0 ) {
  1113. add_filter( 'posts_search', array( $this, 'personal_data_search_filter' ) );
  1114. $this->pde_last_post_id_erased = $last_post_id;
  1115. $this->pde_email_address = $email;
  1116. $post_ids = get_posts(
  1117. array(
  1118. 'post_type' => 'feedback',
  1119. 'post_status' => 'publish',
  1120. // This search parameter gets overwritten in ->personal_data_search_filter()
  1121. 's' => '..PDE..AUTHOR EMAIL:..PDE..',
  1122. 'sentence' => true,
  1123. 'order' => 'ASC',
  1124. 'orderby' => 'ID',
  1125. 'fields' => 'ids',
  1126. 'posts_per_page' => $per_page,
  1127. 'paged' => $last_post_id ? 1 : $page,
  1128. 'suppress_filters' => false,
  1129. )
  1130. );
  1131. $this->pde_last_post_id_erased = 0;
  1132. $this->pde_email_address = '';
  1133. remove_filter( 'posts_search', array( $this, 'personal_data_search_filter' ) );
  1134. return $post_ids;
  1135. }
  1136. /**
  1137. * Filters searches by email address.
  1138. *
  1139. * @since 6.1.1
  1140. *
  1141. * @param string $search SQL where clause.
  1142. *
  1143. * @return array Filtered SQL where clause.
  1144. */
  1145. public function personal_data_search_filter( $search ) {
  1146. global $wpdb;
  1147. /*
  1148. * Limits search to `post_content` only, and we only match the
  1149. * author's email address whenever it's on a line by itself.
  1150. */
  1151. if ( $this->pde_email_address && false !== strpos( $search, '..PDE..AUTHOR EMAIL:..PDE..' ) ) {
  1152. $search = $wpdb->prepare(
  1153. " AND (
  1154. {$wpdb->posts}.post_content LIKE %s
  1155. OR {$wpdb->posts}.post_content LIKE %s
  1156. )",
  1157. // `chr( 10 )` = `\n`, `chr( 13 )` = `\r`
  1158. '%' . $wpdb->esc_like( chr( 10 ) . 'AUTHOR EMAIL: ' . $this->pde_email_address . chr( 10 ) ) . '%',
  1159. '%' . $wpdb->esc_like( chr( 13 ) . 'AUTHOR EMAIL: ' . $this->pde_email_address . chr( 13 ) ) . '%'
  1160. );
  1161. if ( $this->pde_last_post_id_erased ) {
  1162. $search .= $wpdb->prepare( " AND {$wpdb->posts}.ID > %d", $this->pde_last_post_id_erased );
  1163. }
  1164. }
  1165. return $search;
  1166. }
  1167. /**
  1168. * Prepares feedback post data for CSV export.
  1169. *
  1170. * @param array $post_ids Post IDs to fetch the data for. These need to be Feedback posts.
  1171. *
  1172. * @return array
  1173. */
  1174. public function get_export_data_for_posts( $post_ids ) {
  1175. $posts_data = array();
  1176. $field_names = array();
  1177. $result = array();
  1178. /**
  1179. * Fetch posts and get the possible field names for later use
  1180. */
  1181. foreach ( $post_ids as $post_id ) {
  1182. /**
  1183. * Fetch post main data, because we need the subject and author data for the feedback form.
  1184. */
  1185. $post_real_data = $this->get_parsed_field_contents_of_post( $post_id );
  1186. /**
  1187. * If `$post_real_data` is not an array or there is no `_feedback_subject` set,
  1188. * then something must be wrong with the feedback post. Skip it.
  1189. */
  1190. if ( ! is_array( $post_real_data ) || ! isset( $post_real_data['_feedback_subject'] ) ) {
  1191. continue;
  1192. }
  1193. /**
  1194. * Fetch main post comment. This is from the default textarea fields.
  1195. * If it is non-empty, then we add it to data, otherwise skip it.
  1196. */
  1197. $post_comment_content = $this->get_post_content_for_csv_export( $post_id );
  1198. if ( ! empty( $post_comment_content ) ) {
  1199. $post_real_data['_feedback_main_comment'] = $post_comment_content;
  1200. }
  1201. /**
  1202. * Map parsed fields to proper field names
  1203. */
  1204. $mapped_fields = $this->map_parsed_field_contents_of_post_to_field_names( $post_real_data );
  1205. /**
  1206. * Fetch post meta data.
  1207. */
  1208. $post_meta_data = $this->get_post_meta_for_csv_export( $post_id );
  1209. /**
  1210. * If `$post_meta_data` is not an array or if it is empty, then there is no
  1211. * extra feedback to work with. Create an empty array.
  1212. */
  1213. if ( ! is_array( $post_meta_data ) || empty( $post_meta_data ) ) {
  1214. $post_meta_data = array();
  1215. }
  1216. /**
  1217. * Prepend the feedback subject to the list of fields.
  1218. */
  1219. $post_meta_data = array_merge(
  1220. $mapped_fields,
  1221. $post_meta_data
  1222. );
  1223. /**
  1224. * Save post metadata for later usage.
  1225. */
  1226. $posts_data[ $post_id ] = $post_meta_data;
  1227. /**
  1228. * Save field names, so we can use them as header fields later in the CSV.
  1229. */
  1230. $field_names = array_merge( $field_names, array_keys( $post_meta_data ) );
  1231. }
  1232. /**
  1233. * Make sure the field names are unique, because we don't want duplicate data.
  1234. */
  1235. $field_names = array_unique( $field_names );
  1236. /**
  1237. * Sort the field names by the field id number
  1238. */
  1239. sort( $field_names, SORT_NUMERIC );
  1240. /**
  1241. * Loop through every post, which is essentially CSV row.
  1242. */
  1243. foreach ( $posts_data as $post_id => $single_post_data ) {
  1244. /**
  1245. * Go through all the possible fields and check if the field is available
  1246. * in the current post.
  1247. *
  1248. * If it is - add the data as a value.
  1249. * If it is not - add an empty string, which is just a placeholder in the CSV.
  1250. */
  1251. foreach ( $field_names as $single_field_name ) {
  1252. if (
  1253. isset( $single_post_data[ $single_field_name ] )
  1254. && ! empty( $single_post_data[ $single_field_name ] )
  1255. ) {
  1256. $result[ $single_field_name ][] = trim( $single_post_data[ $single_field_name ] );
  1257. } else {
  1258. $result[ $single_field_name ][] = '';
  1259. }
  1260. }
  1261. }
  1262. return $result;
  1263. }
  1264. /**
  1265. * download as a csv a contact form or all of them in a csv file
  1266. */
  1267. function download_feedback_as_csv() {
  1268. if ( empty( $_POST['feedback_export_nonce'] ) ) {
  1269. return;
  1270. }
  1271. check_admin_referer( 'feedback_export', 'feedback_export_nonce' );
  1272. if ( ! current_user_can( 'export' ) ) {
  1273. return;
  1274. }
  1275. $args = array(
  1276. 'posts_per_page' => -1,
  1277. 'post_type' => 'feedback',
  1278. 'post_status' => 'publish',
  1279. 'order' => 'ASC',
  1280. 'fields' => 'ids',
  1281. 'suppress_filters' => false,
  1282. );
  1283. $filename = date( 'Y-m-d' ) . '-feedback-export.csv';
  1284. // Check if we want to download all the feedbacks or just a certain contact form
  1285. if ( ! empty( $_POST['post'] ) && $_POST['post'] !== 'all' ) {
  1286. $args['post_parent'] = (int) $_POST['post'];
  1287. $filename = date( 'Y-m-d' ) . '-' . str_replace( '&nbsp;', '-', get_the_title( (int) $_POST['post'] ) ) . '.csv';
  1288. }
  1289. $feedbacks = get_posts( $args );
  1290. if ( empty( $feedbacks ) ) {
  1291. return;
  1292. }
  1293. $filename = sanitize_file_name( $filename );
  1294. /**
  1295. * Prepare data for export.
  1296. */
  1297. $data = $this->get_export_data_for_posts( $feedbacks );
  1298. /**
  1299. * If `$data` is empty, there's nothing we can do below.
  1300. */
  1301. if ( ! is_array( $data ) || empty( $data ) ) {
  1302. return;
  1303. }
  1304. /**
  1305. * Extract field names from `$data` for later use.
  1306. */
  1307. $fields = array_keys( $data );
  1308. /**
  1309. * Count how many rows will be exported.
  1310. */
  1311. $row_count = count( reset( $data ) );
  1312. // Forces the download of the CSV instead of echoing
  1313. header( 'Content-Disposition: attachment; filename=' . $filename );
  1314. header( 'Pragma: no-cache' );
  1315. header( 'Expires: 0' );
  1316. header( 'Content-Type: text/csv; charset=utf-8' );
  1317. $output = fopen( 'php://output', 'w' );
  1318. /**
  1319. * Print CSV headers
  1320. */
  1321. fputcsv( $output, $fields );
  1322. /**
  1323. * Print rows to the output.
  1324. */
  1325. for ( $i = 0; $i < $row_count; $i ++ ) {
  1326. $current_row = array();
  1327. /**
  1328. * Put all the fields in `$current_row` array.
  1329. */
  1330. foreach ( $fields as $single_field_name ) {
  1331. $current_row[] = $this->esc_csv( $data[ $single_field_name ][ $i ] );
  1332. }
  1333. /**
  1334. * Output the complete CSV row
  1335. */
  1336. fputcsv( $output, $current_row );
  1337. }
  1338. fclose( $output );
  1339. }
  1340. /**
  1341. * Escape a string to be used in a CSV context
  1342. *
  1343. * Malicious input can inject formulas into CSV files, opening up the possibility for phishing attacks and
  1344. * disclosure of sensitive information.
  1345. *
  1346. * Additionally, Excel exposes the ability to launch arbitrary commands through the DDE protocol.
  1347. *
  1348. * @see https://www.contextis.com/en/blog/comma-separated-vulnerabilities
  1349. *
  1350. * @param string $field
  1351. *
  1352. * @return string
  1353. */
  1354. public function esc_csv( $field ) {
  1355. $active_content_triggers = array( '=', '+', '-', '@' );
  1356. if ( in_array( mb_substr( $field, 0, 1 ), $active_content_triggers, true ) ) {
  1357. $field = "'" . $field;
  1358. }
  1359. return $field;
  1360. }
  1361. /**
  1362. * Returns a string of HTML <option> items from an array of posts
  1363. *
  1364. * @return string a string of HTML <option> items
  1365. */
  1366. protected function get_feedbacks_as_options() {
  1367. $options = '';
  1368. // Get the feedbacks' parents' post IDs
  1369. $feedbacks = get_posts(
  1370. array(
  1371. 'fields' => 'id=>parent',
  1372. 'posts_per_page' => 100000,
  1373. 'post_type' => 'feedback',
  1374. 'post_status' => 'publish',
  1375. 'suppress_filters' => false,
  1376. )
  1377. );
  1378. $parents = array_unique( array_values( $feedbacks ) );
  1379. $posts = get_posts(
  1380. array(
  1381. 'orderby' => 'ID',
  1382. 'posts_per_page' => 1000,
  1383. 'post_type' => 'any',
  1384. 'post__in' => array_values( $parents ),
  1385. 'suppress_filters' => false,
  1386. )
  1387. );
  1388. // creates the string of <option> elements
  1389. foreach ( $posts as $post ) {
  1390. $options .= sprintf( '<option value="%s">%s</option>', esc_attr( $post->ID ), esc_html( $post->post_title ) );
  1391. }
  1392. return $options;
  1393. }
  1394. /**
  1395. * Get the names of all the form's fields
  1396. *
  1397. * @param array|int $posts the post we want the fields of
  1398. *
  1399. * @return array the array of fields
  1400. *
  1401. * @deprecated As this is no longer necessary as of the CSV export rewrite. - 2015-12-29
  1402. */
  1403. protected function get_field_names( $posts ) {
  1404. $posts = (array) $posts;
  1405. $all_fields = array();
  1406. foreach ( $posts as $post ) {
  1407. $fields = self::parse_fields_from_content( $post );
  1408. if ( isset( $fields['_feedback_all_fields'] ) ) {
  1409. $extra_fields = array_keys( $fields['_feedback_all_fields'] );
  1410. $all_fields = array_merge( $all_fields, $extra_fields );
  1411. }
  1412. }
  1413. $all_fields = array_unique( $all_fields );
  1414. return $all_fields;
  1415. }
  1416. public static function parse_fields_from_content( $post_id ) {
  1417. static $post_fields;
  1418. if ( ! is_array( $post_fields ) ) {
  1419. $post_fields = array();
  1420. }
  1421. if ( isset( $post_fields[ $post_id ] ) ) {
  1422. return $post_fields[ $post_id ];
  1423. }
  1424. $all_values = array();
  1425. $post_content = get_post_field( 'post_content', $post_id );
  1426. $content = explode( '<!--more-->', $post_content );
  1427. $lines = array();
  1428. if ( count( $content ) > 1 ) {
  1429. $content = str_ireplace( array( '<br />', ')</p>' ), '', $content[1] );
  1430. $one_line = preg_replace( '/\s+/', ' ', $content );
  1431. $one_line = preg_replace( '/.*Array \( (.*)\)/', '$1', $one_line );
  1432. preg_match_all( '/\[([^\]]+)\] =\&gt\; ([^\[]+)/', $one_line, $matches );
  1433. if ( count( $matches ) > 1 ) {
  1434. $all_values = array_combine( array_map( 'trim', $matches[1] ), array_map( 'trim', $matches[2] ) );
  1435. }
  1436. $lines = array_filter( explode( "\n", $content ) );
  1437. }
  1438. $var_map = array(
  1439. 'AUTHOR' => '_feedback_author',
  1440. 'AUTHOR EMAIL' => '_feedback_author_email',
  1441. 'AUTHOR URL' => '_feedback_author_url',
  1442. 'SUBJECT' => '_feedback_subject',
  1443. 'IP' => '_feedback_ip',
  1444. );
  1445. $fields = array();
  1446. foreach ( $lines as $line ) {
  1447. $vars = explode( ': ', $line, 2 );
  1448. if ( ! empty( $vars ) ) {
  1449. if ( isset( $var_map[ $vars[0] ] ) ) {
  1450. $fields[ $var_map[ $vars[0] ] ] = self::strip_tags( trim( $vars[1] ) );
  1451. }
  1452. }
  1453. }
  1454. $fields['_feedback_all_fields'] = $all_values;
  1455. $post_fields[ $post_id ] = $fields;
  1456. return $fields;
  1457. }
  1458. /**
  1459. * Creates a valid csv row from a post id
  1460. *
  1461. * @param int $post_id The id of the post
  1462. * @param array $fields An array containing the names of all the fields of the csv
  1463. * @return String The csv row
  1464. *
  1465. * @deprecated This is no longer needed, as of the CSV export rewrite.
  1466. */
  1467. protected static function make_csv_row_from_feedback( $post_id, $fields ) {
  1468. $content_fields = self::parse_fields_from_content( $post_id );
  1469. $all_fields = array();
  1470. if ( isset( $content_fields['_feedback_all_fields'] ) ) {
  1471. $all_fields = $content_fields['_feedback_all_fields'];
  1472. }
  1473. // Overwrite the parsed content with the content we stored in post_meta in a better format.
  1474. $extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', true );
  1475. foreach ( $extra_fields as $extra_field => $extra_value ) {
  1476. $all_fields[ $extra_field ] = $extra_value;
  1477. }
  1478. // The first element in all of the exports will be the subject
  1479. $row_items[] = $content_fields['_feedback_subject'];
  1480. // Loop the fields array in order to fill the $row_items array correctly
  1481. foreach ( $fields as $field ) {
  1482. if ( $field === __( 'Contact Form', 'jetpack' ) ) { // the first field will ever be the contact form, so we can continue
  1483. continue;
  1484. } elseif ( array_key_exists( $field, $all_fields ) ) {
  1485. $row_items[] = $all_fields[ $field ];
  1486. } else {
  1487. $row_items[] = '';
  1488. }
  1489. }
  1490. return $row_items;
  1491. }
  1492. public static function get_ip_address() {
  1493. return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
  1494. }
  1495. /**
  1496. * Disable Block Editor for feedbacks.
  1497. *
  1498. * @param bool $can_edit Whether the post type can be edited or not.
  1499. * @param string $post_type The post type being checked.
  1500. * @return bool
  1501. */
  1502. public function use_block_editor_for_post_type( $can_edit, $post_type ) {
  1503. return 'feedback' === $post_type ? false : $can_edit;
  1504. }
  1505. }
  1506. /**
  1507. * Generic shortcode class.
  1508. * Does nothing other than store structured data and output the shortcode as a string
  1509. *
  1510. * Not very general - specific to Grunion.
  1511. */
  1512. class Crunion_Contact_Form_Shortcode {
  1513. /**
  1514. * @var string the name of the shortcode: [$shortcode_name /]
  1515. */
  1516. public $shortcode_name;
  1517. /**
  1518. * @var array key => value pairs for the shortcode's attributes: [$shortcode_name key="value" ... /]
  1519. */
  1520. public $attributes;
  1521. /**
  1522. * @var array key => value pair for attribute defaults
  1523. */
  1524. public $defaults = array();
  1525. /**
  1526. * @var null|string Null for selfclosing shortcodes. Hhe inner content of otherwise: [$shortcode_name]$content[/$shortcode_name]
  1527. */
  1528. public $content;
  1529. /**
  1530. * @var array Associative array of inner "child" shortcodes equivalent to the $content: [$shortcode_name][child 1/][child 2/][/$shortcode_name]
  1531. */
  1532. public $fields;
  1533. /**
  1534. * @var null|string The HTML of the parsed inner "child" shortcodes". Null for selfclosing shortcodes.
  1535. */
  1536. public $body;
  1537. /**
  1538. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
  1539. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
  1540. */
  1541. function __construct( $attributes, $content = null ) {
  1542. $this->attributes = $this->unesc_attr( $attributes );
  1543. if ( is_array( $content ) ) {
  1544. $string_content = '';
  1545. foreach ( $content as $field ) {
  1546. $string_content .= (string) $field;
  1547. }
  1548. $this->content = $string_content;
  1549. } else {
  1550. $this->content = $content;
  1551. }
  1552. $this->parse_content( $this->content );
  1553. }
  1554. /**
  1555. * Processes the shortcode's inner content for "child" shortcodes
  1556. *
  1557. * @param string $content The shortcode's inner content: [shortcode]$content[/shortcode]
  1558. */
  1559. function parse_content( $content ) {
  1560. if ( is_null( $content ) ) {
  1561. $this->body = null;
  1562. }
  1563. $this->body = do_shortcode( $content );
  1564. }
  1565. /**
  1566. * Returns the value of the requested attribute.
  1567. *
  1568. * @param string $key The attribute to retrieve
  1569. * @return mixed
  1570. */
  1571. function get_attribute( $key ) {
  1572. return isset( $this->attributes[ $key ] ) ? $this->attributes[ $key ] : null;
  1573. }
  1574. function esc_attr( $value ) {
  1575. if ( is_array( $value ) ) {
  1576. return array_map( array( $this, 'esc_attr' ), $value );
  1577. }
  1578. $value = Grunion_Contact_Form_Plugin::strip_tags( $value );
  1579. $value = _wp_specialchars( $value, ENT_QUOTES, false, true );
  1580. // Shortcode attributes can't contain "]"
  1581. $value = str_replace( ']', '', $value );
  1582. $value = str_replace( ',', '&#x002c;', $value ); // store commas encoded
  1583. $value = strtr(
  1584. $value, array(
  1585. '%' => '%25',
  1586. '&' => '%26',
  1587. )
  1588. );
  1589. // shortcode_parse_atts() does stripcslashes()
  1590. $value = addslashes( $value );
  1591. return $value;
  1592. }
  1593. function unesc_attr( $value ) {
  1594. if ( is_array( $value ) ) {
  1595. return array_map( array( $this, 'unesc_attr' ), $value );
  1596. }
  1597. // For back-compat with old Grunion encoding
  1598. // Also, unencode commas
  1599. $value = strtr(
  1600. $value, array(
  1601. '%26' => '&',
  1602. '%25' => '%',
  1603. )
  1604. );
  1605. $value = preg_replace( array( '/&#x0*22;/i', '/&#x0*27;/i', '/&#x0*26;/i', '/&#x0*2c;/i' ), array( '"', "'", '&', ',' ), $value );
  1606. $value = htmlspecialchars_decode( $value, ENT_QUOTES );
  1607. $value = Grunion_Contact_Form_Plugin::strip_tags( $value );
  1608. return $value;
  1609. }
  1610. /**
  1611. * Generates the shortcode
  1612. */
  1613. function __toString() {
  1614. $r = "[{$this->shortcode_name} ";
  1615. foreach ( $this->attributes as $key => $value ) {
  1616. if ( ! $value ) {
  1617. continue;
  1618. }
  1619. if ( isset( $this->defaults[ $key ] ) && $this->defaults[ $key ] == $value ) {
  1620. continue;
  1621. }
  1622. if ( 'id' == $key ) {
  1623. continue;
  1624. }
  1625. $value = $this->esc_attr( $value );
  1626. if ( is_array( $value ) ) {
  1627. $value = join( ',', $value );
  1628. }
  1629. if ( false === strpos( $value, "'" ) ) {
  1630. $value = "'$value'";
  1631. } elseif ( false === strpos( $value, '"' ) ) {
  1632. $value = '"' . $value . '"';
  1633. } else {
  1634. // Shortcodes can't contain both '"' and "'". Strip one.
  1635. $value = str_replace( "'", '', $value );
  1636. $value = "'$value'";
  1637. }
  1638. $r .= "{$key}={$value} ";
  1639. }
  1640. $r = rtrim( $r );
  1641. if ( $this->fields ) {
  1642. $r .= ']';
  1643. foreach ( $this->fields as $field ) {
  1644. $r .= (string) $field;
  1645. }
  1646. $r .= "[/{$this->shortcode_name}]";
  1647. } else {
  1648. $r .= '/]';
  1649. }
  1650. return $r;
  1651. }
  1652. }
  1653. /**
  1654. * Class for the contact-form shortcode.
  1655. * Parses shortcode to output the contact form as HTML
  1656. * Sends email and stores the contact form response (a.k.a. "feedback")
  1657. */
  1658. class Grunion_Contact_Form extends Crunion_Contact_Form_Shortcode {
  1659. public $shortcode_name = 'contact-form';
  1660. /**
  1661. * @var WP_Error stores form submission errors
  1662. */
  1663. public $errors;
  1664. /**
  1665. * @var string The SHA1 hash of the attributes that comprise the form.
  1666. */
  1667. public $hash;
  1668. /**
  1669. * @var Grunion_Contact_Form The most recent (inclusive) contact-form shortcode processed
  1670. */
  1671. static $last;
  1672. /**
  1673. * @var Whatever form we are currently looking at. If processed, will become $last
  1674. */
  1675. static $current_form;
  1676. /**
  1677. * @var array All found forms, indexed by hash.
  1678. */
  1679. static $forms = array();
  1680. /**
  1681. * @var bool Whether to print the grunion.css style when processing the contact-form shortcode
  1682. */
  1683. static $style = false;
  1684. /**
  1685. * @var array When printing the submit button, what tags are allowed
  1686. */
  1687. static $allowed_html_tags_for_submit_button = array( 'br' => array() );
  1688. function __construct( $attributes, $content = null ) {
  1689. global $post;
  1690. $this->hash = sha1( json_encode( $attributes ) . $content );
  1691. self::$forms[ $this->hash ] = $this;
  1692. // Set up the default subject and recipient for this form.
  1693. $default_to = '';
  1694. $default_subject = '[' . get_option( 'blogname' ) . ']';
  1695. if ( ! isset( $attributes ) || ! is_array( $attributes ) ) {
  1696. $attributes = array();
  1697. }
  1698. if ( ! empty( $attributes['widget'] ) && $attributes['widget'] ) {
  1699. $default_to .= get_option( 'admin_email' );
  1700. $attributes['id'] = 'widget-' . $attributes['widget'];
  1701. $default_subject = sprintf( _x( '%1$s Sidebar', '%1$s = blog name', 'jetpack' ), $default_subject );
  1702. } elseif ( $post ) {
  1703. $attributes['id'] = $post->ID;
  1704. $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 ) );
  1705. $post_author = get_userdata( $post->post_author );
  1706. $default_to .= $post_author->user_email;
  1707. }
  1708. // Keep reference to $this for parsing form fields.
  1709. self::$current_form = $this;
  1710. $this->defaults = array(
  1711. 'to' => $default_to,
  1712. 'subject' => $default_subject,
  1713. 'show_subject' => 'no', // only used in back-compat mode
  1714. 'widget' => 0, // Not exposed to the user. Works with Grunion_Contact_Form_Plugin::widget_atts()
  1715. 'id' => null, // Not exposed to the user. Set above.
  1716. 'submit_button_text' => __( 'Submit', 'jetpack' ),
  1717. // These attributes come from the block editor, so use camel case instead of snake case.
  1718. 'customThankyou' => '', // Whether to show a custom thankyou response after submitting a form. '' for no, 'message' for a custom message, 'redirect' to redirect to a new URL.
  1719. 'customThankyouHeading' => __( 'Message Sent', 'jetpack' ), // The text to show above customThankyouMessage.
  1720. 'customThankyouMessage' => __( 'Thank you for your submission!', 'jetpack' ), // The message to show when customThankyou is set to 'message'.
  1721. 'customThankyouRedirect' => '', // The URL to redirect to when customThankyou is set to 'redirect'.
  1722. 'jetpackCRM' => true, // Whether Jetpack CRM should store the form submission.
  1723. );
  1724. $attributes = shortcode_atts( $this->defaults, $attributes, 'contact-form' );
  1725. // We only enable the contact-field shortcode temporarily while processing the contact-form shortcode.
  1726. Grunion_Contact_Form_Plugin::$using_contact_form_field = true;
  1727. parent::__construct( $attributes, $content );
  1728. // There were no fields in the contact form. The form was probably just [contact-form /]. Build a default form.
  1729. if ( empty( $this->fields ) ) {
  1730. // same as the original Grunion v1 form.
  1731. $default_form = '
  1732. [contact-field label="' . __( 'Name', 'jetpack' ) . '" type="name" required="true" /]
  1733. [contact-field label="' . __( 'Email', 'jetpack' ) . '" type="email" required="true" /]
  1734. [contact-field label="' . __( 'Website', 'jetpack' ) . '" type="url" /]';
  1735. if ( 'yes' == strtolower( $this->get_attribute( 'show_subject' ) ) ) {
  1736. $default_form .= '
  1737. [contact-field label="' . __( 'Subject', 'jetpack' ) . '" type="subject" /]';
  1738. }
  1739. $default_form .= '
  1740. [contact-field label="' . __( 'Message', 'jetpack' ) . '" type="textarea" /]';
  1741. $this->parse_content( $default_form );
  1742. // Store the shortcode.
  1743. $this->store_shortcode( $default_form, $attributes, $this->hash );
  1744. } else {
  1745. // Store the shortcode.
  1746. $this->store_shortcode( $content, $attributes, $this->hash );
  1747. }
  1748. // $this->body and $this->fields have been setup. We no longer need the contact-field shortcode.
  1749. Grunion_Contact_Form_Plugin::$using_contact_form_field = false;
  1750. }
  1751. /**
  1752. * Store shortcode content for recall later
  1753. * - used to receate shortcode when user uses do_shortcode
  1754. *
  1755. * @param string $content
  1756. * @param array $attributes
  1757. * @param string $hash
  1758. */
  1759. static function store_shortcode( $content = null, $attributes = null, $hash = null ) {
  1760. if ( $content != null and isset( $attributes['id'] ) ) {
  1761. if ( empty( $hash ) ) {
  1762. $hash = sha1( json_encode( $attributes ) . $content );
  1763. }
  1764. $shortcode_meta = get_post_meta( $attributes['id'], "_g_feedback_shortcode_{$hash}", true );
  1765. if ( $shortcode_meta != '' or $shortcode_meta != $content ) {
  1766. update_post_meta( $attributes['id'], "_g_feedback_shortcode_{$hash}", $content );
  1767. // Save attributes to post_meta for later use. They're not available later in do_shortcode situations.
  1768. update_post_meta( $attributes['id'], "_g_feedback_shortcode_atts_{$hash}", $attributes );
  1769. }
  1770. }
  1771. }
  1772. /**
  1773. * Toggle for printing the grunion.css stylesheet
  1774. *
  1775. * @param bool $style
  1776. */
  1777. static function style( $style ) {
  1778. $previous_style = self::$style;
  1779. self::$style = (bool) $style;
  1780. return $previous_style;
  1781. }
  1782. /**
  1783. * Turn on printing of grunion.css stylesheet
  1784. *
  1785. * @see ::style()
  1786. * @internal
  1787. * @param bool $style
  1788. */
  1789. static function _style_on() {
  1790. return self::style( true );
  1791. }
  1792. /**
  1793. * The contact-form shortcode processor
  1794. *
  1795. * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  1796. * @param string|null $content The shortcode's inner content: [contact-form]$content[/contact-form]
  1797. * @return string HTML for the concat form.
  1798. */
  1799. static function parse( $attributes, $content ) {
  1800. if ( Settings::is_syncing() ) {
  1801. return '';
  1802. }
  1803. // Create a new Grunion_Contact_Form object (this class)
  1804. $form = new Grunion_Contact_Form( $attributes, $content );
  1805. $id = $form->get_attribute( 'id' );
  1806. if ( ! $id ) { // something terrible has happened
  1807. return '[contact-form]';
  1808. }
  1809. if ( is_feed() ) {
  1810. return '[contact-form]';
  1811. }
  1812. self::$last = $form;
  1813. // Enqueue the grunion.css stylesheet if self::$style allows it
  1814. if ( self::$style && ( empty( $_REQUEST['action'] ) || $_REQUEST['action'] != 'grunion_shortcode_to_json' ) ) {
  1815. // Enqueue the style here instead of printing it, because if some other plugin has run the_post()+rewind_posts(),
  1816. // (like VideoPress does), the style tag gets "printed" the first time and discarded, leaving the contact form unstyled.
  1817. // when WordPress does the real loop.
  1818. wp_enqueue_style( 'grunion.css' );
  1819. }
  1820. $r = '';
  1821. $r .= "<div id='contact-form-$id'>\n";
  1822. if ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) {
  1823. // There are errors. Display them
  1824. $r .= "<div class='form-error'>\n<h3>" . __( 'Error!', 'jetpack' ) . "</h3>\n<ul class='form-errors'>\n";
  1825. foreach ( $form->errors->get_error_messages() as $message ) {
  1826. $r .= "\t<li class='form-error-message'>" . esc_html( $message ) . "</li>\n";
  1827. }
  1828. $r .= "</ul>\n</div>\n\n";
  1829. }
  1830. if ( isset( $_GET['contact-form-id'] )
  1831. && (int) $_GET['contact-form-id'] === (int) self::$last->get_attribute( 'id' )
  1832. && isset( $_GET['contact-form-sent'], $_GET['contact-form-hash'] )
  1833. && is_string( $_GET['contact-form-hash'] )
  1834. && hash_equals( $form->hash, $_GET['contact-form-hash'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  1835. // The contact form was submitted. Show the success message/results.
  1836. $feedback_id = (int) $_GET['contact-form-sent'];
  1837. $back_url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', '_wpnonce' ) );
  1838. $r_success_message =
  1839. '<h3>' . esc_html( $form->get_attribute( 'customThankyouHeading' ) ) .
  1840. ' (<a href="' . esc_url( $back_url ) . '">' . esc_html__( 'go back', 'jetpack' ) . '</a>)' .
  1841. "</h3>\n\n";
  1842. // Don't show the feedback details unless the nonce matches
  1843. if ( $feedback_id && wp_verify_nonce( stripslashes( $_GET['_wpnonce'] ), "contact-form-sent-{$feedback_id}" ) ) {
  1844. $r_success_message .= self::success_message( $feedback_id, $form );
  1845. }
  1846. /**
  1847. * Filter the message returned after a successful contact form submission.
  1848. *
  1849. * @module contact-form
  1850. *
  1851. * @since 1.3.1
  1852. *
  1853. * @param string $r_success_message Success message.
  1854. */
  1855. $r .= apply_filters( 'grunion_contact_form_success_message', $r_success_message );
  1856. } else {
  1857. // Nothing special - show the normal contact form
  1858. if ( $form->get_attribute( 'widget' ) ) {
  1859. // Submit form to the current URL
  1860. $url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', 'action', '_wpnonce' ) );
  1861. } else {
  1862. // Submit form to the post permalink
  1863. $url = get_permalink();
  1864. }
  1865. // For SSL/TLS page. See RFC 3986 Section 4.2
  1866. $url = set_url_scheme( $url );
  1867. // May eventually want to send this to admin-post.php...
  1868. /**
  1869. * Filter the contact form action URL.
  1870. *
  1871. * @module contact-form
  1872. *
  1873. * @since 1.3.1
  1874. *
  1875. * @param string $contact_form_id Contact form post URL.
  1876. * @param $post $GLOBALS['post'] Post global variable.
  1877. * @param int $id Contact Form ID.
  1878. */
  1879. $url = apply_filters( 'grunion_contact_form_form_action', "{$url}#contact-form-{$id}", $GLOBALS['post'], $id );
  1880. $has_submit_button_block = ! ( false === strpos( $content, 'wp-block-jetpack-button' ) );
  1881. $form_classes = 'contact-form commentsblock';
  1882. if ( $has_submit_button_block ) {
  1883. $form_classes .= ' wp-block-jetpack-contact-form';
  1884. }
  1885. $r .= "<form action='" . esc_url( $url ) . "' method='post' class='" . esc_attr( $form_classes ) . "'>\n";
  1886. $r .= $form->body;
  1887. // In new versions of the contact form block the button is an inner block
  1888. // so the button does not need to be constructed server-side.
  1889. if ( ! $has_submit_button_block ) {
  1890. $r .= "\t<p class='contact-submit'>\n";
  1891. $gutenberg_submit_button_classes = '';
  1892. if ( ! empty( $attributes['submitButtonClasses'] ) ) {
  1893. $gutenberg_submit_button_classes = ' ' . $attributes['submitButtonClasses'];
  1894. }
  1895. /**
  1896. * Filter the contact form submit button class attribute.
  1897. *
  1898. * @module contact-form
  1899. *
  1900. * @since 6.6.0
  1901. *
  1902. * @param string $class Additional CSS classes for button attribute.
  1903. */
  1904. $submit_button_class = apply_filters( 'jetpack_contact_form_submit_button_class', 'pushbutton-wide' . $gutenberg_submit_button_classes );
  1905. $submit_button_styles = '';
  1906. if ( ! empty( $attributes['customBackgroundButtonColor'] ) ) {
  1907. $submit_button_styles .= 'background-color: ' . $attributes['customBackgroundButtonColor'] . '; ';
  1908. }
  1909. if ( ! empty( $attributes['customTextButtonColor'] ) ) {
  1910. $submit_button_styles .= 'color: ' . $attributes['customTextButtonColor'] . ';';
  1911. }
  1912. if ( ! empty( $attributes['submitButtonText'] ) ) {
  1913. $submit_button_text = $attributes['submitButtonText'];
  1914. } else {
  1915. $submit_button_text = $form->get_attribute( 'submit_button_text' );
  1916. }
  1917. $r .= "\t\t<button type='submit' class='" . esc_attr( $submit_button_class ) . "'";
  1918. if ( ! empty( $submit_button_styles ) ) {
  1919. $r .= " style='" . esc_attr( $submit_button_styles ) . "'";
  1920. }
  1921. $r .= ">";
  1922. $r .= wp_kses(
  1923. $submit_button_text,
  1924. self::$allowed_html_tags_for_submit_button
  1925. ) . "</button>";
  1926. }
  1927. if ( is_user_logged_in() ) {
  1928. $r .= "\t\t" . wp_nonce_field( 'contact-form_' . $id, '_wpnonce', true, false ) . "\n"; // nonce and referer
  1929. }
  1930. if ( isset( $attributes['hasFormSettingsSet'] ) && $attributes['hasFormSettingsSet'] ) {
  1931. $r .= "\t\t<input type='hidden' name='is_block' value='1' />\n";
  1932. }
  1933. $r .= "\t\t<input type='hidden' name='contact-form-id' value='$id' />\n";
  1934. $r .= "\t\t<input type='hidden' name='action' value='grunion-contact-form' />\n";
  1935. $r .= "\t\t<input type='hidden' name='contact-form-hash' value='" . esc_attr( $form->hash ) . "' />\n";
  1936. if ( ! $has_submit_button_block ) {
  1937. $r .= "\t</p>\n";
  1938. }
  1939. $r .= "</form>\n";
  1940. }
  1941. $r .= '</div>';
  1942. /**
  1943. * Filter the contact form, allowing plugins to modify the HTML.
  1944. *
  1945. * @module contact-form
  1946. *
  1947. * @since 10.2.0
  1948. *
  1949. * @param string $r The contact form HTML.
  1950. */
  1951. return apply_filters( 'jetpack_contact_form_html', $r );
  1952. }
  1953. /**
  1954. * Returns a success message to be returned if the form is sent via AJAX.
  1955. *
  1956. * @param int $feedback_id
  1957. * @param object Grunion_Contact_Form $form
  1958. *
  1959. * @return string $message
  1960. */
  1961. static function success_message( $feedback_id, $form ) {
  1962. if ( 'message' === $form->get_attribute( 'customThankyou' ) ) {
  1963. $message = wpautop( $form->get_attribute( 'customThankyouMessage' ) );
  1964. } else {
  1965. $message = '<blockquote class="contact-form-submission">'
  1966. . '<p>' . join( '</p><p>', self::get_compiled_form( $feedback_id, $form ) ) . '</p>'
  1967. . '</blockquote>';
  1968. }
  1969. return wp_kses(
  1970. $message,
  1971. array(
  1972. 'br' => array(),
  1973. 'blockquote' => array( 'class' => array() ),
  1974. 'p' => array(),
  1975. )
  1976. );
  1977. }
  1978. /**
  1979. * Returns a compiled form with labels and values in a form of an array
  1980. * of lines.
  1981. *
  1982. * @param int $feedback_id
  1983. * @param object Grunion_Contact_Form $form
  1984. *
  1985. * @return array $lines
  1986. */
  1987. static function get_compiled_form( $feedback_id, $form ) {
  1988. $feedback = get_post( $feedback_id );
  1989. $field_ids = $form->get_field_ids();
  1990. $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $feedback_id );
  1991. // Maps field_ids to post_meta keys
  1992. $field_value_map = array(
  1993. 'name' => 'author',
  1994. 'email' => 'author_email',
  1995. 'url' => 'author_url',
  1996. 'subject' => 'subject',
  1997. 'textarea' => false, // not a post_meta key. This is stored in post_content
  1998. );
  1999. $compiled_form = array();
  2000. // "Standard" field allowed list.
  2001. foreach ( $field_value_map as $type => $meta_key ) {
  2002. if ( isset( $field_ids[ $type ] ) ) {
  2003. $field = $form->fields[ $field_ids[ $type ] ];
  2004. if ( $meta_key ) {
  2005. if ( isset( $content_fields[ "_feedback_{$meta_key}" ] ) ) {
  2006. $value = $content_fields[ "_feedback_{$meta_key}" ];
  2007. }
  2008. } else {
  2009. // The feedback content is stored as the first "half" of post_content
  2010. $value = $feedback->post_content;
  2011. list( $value ) = explode( '<!--more-->', $value );
  2012. $value = trim( $value );
  2013. }
  2014. $field_index = array_search( $field_ids[ $type ], $field_ids['all'] );
  2015. $compiled_form[ $field_index ] = sprintf(
  2016. '<b>%1$s:</b> %2$s<br /><br />',
  2017. wp_kses( $field->get_attribute( 'label' ), array() ),
  2018. self::escape_and_sanitize_field_value( $value )
  2019. );
  2020. }
  2021. }
  2022. // "Non-standard" fields
  2023. if ( $field_ids['extra'] ) {
  2024. // array indexed by field label (not field id)
  2025. $extra_fields = get_post_meta( $feedback_id, '_feedback_extra_fields', true );
  2026. /**
  2027. * Only get data for the compiled form if `$extra_fields` is a valid and non-empty array.
  2028. */
  2029. if ( is_array( $extra_fields ) && ! empty( $extra_fields ) ) {
  2030. $extra_field_keys = array_keys( $extra_fields );
  2031. $i = 0;
  2032. foreach ( $field_ids['extra'] as $field_id ) {
  2033. $field = $form->fields[ $field_id ];
  2034. $field_index = array_search( $field_id, $field_ids['all'] );
  2035. $label = $field->get_attribute( 'label' );
  2036. $compiled_form[ $field_index ] = sprintf(
  2037. '<b>%1$s:</b> %2$s<br /><br />',
  2038. wp_kses( $label, array() ),
  2039. self::escape_and_sanitize_field_value( $extra_fields[ $extra_field_keys[ $i ] ] )
  2040. );
  2041. $i++;
  2042. }
  2043. }
  2044. }
  2045. // Sorting lines by the field index
  2046. ksort( $compiled_form );
  2047. return $compiled_form;
  2048. }
  2049. static function escape_and_sanitize_field_value( $value ) {
  2050. $value = str_replace( array( '[' , ']' ) , array( '&#91;' , '&#93;' ) , $value );
  2051. return nl2br( wp_kses( $value, array() ) );
  2052. }
  2053. /**
  2054. * Only strip out empty string values and keep all the other values as they are.
  2055. *
  2056. * @param $single_value
  2057. *
  2058. * @return bool
  2059. */
  2060. static function remove_empty( $single_value ) {
  2061. return ( $single_value !== '' );
  2062. }
  2063. /**
  2064. * Escape a shortcode value.
  2065. *
  2066. * Shortcode attribute values have a number of unfortunate restrictions, which fortunately we
  2067. * can get around by adding some extra HTML encoding.
  2068. *
  2069. * The output HTML will have a few extra escapes, but that makes no functional difference.
  2070. *
  2071. * @since 9.1.0
  2072. * @param string $val Value to escape.
  2073. * @return string
  2074. */
  2075. private static function esc_shortcode_val( $val ) {
  2076. return strtr(
  2077. esc_html( $val ),
  2078. array(
  2079. // Brackets in attribute values break the shortcode parser.
  2080. '[' => '&#091;',
  2081. ']' => '&#093;',
  2082. // Shortcode parser screws up backslashes too, thanks to calls to `stripcslashes`.
  2083. '\\' => '&#092;',
  2084. // The existing code here represents arrays as comma-separated strings.
  2085. // Rather than trying to change representations now, just escape the commas in values.
  2086. ',' => '&#044;',
  2087. )
  2088. );
  2089. }
  2090. /**
  2091. * The contact-field shortcode processor
  2092. * 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.
  2093. *
  2094. * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  2095. * @param string|null $content The shortcode's inner content: [contact-field]$content[/contact-field]
  2096. * @return HTML for the contact form field
  2097. */
  2098. static function parse_contact_field( $attributes, $content ) {
  2099. // Don't try to parse contact form fields if not inside a contact form
  2100. if ( ! Grunion_Contact_Form_Plugin::$using_contact_form_field ) {
  2101. $att_strs = array();
  2102. if ( ! isset( $attributes['label'] ) ) {
  2103. $type = isset( $attributes['type'] ) ? $attributes['type'] : null;
  2104. $attributes['label'] = self::get_default_label_from_type( $type );
  2105. }
  2106. foreach ( $attributes as $att => $val ) {
  2107. if ( is_numeric( $att ) ) { // Is a valueless attribute
  2108. $att_strs[] = self::esc_shortcode_val( $val );
  2109. } elseif ( isset( $val ) ) { // A regular attr - value pair
  2110. if ( ( $att === 'options' || $att === 'values' ) && is_string( $val ) ) { // remove any empty strings
  2111. $val = explode( ',', $val );
  2112. }
  2113. if ( is_array( $val ) ) {
  2114. $val = array_filter( $val, array( __CLASS__, 'remove_empty' ) ); // removes any empty strings
  2115. $att_strs[] = esc_html( $att ) . '="' . implode( ',', array_map( array( __CLASS__, 'esc_shortcode_val' ), $val ) ) . '"';
  2116. } elseif ( is_bool( $val ) ) {
  2117. $att_strs[] = esc_html( $att ) . '="' . ( $val ? '1' : '' ) . '"';
  2118. } else {
  2119. $att_strs[] = esc_html( $att ) . '="' . self::esc_shortcode_val( $val ) . '"';
  2120. }
  2121. }
  2122. }
  2123. $html = '[contact-field ' . implode( ' ', $att_strs );
  2124. if ( isset( $content ) && ! empty( $content ) ) { // If there is content, let's add a closing tag
  2125. $html .= ']' . esc_html( $content ) . '[/contact-field]';
  2126. } else { // Otherwise let's add a closing slash in the first tag
  2127. $html .= '/]';
  2128. }
  2129. return $html;
  2130. }
  2131. $form = Grunion_Contact_Form::$current_form;
  2132. $field = new Grunion_Contact_Form_Field( $attributes, $content, $form );
  2133. $field_id = $field->get_attribute( 'id' );
  2134. if ( $field_id ) {
  2135. $form->fields[ $field_id ] = $field;
  2136. } else {
  2137. $form->fields[] = $field;
  2138. }
  2139. if (
  2140. isset( $_POST['action'] ) && 'grunion-contact-form' === $_POST['action']
  2141. &&
  2142. isset( $_POST['contact-form-id'] ) && $form->get_attribute( 'id' ) == $_POST['contact-form-id']
  2143. &&
  2144. isset( $_POST['contact-form-hash'] ) && hash_equals( $form->hash, $_POST['contact-form-hash'] )
  2145. ) {
  2146. // If we're processing a POST submission for this contact form, validate the field value so we can show errors as necessary.
  2147. $field->validate();
  2148. }
  2149. // Output HTML
  2150. return $field->render();
  2151. }
  2152. static function get_default_label_from_type( $type ) {
  2153. $str = null;
  2154. switch ( $type ) {
  2155. case 'text':
  2156. $str = __( 'Text', 'jetpack' );
  2157. break;
  2158. case 'name':
  2159. $str = __( 'Name', 'jetpack' );
  2160. break;
  2161. case 'email':
  2162. $str = __( 'Email', 'jetpack' );
  2163. break;
  2164. case 'url':
  2165. $str = __( 'Website', 'jetpack' );
  2166. break;
  2167. case 'date':
  2168. $str = __( 'Date', 'jetpack' );
  2169. break;
  2170. case 'telephone':
  2171. $str = __( 'Phone', 'jetpack' );
  2172. break;
  2173. case 'textarea':
  2174. $str = __( 'Message', 'jetpack' );
  2175. break;
  2176. case 'checkbox':
  2177. $str = __( 'Checkbox', 'jetpack' );
  2178. break;
  2179. case 'checkbox-multiple':
  2180. $str = __( 'Choose several', 'jetpack' );
  2181. break;
  2182. case 'radio':
  2183. $str = __( 'Choose one', 'jetpack' );
  2184. break;
  2185. case 'select':
  2186. $str = __( 'Select one', 'jetpack' );
  2187. break;
  2188. case 'consent':
  2189. $str = __( 'Consent', 'jetpack' );
  2190. break;
  2191. default:
  2192. $str = null;
  2193. }
  2194. return $str;
  2195. }
  2196. /**
  2197. * Loops through $this->fields to generate a (structured) list of field IDs.
  2198. *
  2199. * Important: Currently the allowed fields are defined as follows:
  2200. * `name`, `email`, `url`, `subject`, `textarea`
  2201. *
  2202. * If you need to add new fields to the Contact Form, please don't add them
  2203. * to the allowed fields and leave them as extra fields.
  2204. *
  2205. * The reasoning behind this is that both the admin Feedback view and the CSV
  2206. * export will not include any fields that are added to the list of
  2207. * allowed fields without taking proper care to add them to all the
  2208. * other places where they accessed/used/saved.
  2209. *
  2210. * The safest way to add new fields is to add them to the dropdown and the
  2211. * HTML list ( @see Grunion_Contact_Form_Field::render ) and don't add them
  2212. * to the list of allowed fields. This way they will become a part of the
  2213. * `extra fields` which are saved in the post meta and will be properly
  2214. * handled by the admin Feedback view and the CSV Export without any extra
  2215. * work.
  2216. *
  2217. * If there is need to add a field to the allowed fields, then please
  2218. * take proper care to add logic to handle the field in the following places:
  2219. *
  2220. * - Below in the switch statement - so the field is recognized as allowed.
  2221. *
  2222. * - Grunion_Contact_Form::process_submission - validation and logic.
  2223. *
  2224. * - Grunion_Contact_Form::process_submission - add the field as an additional
  2225. * field in the `post_content` when saving the feedback content.
  2226. *
  2227. * - Grunion_Contact_Form_Plugin::parse_fields_from_content - add mapping
  2228. * for the field, defined in the above method.
  2229. *
  2230. * - Grunion_Contact_Form_Plugin::map_parsed_field_contents_of_post_to_field_names -
  2231. * add mapping of the field for the CSV Export. Otherwise it will be missing
  2232. * from the exported data.
  2233. *
  2234. * - admin.php / grunion_manage_post_columns - add the field to the render logic.
  2235. * Otherwise it will be missing from the admin Feedback view.
  2236. *
  2237. * @return array
  2238. */
  2239. function get_field_ids() {
  2240. $field_ids = array(
  2241. 'all' => array(), // array of all field_ids.
  2242. 'extra' => array(), // array of all non-allowed field IDs.
  2243. // Allowed "standard" field IDs:
  2244. // 'email' => field_id,
  2245. // 'name' => field_id,
  2246. // 'url' => field_id,
  2247. // 'subject' => field_id,
  2248. // 'textarea' => field_id,
  2249. );
  2250. foreach ( $this->fields as $id => $field ) {
  2251. $field_ids['all'][] = $id;
  2252. $type = $field->get_attribute( 'type' );
  2253. if ( isset( $field_ids[ $type ] ) ) {
  2254. // This type of field is already present in our allowed list of "standard" fields for this form
  2255. // Put it in extra
  2256. $field_ids['extra'][] = $id;
  2257. continue;
  2258. }
  2259. /**
  2260. * See method description before modifying the switch cases.
  2261. */
  2262. switch ( $type ) {
  2263. case 'email':
  2264. case 'name':
  2265. case 'url':
  2266. case 'subject':
  2267. case 'textarea':
  2268. case 'consent':
  2269. $field_ids[ $type ] = $id;
  2270. break;
  2271. default:
  2272. // Put everything else in extra
  2273. $field_ids['extra'][] = $id;
  2274. }
  2275. }
  2276. return $field_ids;
  2277. }
  2278. /**
  2279. * Process the contact form's POST submission
  2280. * Stores feedback. Sends email.
  2281. */
  2282. function process_submission() {
  2283. global $post;
  2284. $plugin = Grunion_Contact_Form_Plugin::init();
  2285. $id = $this->get_attribute( 'id' );
  2286. $to = $this->get_attribute( 'to' );
  2287. $widget = $this->get_attribute( 'widget' );
  2288. $contact_form_subject = $this->get_attribute( 'subject' );
  2289. $email_marketing_consent = false;
  2290. $to = str_replace( ' ', '', $to );
  2291. $emails = explode( ',', $to );
  2292. $valid_emails = array();
  2293. foreach ( (array) $emails as $email ) {
  2294. if ( ! is_email( $email ) ) {
  2295. continue;
  2296. }
  2297. if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $email ) ) {
  2298. continue;
  2299. }
  2300. $valid_emails[] = $email;
  2301. }
  2302. // No one to send it to, which means none of the "to" attributes are valid emails.
  2303. // Use default email instead.
  2304. if ( ! $valid_emails ) {
  2305. $valid_emails = $this->defaults['to'];
  2306. }
  2307. $to = $valid_emails;
  2308. // Last ditch effort to set a recipient if somehow none have been set.
  2309. if ( empty( $to ) ) {
  2310. $to = get_option( 'admin_email' );
  2311. }
  2312. // Make sure we're processing the form we think we're processing... probably a redundant check.
  2313. if ( $widget ) {
  2314. if ( 'widget-' . $widget != $_POST['contact-form-id'] ) {
  2315. return false;
  2316. }
  2317. } else {
  2318. if ( $post->ID != $_POST['contact-form-id'] ) {
  2319. return false;
  2320. }
  2321. }
  2322. $field_ids = $this->get_field_ids();
  2323. // Initialize all these "standard" fields to null
  2324. $comment_author_email = $comment_author_email_label = // v
  2325. $comment_author = $comment_author_label = // v
  2326. $comment_author_url = $comment_author_url_label = // v
  2327. $comment_content = $comment_content_label = null;
  2328. // For each of the "standard" fields, grab their field label and value.
  2329. if ( isset( $field_ids['name'] ) ) {
  2330. $field = $this->fields[ $field_ids['name'] ];
  2331. $comment_author = Grunion_Contact_Form_Plugin::strip_tags(
  2332. stripslashes(
  2333. /** This filter is already documented in core/wp-includes/comment-functions.php */
  2334. apply_filters( 'pre_comment_author_name', addslashes( $field->value ) )
  2335. )
  2336. );
  2337. $comment_author_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  2338. }
  2339. if ( isset( $field_ids['email'] ) ) {
  2340. $field = $this->fields[ $field_ids['email'] ];
  2341. $comment_author_email = Grunion_Contact_Form_Plugin::strip_tags(
  2342. stripslashes(
  2343. /** This filter is already documented in core/wp-includes/comment-functions.php */
  2344. apply_filters( 'pre_comment_author_email', addslashes( $field->value ) )
  2345. )
  2346. );
  2347. $comment_author_email_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  2348. }
  2349. if ( isset( $field_ids['url'] ) ) {
  2350. $field = $this->fields[ $field_ids['url'] ];
  2351. $comment_author_url = Grunion_Contact_Form_Plugin::strip_tags(
  2352. stripslashes(
  2353. /** This filter is already documented in core/wp-includes/comment-functions.php */
  2354. apply_filters( 'pre_comment_author_url', addslashes( $field->value ) )
  2355. )
  2356. );
  2357. if ( 'http://' == $comment_author_url ) {
  2358. $comment_author_url = '';
  2359. }
  2360. $comment_author_url_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  2361. }
  2362. if ( isset( $field_ids['textarea'] ) ) {
  2363. $field = $this->fields[ $field_ids['textarea'] ];
  2364. $comment_content = trim( Grunion_Contact_Form_Plugin::strip_tags( $field->value ) );
  2365. $comment_content_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  2366. }
  2367. if ( isset( $field_ids['subject'] ) ) {
  2368. $field = $this->fields[ $field_ids['subject'] ];
  2369. if ( $field->value ) {
  2370. $contact_form_subject = Grunion_Contact_Form_Plugin::strip_tags( $field->value );
  2371. }
  2372. }
  2373. if ( isset( $field_ids['consent'] ) ) {
  2374. $field = $this->fields[ $field_ids['consent'] ];
  2375. if ( $field->value ) {
  2376. $email_marketing_consent = true;
  2377. }
  2378. }
  2379. $all_values = $extra_values = array();
  2380. $i = 1; // Prefix counter for stored metadata
  2381. // For all fields, grab label and value
  2382. foreach ( $field_ids['all'] as $field_id ) {
  2383. $field = $this->fields[ $field_id ];
  2384. $label = $i . '_' . $field->get_attribute( 'label' );
  2385. $value = $field->value;
  2386. $all_values[ $label ] = $value;
  2387. $i++; // Increment prefix counter for the next field
  2388. }
  2389. // For the "non-standard" fields, grab label and value
  2390. // Extra fields have their prefix starting from count( $all_values ) + 1
  2391. foreach ( $field_ids['extra'] as $field_id ) {
  2392. $field = $this->fields[ $field_id ];
  2393. $label = $i . '_' . $field->get_attribute( 'label' );
  2394. $value = $field->value;
  2395. if ( is_array( $value ) ) {
  2396. $value = implode( ', ', $value );
  2397. }
  2398. $extra_values[ $label ] = $value;
  2399. $i++; // Increment prefix counter for the next extra field
  2400. }
  2401. if ( isset( $_REQUEST['is_block'] ) && $_REQUEST['is_block'] ) {
  2402. $extra_values['is_block'] = true;
  2403. }
  2404. $contact_form_subject = trim( $contact_form_subject );
  2405. $comment_author_IP = Grunion_Contact_Form_Plugin::get_ip_address();
  2406. $vars = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'contact_form_subject', 'comment_author_IP' );
  2407. foreach ( $vars as $var ) {
  2408. $$var = str_replace( array( "\n", "\r" ), '', $$var );
  2409. }
  2410. // Ensure that Akismet gets all of the relevant information from the contact form,
  2411. // not just the textarea field and predetermined subject.
  2412. $akismet_vars = compact( $vars );
  2413. $akismet_vars['comment_content'] = $comment_content;
  2414. foreach ( array_merge( $field_ids['all'], $field_ids['extra'] ) as $field_id ) {
  2415. $field = $this->fields[ $field_id ];
  2416. // Skip any fields that are just a choice from a pre-defined list. They wouldn't have any value
  2417. // from a spam-filtering point of view.
  2418. if ( in_array( $field->get_attribute( 'type' ), array( 'select', 'checkbox', 'checkbox-multiple', 'radio' ) ) ) {
  2419. continue;
  2420. }
  2421. // Normalize the label into a slug.
  2422. $field_slug = trim( // Strip all leading/trailing dashes.
  2423. preg_replace( // Normalize everything to a-z0-9_-
  2424. '/[^a-z0-9_]+/',
  2425. '-',
  2426. strtolower( $field->get_attribute( 'label' ) ) // Lowercase
  2427. ),
  2428. '-'
  2429. );
  2430. $field_value = ( is_array( $field->value ) ) ? trim( implode( ', ', $field->value ) ) : trim( $field->value );
  2431. // Skip any values that are already in the array we're sending.
  2432. if ( $field_value && in_array( $field_value, $akismet_vars ) ) {
  2433. continue;
  2434. }
  2435. $akismet_vars[ 'contact_form_field_' . $field_slug ] = $field_value;
  2436. }
  2437. $spam = '';
  2438. $akismet_values = $plugin->prepare_for_akismet( $akismet_vars );
  2439. // Is it spam?
  2440. /** This filter is already documented in modules/contact-form/admin.php */
  2441. $is_spam = apply_filters( 'jetpack_contact_form_is_spam', false, $akismet_values );
  2442. if ( is_wp_error( $is_spam ) ) { // WP_Error to abort
  2443. return $is_spam; // abort
  2444. } elseif ( $is_spam === true ) { // TRUE to flag a spam
  2445. $spam = '***SPAM*** ';
  2446. }
  2447. /**
  2448. * Filter whether a submitted contact form is in the comment disallowed list.
  2449. *
  2450. * @module contact-form
  2451. *
  2452. * @since 8.9.0
  2453. *
  2454. * @param bool $result Is the submitted feedback in the disallowed list.
  2455. * @param array $akismet_values Feedack values returned by the Akismet plugin.
  2456. */
  2457. $in_comment_disallowed_list = apply_filters( 'jetpack_contact_form_in_comment_disallowed_list', false, $akismet_values );
  2458. if ( ! $comment_author ) {
  2459. $comment_author = $comment_author_email;
  2460. }
  2461. /**
  2462. * Filter the email where a submitted feedback is sent.
  2463. *
  2464. * @module contact-form
  2465. *
  2466. * @since 1.3.1
  2467. *
  2468. * @param string|array $to Array of valid email addresses, or single email address.
  2469. */
  2470. $to = (array) apply_filters( 'contact_form_to', $to );
  2471. $reply_to_addr = $to[0]; // get just the address part before the name part is added
  2472. foreach ( $to as $to_key => $to_value ) {
  2473. $to[ $to_key ] = Grunion_Contact_Form_Plugin::strip_tags( $to_value );
  2474. $to[ $to_key ] = self::add_name_to_address( $to_value );
  2475. }
  2476. $blog_url = wp_parse_url( site_url() );
  2477. $from_email_addr = 'wordpress@' . $blog_url['host'];
  2478. if ( ! empty( $comment_author_email ) ) {
  2479. $reply_to_addr = $comment_author_email;
  2480. }
  2481. /*
  2482. * The email headers here are formatted in a format
  2483. * that is the most likely to be accepted by wp_mail(),
  2484. * without escaping.
  2485. * More info: https://github.com/Automattic/jetpack/pull/19727
  2486. */
  2487. $headers = 'From: ' . $comment_author . ' <' . $from_email_addr . ">\r\n" .
  2488. 'Reply-To: ' . $comment_author . ' <' . $reply_to_addr . ">\r\n";
  2489. /**
  2490. * Allow customizing the email headers.
  2491. *
  2492. * Warning: DO NOT add headers or header data from the form submission without proper
  2493. * escaping and validation, or you're liable to allow abusers to use your site to send spam.
  2494. *
  2495. * Especially DO NOT take email addresses from the form data to add as CC or BCC headers
  2496. * without strictly validating each address against a list of allowed addresses.
  2497. *
  2498. * @module contact-form
  2499. *
  2500. * @since 10.2.0
  2501. *
  2502. * @param string|array $headers Email headers.
  2503. * @param string $comment_author Name of the author of the submitted feedback, if provided in form.
  2504. * @param string $reply_to_addr Email of the author of the submitted feedback, if provided in form.
  2505. * @param string|array $to Array of valid email addresses, or single email address, where the form is sent.
  2506. */
  2507. $headers = apply_filters(
  2508. 'jetpack_contact_form_email_headers',
  2509. $headers,
  2510. $comment_author,
  2511. $reply_to_addr,
  2512. $to
  2513. );
  2514. $all_values['email_marketing_consent'] = $email_marketing_consent;
  2515. // Build feedback reference
  2516. $feedback_time = current_time( 'mysql' );
  2517. $feedback_title = "{$comment_author} - {$feedback_time}";
  2518. $feedback_id = md5( $feedback_title );
  2519. $entry_values = array(
  2520. 'entry_title' => the_title_attribute( 'echo=0' ),
  2521. 'entry_permalink' => esc_url( get_permalink( get_the_ID() ) ),
  2522. 'feedback_id' => $feedback_id,
  2523. );
  2524. $all_values = array_merge( $all_values, $entry_values );
  2525. /** This filter is already documented in modules/contact-form/admin.php */
  2526. $subject = apply_filters( 'contact_form_subject', $contact_form_subject, $all_values );
  2527. $url = $widget ? home_url( '/' ) : get_permalink( $post->ID );
  2528. $date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' );
  2529. $date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) );
  2530. $time = date_i18n( $date_time_format, current_time( 'timestamp' ) );
  2531. // Keep a copy of the feedback as a custom post type.
  2532. if ( $in_comment_disallowed_list ) {
  2533. $feedback_status = 'trash';
  2534. } elseif ( $is_spam ) {
  2535. $feedback_status = 'spam';
  2536. } else {
  2537. $feedback_status = 'publish';
  2538. }
  2539. foreach ( (array) $akismet_values as $av_key => $av_value ) {
  2540. $akismet_values[ $av_key ] = Grunion_Contact_Form_Plugin::strip_tags( $av_value );
  2541. }
  2542. foreach ( (array) $all_values as $all_key => $all_value ) {
  2543. $all_values[ $all_key ] = Grunion_Contact_Form_Plugin::strip_tags( $all_value );
  2544. }
  2545. foreach ( (array) $extra_values as $ev_key => $ev_value ) {
  2546. $extra_values[ $ev_key ] = Grunion_Contact_Form_Plugin::strip_tags( $ev_value );
  2547. }
  2548. /*
  2549. We need to make sure that the post author is always zero for contact
  2550. * form submissions. This prevents export/import from trying to create
  2551. * new users based on form submissions from people who were logged in
  2552. * at the time.
  2553. *
  2554. * Unfortunately wp_insert_post() tries very hard to make sure the post
  2555. * author gets the currently logged in user id. That is how we ended up
  2556. * with this work around. */
  2557. add_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );
  2558. $post_id = wp_insert_post(
  2559. array(
  2560. 'post_date' => addslashes( $feedback_time ),
  2561. 'post_type' => 'feedback',
  2562. 'post_status' => addslashes( $feedback_status ),
  2563. 'post_parent' => (int) $post->ID,
  2564. 'post_title' => addslashes( wp_kses( $feedback_title, array() ) ),
  2565. '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
  2566. 'post_name' => $feedback_id,
  2567. )
  2568. );
  2569. // once insert has finished we don't need this filter any more
  2570. remove_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10 );
  2571. update_post_meta( $post_id, '_feedback_extra_fields', $this->addslashes_deep( $extra_values ) );
  2572. if ( 'publish' == $feedback_status ) {
  2573. // Increase count of unread feedback.
  2574. $unread = get_option( 'feedback_unread_count', 0 ) + 1;
  2575. update_option( 'feedback_unread_count', $unread );
  2576. }
  2577. if ( defined( 'AKISMET_VERSION' ) ) {
  2578. update_post_meta( $post_id, '_feedback_akismet_values', $this->addslashes_deep( $akismet_values ) );
  2579. }
  2580. /**
  2581. * Fires after the feedback post for the contact form submission has been inserted.
  2582. *
  2583. * @module contact-form
  2584. *
  2585. * @since 8.6.0
  2586. *
  2587. * @param integer $post_id The post id that contains the contact form data.
  2588. * @param array $this->fields An array containg the form's Grunion_Contact_Form_Field objects.
  2589. * @param boolean $is_spam Whether the form submission has been identified as spam.
  2590. * @param array $entry_values The feedback entry values.
  2591. */
  2592. do_action( 'grunion_after_feedback_post_inserted', $post_id, $this->fields, $is_spam, $entry_values );
  2593. $message = self::get_compiled_form( $post_id, $this );
  2594. array_push(
  2595. $message,
  2596. '<br />',
  2597. '<hr />',
  2598. __( 'Time:', 'jetpack' ) . ' ' . $time . '<br />',
  2599. __( 'IP Address:', 'jetpack' ) . ' ' . $comment_author_IP . '<br />',
  2600. __( 'Contact Form URL:', 'jetpack' ) . ' ' . $url . '<br />'
  2601. );
  2602. if ( is_user_logged_in() ) {
  2603. array_push(
  2604. $message,
  2605. sprintf(
  2606. '<p>' . __( 'Sent by a verified %s user.', 'jetpack' ) . '</p>',
  2607. isset( $GLOBALS['current_site']->site_name ) && $GLOBALS['current_site']->site_name ?
  2608. $GLOBALS['current_site']->site_name : '"' . get_option( 'blogname' ) . '"'
  2609. )
  2610. );
  2611. } else {
  2612. array_push( $message, '<p>' . __( 'Sent by an unverified visitor to your site.', 'jetpack' ) . '</p>' );
  2613. }
  2614. $message = join( '', $message );
  2615. /**
  2616. * Filters the message sent via email after a successful form submission.
  2617. *
  2618. * @module contact-form
  2619. *
  2620. * @since 1.3.1
  2621. *
  2622. * @param string $message Feedback email message.
  2623. */
  2624. $message = apply_filters( 'contact_form_message', $message );
  2625. // This is called after `contact_form_message`, in order to preserve back-compat
  2626. $message = self::wrap_message_in_html_tags( $message );
  2627. update_post_meta( $post_id, '_feedback_email', $this->addslashes_deep( compact( 'to', 'message' ) ) );
  2628. /**
  2629. * Fires right before the contact form message is sent via email to
  2630. * the recipient specified in the contact form.
  2631. *
  2632. * @module contact-form
  2633. *
  2634. * @since 1.3.1
  2635. *
  2636. * @param integer $post_id Post contact form lives on
  2637. * @param array $all_values Contact form fields
  2638. * @param array $extra_values Contact form fields not included in $all_values
  2639. */
  2640. do_action( 'grunion_pre_message_sent', $post_id, $all_values, $extra_values );
  2641. // schedule deletes of old spam feedbacks
  2642. if ( ! wp_next_scheduled( 'grunion_scheduled_delete' ) ) {
  2643. wp_schedule_event( time() + 250, 'daily', 'grunion_scheduled_delete' );
  2644. }
  2645. if (
  2646. $is_spam !== true &&
  2647. /**
  2648. * Filter to choose whether an email should be sent after each successful contact form submission.
  2649. *
  2650. * @module contact-form
  2651. *
  2652. * @since 2.6.0
  2653. *
  2654. * @param bool true Should an email be sent after a form submission. Default to true.
  2655. * @param int $post_id Post ID.
  2656. */
  2657. true === apply_filters( 'grunion_should_send_email', true, $post_id )
  2658. ) {
  2659. self::wp_mail( $to, "{$spam}{$subject}", $message, $headers );
  2660. } elseif (
  2661. true === $is_spam &&
  2662. /**
  2663. * Choose whether an email should be sent for each spam contact form submission.
  2664. *
  2665. * @module contact-form
  2666. *
  2667. * @since 1.3.1
  2668. *
  2669. * @param bool false Should an email be sent after a spam form submission. Default to false.
  2670. */
  2671. apply_filters( 'grunion_still_email_spam', false ) == true
  2672. ) { // don't send spam by default. Filterable.
  2673. self::wp_mail( $to, "{$spam}{$subject}", $message, $headers );
  2674. }
  2675. /**
  2676. * Fires an action hook right after the email(s) have been sent.
  2677. *
  2678. * @module contact-form
  2679. *
  2680. * @since 7.3.0
  2681. *
  2682. * @param int $post_id Post contact form lives on.
  2683. * @param string|array $to Array of valid email addresses, or single email address.
  2684. * @param string $subject Feedback email subject.
  2685. * @param string $message Feedback email message.
  2686. * @param string|array $headers Optional. Additional headers.
  2687. * @param array $all_values Contact form fields.
  2688. * @param array $extra_values Contact form fields not included in $all_values
  2689. */
  2690. do_action( 'grunion_after_message_sent', $post_id, $to, $subject, $message, $headers, $all_values, $extra_values );
  2691. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  2692. return self::success_message( $post_id, $this );
  2693. }
  2694. $redirect = '';
  2695. $custom_redirect = false;
  2696. if ( 'redirect' === $this->get_attribute( 'customThankyou' ) ) {
  2697. $custom_redirect = true;
  2698. $redirect = esc_url( $this->get_attribute( 'customThankyouRedirect' ) );
  2699. }
  2700. if ( ! $redirect ) {
  2701. $custom_redirect = false;
  2702. $redirect = wp_get_referer();
  2703. }
  2704. if ( ! $redirect ) { // wp_get_referer() returns false if the referer is the same as the current page.
  2705. $custom_redirect = false;
  2706. $redirect = $_SERVER['REQUEST_URI'];
  2707. }
  2708. if ( ! $custom_redirect ) {
  2709. $redirect = add_query_arg(
  2710. urlencode_deep(
  2711. array(
  2712. 'contact-form-id' => $id,
  2713. 'contact-form-sent' => $post_id,
  2714. 'contact-form-hash' => $this->hash,
  2715. '_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :( .
  2716. )
  2717. ),
  2718. $redirect
  2719. );
  2720. }
  2721. /**
  2722. * Filter the URL where the reader is redirected after submitting a form.
  2723. *
  2724. * @module contact-form
  2725. *
  2726. * @since 1.9.0
  2727. *
  2728. * @param string $redirect Post submission URL.
  2729. * @param int $id Contact Form ID.
  2730. * @param int $post_id Post ID.
  2731. */
  2732. $redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id );
  2733. // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We intentially allow external redirects here.
  2734. wp_redirect( $redirect );
  2735. exit;
  2736. }
  2737. /**
  2738. * Wrapper for wp_mail() that enables HTML messages with text alternatives
  2739. *
  2740. * @param string|array $to Array or comma-separated list of email addresses to send message.
  2741. * @param string $subject Email subject.
  2742. * @param string $message Message contents.
  2743. * @param string|array $headers Optional. Additional headers.
  2744. * @param string|array $attachments Optional. Files to attach.
  2745. *
  2746. * @return bool Whether the email contents were sent successfully.
  2747. */
  2748. public static function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  2749. add_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
  2750. add_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );
  2751. $result = wp_mail( $to, $subject, $message, $headers, $attachments );
  2752. remove_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
  2753. remove_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );
  2754. return $result;
  2755. }
  2756. /**
  2757. * Add a display name part to an email address
  2758. *
  2759. * SpamAssassin doesn't like addresses in HTML messages that are missing display names (e.g., `foo@bar.org`
  2760. * instead of `Foo Bar <foo@bar.org>`.
  2761. *
  2762. * @param string $address
  2763. *
  2764. * @return string
  2765. */
  2766. function add_name_to_address( $address ) {
  2767. // If it's just the address, without a display name
  2768. if ( is_email( $address ) ) {
  2769. $address_parts = explode( '@', $address );
  2770. /*
  2771. * The email address format here is formatted in a format
  2772. * that is the most likely to be accepted by wp_mail(),
  2773. * without escaping.
  2774. * More info: https://github.com/Automattic/jetpack/pull/19727
  2775. */
  2776. $address = sprintf( '%s <%s>', $address_parts[0], $address );
  2777. }
  2778. return $address;
  2779. }
  2780. /**
  2781. * Get the content type that should be assigned to outbound emails
  2782. *
  2783. * @return string
  2784. */
  2785. static function get_mail_content_type() {
  2786. return 'text/html';
  2787. }
  2788. /**
  2789. * Wrap a message body with the appropriate in HTML tags
  2790. *
  2791. * This helps to ensure correct parsing by clients, and also helps avoid triggering spam filtering rules
  2792. *
  2793. * @param string $body
  2794. *
  2795. * @return string
  2796. */
  2797. static function wrap_message_in_html_tags( $body ) {
  2798. // Don't do anything if the message was already wrapped in HTML tags
  2799. // That could have be done by a plugin via filters
  2800. if ( false !== strpos( $body, '<html' ) ) {
  2801. return $body;
  2802. }
  2803. $html_message = sprintf(
  2804. // The tabs are just here so that the raw code is correctly formatted for developers
  2805. // They're removed so that they don't affect the final message sent to users
  2806. str_replace(
  2807. "\t", '',
  2808. '<!doctype html>
  2809. <html xmlns="http://www.w3.org/1999/xhtml">
  2810. <body>
  2811. %s
  2812. </body>
  2813. </html>'
  2814. ),
  2815. $body
  2816. );
  2817. return $html_message;
  2818. }
  2819. /**
  2820. * Add a plain-text alternative part to an outbound email
  2821. *
  2822. * This makes the message more accessible to mail clients that aren't HTML-aware, and decreases the likelihood
  2823. * that the message will be flagged as spam.
  2824. *
  2825. * @param PHPMailer $phpmailer
  2826. */
  2827. static function add_plain_text_alternative( $phpmailer ) {
  2828. // Add an extra break so that the extra space above the <p> is preserved after the <p> is stripped out
  2829. $alt_body = str_replace( '<p>', '<p><br />', $phpmailer->Body );
  2830. // Convert <br> to \n breaks, to preserve the space between lines that we want to keep
  2831. $alt_body = str_replace( array( '<br>', '<br />' ), "\n", $alt_body );
  2832. // Convert <hr> to an plain-text equivalent, to preserve the integrity of the message
  2833. $alt_body = str_replace( array( '<hr>', '<hr />' ), "----\n", $alt_body );
  2834. // Trim the plain text message to remove the \n breaks that were after <doctype>, <html>, and <body>
  2835. $phpmailer->AltBody = trim( strip_tags( $alt_body ) );
  2836. }
  2837. function addslashes_deep( $value ) {
  2838. if ( is_array( $value ) ) {
  2839. return array_map( array( $this, 'addslashes_deep' ), $value );
  2840. } elseif ( is_object( $value ) ) {
  2841. $vars = get_object_vars( $value );
  2842. foreach ( $vars as $key => $data ) {
  2843. $value->{$key} = $this->addslashes_deep( $data );
  2844. }
  2845. return $value;
  2846. }
  2847. return addslashes( $value );
  2848. }
  2849. } // end class Grunion_Contact_Form
  2850. /**
  2851. * Class for the contact-field shortcode.
  2852. * Parses shortcode to output the contact form field as HTML.
  2853. * Validates input.
  2854. */
  2855. class Grunion_Contact_Form_Field extends Crunion_Contact_Form_Shortcode {
  2856. public $shortcode_name = 'contact-field';
  2857. /**
  2858. * @var Grunion_Contact_Form parent form
  2859. */
  2860. public $form;
  2861. /**
  2862. * @var string default or POSTed value
  2863. */
  2864. public $value;
  2865. /**
  2866. * @var bool Is the input invalid?
  2867. */
  2868. public $error = false;
  2869. /**
  2870. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
  2871. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
  2872. * @param Grunion_Contact_Form $form The parent form
  2873. */
  2874. function __construct( $attributes, $content = null, $form = null ) {
  2875. $attributes = shortcode_atts(
  2876. array(
  2877. 'label' => null,
  2878. 'type' => 'text',
  2879. 'required' => false,
  2880. 'options' => array(),
  2881. 'id' => null,
  2882. 'default' => null,
  2883. 'values' => null,
  2884. 'placeholder' => null,
  2885. 'class' => null,
  2886. 'width' => null,
  2887. 'consenttype' => null,
  2888. 'implicitconsentmessage' => null,
  2889. 'explicitconsentmessage' => null,
  2890. ), $attributes, 'contact-field'
  2891. );
  2892. // special default for subject field
  2893. if ( 'subject' == $attributes['type'] && is_null( $attributes['default'] ) && ! is_null( $form ) ) {
  2894. $attributes['default'] = $form->get_attribute( 'subject' );
  2895. }
  2896. // allow required=1 or required=true
  2897. if ( '1' == $attributes['required'] || 'true' == strtolower( $attributes['required'] ) ) {
  2898. $attributes['required'] = true;
  2899. } else {
  2900. $attributes['required'] = false;
  2901. }
  2902. // parse out comma-separated options list (for selects, radios, and checkbox-multiples)
  2903. if ( ! empty( $attributes['options'] ) && is_string( $attributes['options'] ) ) {
  2904. $attributes['options'] = array_map( 'trim', explode( ',', $attributes['options'] ) );
  2905. if ( ! empty( $attributes['values'] ) && is_string( $attributes['values'] ) ) {
  2906. $attributes['values'] = array_map( 'trim', explode( ',', $attributes['values'] ) );
  2907. }
  2908. }
  2909. if ( $form ) {
  2910. // make a unique field ID based on the label, with an incrementing number if needed to avoid clashes
  2911. $form_id = $form->get_attribute( 'id' );
  2912. $id = isset( $attributes['id'] ) ? $attributes['id'] : false;
  2913. $unescaped_label = $this->unesc_attr( $attributes['label'] );
  2914. $unescaped_label = str_replace( '%', '-', $unescaped_label ); // jQuery doesn't like % in IDs?
  2915. $unescaped_label = preg_replace( '/[^a-zA-Z0-9.-_:]/', '', $unescaped_label );
  2916. if ( empty( $id ) ) {
  2917. $id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label );
  2918. $i = 0;
  2919. $max_tries = 99;
  2920. while ( isset( $form->fields[ $id ] ) ) {
  2921. $i++;
  2922. $id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label . '-' . $i );
  2923. if ( $i > $max_tries ) {
  2924. break;
  2925. }
  2926. }
  2927. }
  2928. $attributes['id'] = $id;
  2929. }
  2930. parent::__construct( $attributes, $content );
  2931. // Store parent form
  2932. $this->form = $form;
  2933. }
  2934. /**
  2935. * This field's input is invalid. Flag as invalid and add an error to the parent form
  2936. *
  2937. * @param string $message The error message to display on the form.
  2938. */
  2939. function add_error( $message ) {
  2940. $this->is_error = true;
  2941. if ( ! is_wp_error( $this->form->errors ) ) {
  2942. $this->form->errors = new WP_Error;
  2943. }
  2944. $this->form->errors->add( $this->get_attribute( 'id' ), $message );
  2945. }
  2946. /**
  2947. * Is the field input invalid?
  2948. *
  2949. * @see $error
  2950. *
  2951. * @return bool
  2952. */
  2953. function is_error() {
  2954. return $this->error;
  2955. }
  2956. /**
  2957. * Validates the form input
  2958. */
  2959. function validate() {
  2960. // If it's not required, there's nothing to validate
  2961. if ( ! $this->get_attribute( 'required' ) ) {
  2962. return;
  2963. }
  2964. $field_id = $this->get_attribute( 'id' );
  2965. $field_type = $this->get_attribute( 'type' );
  2966. $field_label = $this->get_attribute( 'label' );
  2967. if ( isset( $_POST[ $field_id ] ) ) {
  2968. if ( is_array( $_POST[ $field_id ] ) ) {
  2969. $field_value = array_map( 'stripslashes', $_POST[ $field_id ] );
  2970. } else {
  2971. $field_value = stripslashes( $_POST[ $field_id ] );
  2972. }
  2973. } else {
  2974. $field_value = '';
  2975. }
  2976. switch ( $field_type ) {
  2977. case 'email':
  2978. // Make sure the email address is valid
  2979. if ( ! is_string( $field_value ) || ! is_email( $field_value ) ) {
  2980. /* translators: %s is the name of a form field */
  2981. $this->add_error( sprintf( __( '%s requires a valid email address', 'jetpack' ), $field_label ) );
  2982. }
  2983. break;
  2984. case 'checkbox-multiple':
  2985. // Check that there is at least one option selected
  2986. if ( empty( $field_value ) ) {
  2987. /* translators: %s is the name of a form field */
  2988. $this->add_error( sprintf( __( '%s requires at least one selection', 'jetpack' ), $field_label ) );
  2989. }
  2990. break;
  2991. default:
  2992. // Just check for presence of any text
  2993. if ( ! is_string( $field_value ) || ! strlen( trim( $field_value ) ) ) {
  2994. /* translators: %s is the name of a form field */
  2995. $this->add_error( sprintf( __( '%s is required', 'jetpack' ), $field_label ) );
  2996. }
  2997. }
  2998. }
  2999. /**
  3000. * Check the default value for options field
  3001. *
  3002. * @param string value
  3003. * @param int index
  3004. * @param string default value
  3005. *
  3006. * @return string
  3007. */
  3008. public function get_option_value( $value, $index, $options ) {
  3009. if ( empty( $value[ $index ] ) ) {
  3010. return $options;
  3011. }
  3012. return $value[ $index ];
  3013. }
  3014. /**
  3015. * Outputs the HTML for this form field
  3016. *
  3017. * @return string HTML
  3018. */
  3019. function render() {
  3020. global $current_user, $user_identity;
  3021. $field_id = $this->get_attribute( 'id' );
  3022. $field_type = $this->get_attribute( 'type' );
  3023. $field_label = $this->get_attribute( 'label' );
  3024. $field_required = $this->get_attribute( 'required' );
  3025. $field_placeholder = $this->get_attribute( 'placeholder' );
  3026. $field_width = $this->get_attribute( 'width' );
  3027. $class = 'date' === $field_type ? 'jp-contact-form-date' : $this->get_attribute( 'class' );
  3028. if ( ! empty( $field_width ) ) {
  3029. $class .= ' grunion-field-width-' . $field_width;
  3030. }
  3031. /**
  3032. * Filters the "class" attribute of the contact form input
  3033. *
  3034. * @module contact-form
  3035. *
  3036. * @since 6.6.0
  3037. *
  3038. * @param string $class Additional CSS classes for input class attribute.
  3039. */
  3040. $field_class = apply_filters( 'jetpack_contact_form_input_class', $class );
  3041. if ( isset( $_POST[ $field_id ] ) ) {
  3042. if ( is_array( $_POST[ $field_id ] ) ) {
  3043. $this->value = array_map( 'stripslashes', $_POST[ $field_id ] );
  3044. } else {
  3045. $this->value = stripslashes( (string) $_POST[ $field_id ] );
  3046. }
  3047. } elseif ( isset( $_GET[ $field_id ] ) ) {
  3048. $this->value = stripslashes( (string) $_GET[ $field_id ] );
  3049. } elseif (
  3050. is_user_logged_in() &&
  3051. ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
  3052. /**
  3053. * Allow third-party tools to prefill the contact form with the user's details when they're logged in.
  3054. *
  3055. * @module contact-form
  3056. *
  3057. * @since 3.2.0
  3058. *
  3059. * @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false.
  3060. */
  3061. true === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
  3062. )
  3063. ) {
  3064. // Special defaults for logged-in users
  3065. switch ( $this->get_attribute( 'type' ) ) {
  3066. case 'email':
  3067. $this->value = $current_user->data->user_email;
  3068. break;
  3069. case 'name':
  3070. $this->value = $user_identity;
  3071. break;
  3072. case 'url':
  3073. $this->value = $current_user->data->user_url;
  3074. break;
  3075. default:
  3076. $this->value = $this->get_attribute( 'default' );
  3077. }
  3078. } else {
  3079. $this->value = $this->get_attribute( 'default' );
  3080. }
  3081. $field_value = Grunion_Contact_Form_Plugin::strip_tags( $this->value );
  3082. $field_label = Grunion_Contact_Form_Plugin::strip_tags( $field_label );
  3083. $rendered_field = $this->render_field( $field_type, $field_id, $field_label, $field_value, $field_class, $field_placeholder, $field_required );
  3084. /**
  3085. * Filter the HTML of the Contact Form.
  3086. *
  3087. * @module contact-form
  3088. *
  3089. * @since 2.6.0
  3090. *
  3091. * @param string $rendered_field Contact Form HTML output.
  3092. * @param string $field_label Field label.
  3093. * @param int|null $id Post ID.
  3094. */
  3095. return apply_filters( 'grunion_contact_form_field_html', $rendered_field, $field_label, ( in_the_loop() ? get_the_ID() : null ) );
  3096. }
  3097. public function render_label( $type, $id, $label, $required, $required_field_text ) {
  3098. $type_class = $type ? ' ' .$type : '';
  3099. return
  3100. "<label
  3101. for='" . esc_attr( $id ) . "'
  3102. class='grunion-field-label{$type_class}" . ( $this->is_error() ? ' form-error' : '' ) . "'
  3103. >"
  3104. . esc_html( $label )
  3105. . ( $required ? '<span>' . $required_field_text . '</span>' : '' )
  3106. . "</label>\n";
  3107. }
  3108. function render_input_field( $type, $id, $value, $class, $placeholder, $required ) {
  3109. return "<input
  3110. type='". esc_attr( $type ) ."'
  3111. name='" . esc_attr( $id ) . "'
  3112. id='" . esc_attr( $id ) . "'
  3113. value='" . esc_attr( $value ) . "'
  3114. " . $class . $placeholder . '
  3115. ' . ( $required ? "required aria-required='true'" : '' ) . "
  3116. />\n";
  3117. }
  3118. function render_email_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
  3119. $field = $this->render_label( 'email', $id, $label, $required, $required_field_text );
  3120. $field .= $this->render_input_field( 'email', $id, $value, $class, $placeholder, $required );
  3121. return $field;
  3122. }
  3123. function render_telephone_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
  3124. $field = $this->render_label( 'telephone', $id, $label, $required, $required_field_text );
  3125. $field .= $this->render_input_field( 'tel', $id, $value, $class, $placeholder, $required );
  3126. return $field;
  3127. }
  3128. function render_url_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
  3129. $field = $this->render_label( 'url', $id, $label, $required, $required_field_text );
  3130. $field .= $this->render_input_field( 'url', $id, $value, $class, $placeholder, $required );
  3131. return $field;
  3132. }
  3133. function render_textarea_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
  3134. $field = $this->render_label( 'textarea', 'contact-form-comment-' . $id, $label, $required, $required_field_text );
  3135. $field .= "<textarea
  3136. name='" . esc_attr( $id ) . "'
  3137. id='contact-form-comment-" . esc_attr( $id ) . "'
  3138. rows='20' "
  3139. . $class
  3140. . $placeholder
  3141. . ' ' . ( $required ? "required aria-required='true'" : '' ) .
  3142. '>' . esc_textarea( $value )
  3143. . "</textarea>\n";
  3144. return $field;
  3145. }
  3146. function render_radio_field( $id, $label, $value, $class, $required, $required_field_text ) {
  3147. $field = $this->render_label( '', $id, $label, $required, $required_field_text );
  3148. foreach ( (array) $this->get_attribute( 'options' ) as $optionIndex => $option ) {
  3149. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  3150. if ( $option ) {
  3151. $field .= "\t\t<label class='grunion-radio-label radio" . ( $this->is_error() ? ' form-error' : '' ) . "'>";
  3152. $field .= "<input
  3153. type='radio'
  3154. name='" . esc_attr( $id ) . "'
  3155. value='" . esc_attr( $this->get_option_value( $this->get_attribute( 'values' ), $optionIndex, $option ) ) . "' "
  3156. . $class
  3157. . checked( $option, $value, false ) . ' '
  3158. . ( $required ? "required aria-required='true'" : '' )
  3159. . '/> ';
  3160. $field .= esc_html( $option ) . "</label>\n";
  3161. $field .= "\t\t<div class='clear-form'></div>\n";
  3162. }
  3163. }
  3164. return $field;
  3165. }
  3166. function render_checkbox_field( $id, $label, $value, $class, $required, $required_field_text ) {
  3167. $field = "<label class='grunion-field-label checkbox" . ( $this->is_error() ? ' form-error' : '' ) . "'>";
  3168. $field .= "\t\t<input type='checkbox' name='" . esc_attr( $id ) . "' value='" . esc_attr__( 'Yes', 'jetpack' ) . "' " . $class . checked( (bool) $value, true, false ) . ' ' . ( $required ? "required aria-required='true'" : '' ) . "/> \n";
  3169. $field .= "\t\t" . esc_html( $label ) . ( $required ? '<span>' . $required_field_text . '</span>' : '' );
  3170. $field .= "</label>\n";
  3171. $field .= "<div class='clear-form'></div>\n";
  3172. return $field;
  3173. }
  3174. /**
  3175. * Render the consent field.
  3176. *
  3177. * @param string $id field id.
  3178. * @param string $class html classes (can be set by the admin).
  3179. */
  3180. private function render_consent_field( $id, $class ) {
  3181. $consent_type = 'explicit' === $this->get_attribute( 'consenttype' ) ? 'explicit' : 'implicit';
  3182. $consent_message = 'explicit' === $consent_type ? $this->get_attribute( 'explicitconsentmessage' ) : $this->get_attribute( 'implicitconsentmessage' );
  3183. $field = "<label class='grunion-field-label consent consent-" . $consent_type . "'>";
  3184. if ( 'implicit' === $consent_type ) {
  3185. $field .= "\t\t<input aria-hidden='true' type='checkbox' checked name='" . esc_attr( $id ) . "' value='" . esc_attr__( 'Yes', 'jetpack' ) . "' style='display:none;' /> \n";
  3186. } else {
  3187. $field .= "\t\t<input type='checkbox' name='" . esc_attr( $id ) . "' value='" . esc_attr__( 'Yes', 'jetpack' ) . "' " . $class . "/> \n";
  3188. }
  3189. $field .= "\t\t" . esc_html( $consent_message );
  3190. $field .= "</label>\n";
  3191. $field .= "<div class='clear-form'></div>\n";
  3192. return $field;
  3193. }
  3194. function render_checkbox_multiple_field( $id, $label, $value, $class, $required, $required_field_text ) {
  3195. $field = $this->render_label( '', $id, $label, $required, $required_field_text );
  3196. foreach ( (array) $this->get_attribute( 'options' ) as $optionIndex => $option ) {
  3197. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  3198. if ( $option ) {
  3199. $field .= "\t\t<label class='grunion-checkbox-multiple-label checkbox-multiple" . ( $this->is_error() ? ' form-error' : '' ) . "'>";
  3200. $field .= "<input type='checkbox' name='" . esc_attr( $id ) . "[]' value='" . esc_attr( $this->get_option_value( $this->get_attribute( 'values' ), $optionIndex, $option ) ) . "' " . $class . checked( in_array( $option, (array) $value ), true, false ) . ' /> ';
  3201. $field .= esc_html( $option ) . "</label>\n";
  3202. $field .= "\t\t<div class='clear-form'></div>\n";
  3203. }
  3204. }
  3205. return $field;
  3206. }
  3207. function render_select_field( $id, $label, $value, $class, $required, $required_field_text ) {
  3208. $field = $this->render_label( 'select', $id, $label, $required, $required_field_text );
  3209. $field .= "\t<select name='" . esc_attr( $id ) . "' id='" . esc_attr( $id ) . "' " . $class . ( $required ? "required aria-required='true'" : '' ) . ">\n";
  3210. foreach ( (array) $this->get_attribute( 'options' ) as $optionIndex => $option ) {
  3211. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  3212. if ( $option ) {
  3213. $field .= "\t\t<option"
  3214. . selected( $option, $value, false )
  3215. . " value='" . esc_attr( $this->get_option_value( $this->get_attribute( 'values' ), $optionIndex, $option ) )
  3216. . "'>" . esc_html( $option )
  3217. . "</option>\n";
  3218. }
  3219. }
  3220. $field .= "\t</select>\n";
  3221. return $field;
  3222. }
  3223. function render_date_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
  3224. $field = $this->render_label( 'date', $id, $label, $required, $required_field_text );
  3225. $field .= $this->render_input_field( 'text', $id, $value, $class, $placeholder, $required );
  3226. /* For AMP requests, use amp-date-picker element: https://amp.dev/documentation/components/amp-date-picker */
  3227. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  3228. return sprintf(
  3229. '<%1$s mode="overlay" layout="container" type="single" input-selector="[name=%2$s]">%3$s</%1$s>',
  3230. 'amp-date-picker',
  3231. esc_attr( $id ),
  3232. $field
  3233. );
  3234. }
  3235. wp_enqueue_script(
  3236. 'grunion-frontend',
  3237. Assets::get_file_url_for_environment(
  3238. '_inc/build/contact-form/js/grunion-frontend.min.js',
  3239. 'modules/contact-form/js/grunion-frontend.js'
  3240. ),
  3241. array( 'jquery', 'jquery-ui-datepicker' )
  3242. );
  3243. wp_enqueue_style( 'jp-jquery-ui-datepicker', plugins_url( 'css/jquery-ui-datepicker.css', __FILE__ ), array( 'dashicons' ), '1.0' );
  3244. // Using Core's built-in datepicker localization routine
  3245. wp_localize_jquery_ui_datepicker();
  3246. return $field;
  3247. }
  3248. function render_default_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder, $type ) {
  3249. $field = $this->render_label( $type, $id, $label, $required, $required_field_text );
  3250. $field .= $this->render_input_field( 'text', $id, $value, $class, $placeholder, $required );
  3251. return $field;
  3252. }
  3253. function render_field( $type, $id, $label, $value, $class, $placeholder, $required ) {
  3254. $field_placeholder = ( ! empty( $placeholder ) ) ? "placeholder='" . esc_attr( $placeholder ) . "'" : '';
  3255. $field_class = "class='" . trim( esc_attr( $type ) . ' ' . esc_attr( $class ) ) . "' ";
  3256. $wrap_classes = empty( $class ) ? '' : implode( '-wrap ', array_filter( explode( ' ', $class ) ) ) . '-wrap'; // this adds
  3257. $shell_field_class = "class='grunion-field-wrap grunion-field-" . trim( esc_attr( $type ) . '-wrap ' . esc_attr( $wrap_classes ) ) . "' ";
  3258. /**
  3259. /**
  3260. * Filter the Contact Form required field text
  3261. *
  3262. * @module contact-form
  3263. *
  3264. * @since 3.8.0
  3265. *
  3266. * @param string $var Required field text. Default is "(required)".
  3267. */
  3268. $required_field_text = esc_html( apply_filters( 'jetpack_required_field_text', __( '(required)', 'jetpack' ) ) );
  3269. $field = "\n<div {$shell_field_class} >\n"; // new in Jetpack 6.8.0
  3270. // If they are logged in, and this is their site, don't pre-populate fields
  3271. if ( current_user_can( 'manage_options' ) ) {
  3272. $value = '';
  3273. }
  3274. switch ( $type ) {
  3275. case 'email':
  3276. $field .= $this->render_email_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
  3277. break;
  3278. case 'telephone':
  3279. $field .= $this->render_telephone_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
  3280. break;
  3281. case 'url':
  3282. $field .= $this->render_url_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
  3283. break;
  3284. case 'textarea':
  3285. $field .= $this->render_textarea_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
  3286. break;
  3287. case 'radio':
  3288. $field .= $this->render_radio_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
  3289. break;
  3290. case 'checkbox':
  3291. $field .= $this->render_checkbox_field( $id, $label, $value, $field_class, $required, $required_field_text );
  3292. break;
  3293. case 'checkbox-multiple':
  3294. $field .= $this->render_checkbox_multiple_field( $id, $label, $value, $field_class, $required, $required_field_text );
  3295. break;
  3296. case 'select':
  3297. $field .= $this->render_select_field( $id, $label, $value, $field_class, $required, $required_field_text );
  3298. break;
  3299. case 'date':
  3300. $field .= $this->render_date_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
  3301. break;
  3302. case 'consent':
  3303. $field .= $this->render_consent_field( $id, $field_class );
  3304. break;
  3305. default: // text field
  3306. $field .= $this->render_default_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder, $type );
  3307. break;
  3308. }
  3309. $field .= "\t</div>\n";
  3310. return $field;
  3311. }
  3312. }
  3313. add_action( 'init', array( 'Grunion_Contact_Form_Plugin', 'init' ), 9 );
  3314. add_action( 'grunion_scheduled_delete', 'grunion_delete_old_spam' );
  3315. /**
  3316. * Deletes old spam feedbacks to keep the posts table size under control
  3317. */
  3318. function grunion_delete_old_spam() {
  3319. global $wpdb;
  3320. $grunion_delete_limit = 100;
  3321. $now_gmt = current_time( 'mysql', 1 );
  3322. $sql = $wpdb->prepare(
  3323. "
  3324. SELECT `ID`
  3325. FROM $wpdb->posts
  3326. WHERE DATE_SUB( %s, INTERVAL 15 DAY ) > `post_date_gmt`
  3327. AND `post_type` = 'feedback'
  3328. AND `post_status` = 'spam'
  3329. LIMIT %d
  3330. ", $now_gmt, $grunion_delete_limit
  3331. );
  3332. $post_ids = $wpdb->get_col( $sql );
  3333. foreach ( (array) $post_ids as $post_id ) {
  3334. // force a full delete, skip the trash
  3335. wp_delete_post( $post_id, true );
  3336. }
  3337. if (
  3338. /**
  3339. * Filter if the module run OPTIMIZE TABLE on the core WP tables.
  3340. *
  3341. * @module contact-form
  3342. *
  3343. * @since 1.3.1
  3344. * @since 6.4.0 Set to false by default.
  3345. *
  3346. * @param bool $filter Should Jetpack optimize the table, defaults to false.
  3347. */
  3348. apply_filters( 'grunion_optimize_table', false )
  3349. ) {
  3350. $wpdb->query( "OPTIMIZE TABLE $wpdb->posts" );
  3351. }
  3352. // if we hit the max then schedule another run
  3353. if ( count( $post_ids ) >= $grunion_delete_limit ) {
  3354. wp_schedule_single_event( time() + 700, 'grunion_scheduled_delete' );
  3355. }
  3356. }
  3357. /**
  3358. * Send an event to Tracks on form submission.
  3359. *
  3360. * @param int $post_id - the post_id for the CPT that is created.
  3361. * @param array $all_values - fields from the default contact form.
  3362. * @param array $extra_values - extra fields added to from the contact form.
  3363. *
  3364. * @return null|void
  3365. */
  3366. function jetpack_tracks_record_grunion_pre_message_sent( $post_id, $all_values, $extra_values ) {
  3367. // Do not do anything if the submission is not from a block.
  3368. if (
  3369. ! isset( $extra_values['is_block'] )
  3370. || ! $extra_values['is_block']
  3371. ) {
  3372. return;
  3373. }
  3374. /*
  3375. * Event details.
  3376. */
  3377. $event_user = wp_get_current_user();
  3378. $event_name = 'contact_form_block_message_sent';
  3379. $event_props = array(
  3380. 'entry_permalink' => esc_url( $all_values['entry_permalink'] ),
  3381. 'feedback_id' => esc_attr( $all_values['feedback_id'] ),
  3382. );
  3383. /*
  3384. * Record event.
  3385. * We use different libs on wpcom and Jetpack.
  3386. */
  3387. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  3388. $event_name = 'wpcom_' . $event_name;
  3389. $event_props['blog_id'] = get_current_blog_id();
  3390. // If the form was sent by a logged out visitor, record event with blog owner.
  3391. if ( empty( $event_user->ID ) ) {
  3392. $event_user_id = wpcom_get_blog_owner( $event_props['blog_id'] );
  3393. $event_user = get_userdata( $event_user_id );
  3394. }
  3395. jetpack_require_lib( 'tracks/client' );
  3396. tracks_record_event( $event_user, $event_name, $event_props );
  3397. } else {
  3398. // If the form was sent by a logged out visitor, record event with Jetpack master user.
  3399. if ( empty( $event_user->ID ) ) {
  3400. $master_user_id = Jetpack_Options::get_option( 'master_user' );
  3401. if ( ! empty( $master_user_id ) ) {
  3402. $event_user = get_userdata( $master_user_id );
  3403. }
  3404. }
  3405. $tracking = new Automattic\Jetpack\Tracking();
  3406. $tracking->record_user_event( $event_name, $event_props, $event_user );
  3407. }
  3408. }
  3409. add_action( 'grunion_pre_message_sent', 'jetpack_tracks_record_grunion_pre_message_sent', 12, 3 );