PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wpforms-lite/src/Integrations/Gutenberg/FormSelector.php

https://gitlab.com/ebrjose/comcebu
PHP | 271 lines | 175 code | 45 blank | 51 comment | 11 complexity | e5820c40891e396aa06aa0f569b6add8 MD5 | raw file
  1. <?php
  2. namespace WPForms\Integrations\Gutenberg;
  3. use WPForms\Integrations\IntegrationInterface;
  4. /**
  5. * Form Selector Gutenberg block with live preview.
  6. *
  7. * @since 1.4.8
  8. */
  9. class FormSelector implements IntegrationInterface {
  10. /**
  11. * Indicate if current integration is allowed to load.
  12. *
  13. * @since 1.4.8
  14. *
  15. * @return bool
  16. */
  17. public function allow_load() {
  18. return function_exists( 'register_block_type' );
  19. }
  20. /**
  21. * Load an integration.
  22. *
  23. * @since 1.4.8
  24. */
  25. public function load() {
  26. $this->hooks();
  27. }
  28. /**
  29. * Integration hooks.
  30. *
  31. * @since 1.4.8
  32. */
  33. protected function hooks() {
  34. add_action( 'init', [ $this, 'register_block' ] );
  35. add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
  36. }
  37. /**
  38. * Register WPForms Gutenberg block on the backend.
  39. *
  40. * @since 1.4.8
  41. */
  42. public function register_block() {
  43. $disable_css_setting = wpforms_setting( 'disable-css', '1' );
  44. $block_style = ! empty( $disable_css_setting ) ? 'wpforms-gutenberg-form-selector' : '';
  45. $attributes = [
  46. 'formId' => [
  47. 'type' => 'string',
  48. ],
  49. 'displayTitle' => [
  50. 'type' => 'boolean',
  51. ],
  52. 'displayDesc' => [
  53. 'type' => 'boolean',
  54. ],
  55. 'className' => [
  56. 'type' => 'string',
  57. ],
  58. ];
  59. register_block_type(
  60. 'wpforms/form-selector',
  61. [
  62. 'attributes' => \apply_filters( 'wpforms_gutenberg_form_selector_attributes', $attributes ),
  63. 'style' => $block_style,
  64. 'editor_style' => 'wpforms-integrations',
  65. 'render_callback' => [ $this, 'get_form_html' ],
  66. ]
  67. );
  68. }
  69. /**
  70. * Load WPForms Gutenberg block scripts.
  71. *
  72. * @since 1.4.8
  73. */
  74. public function enqueue_block_editor_assets() {
  75. $i18n = [
  76. 'title' => \esc_html__( 'WPForms', 'wpforms-lite' ),
  77. 'description' => \esc_html__( 'Select and display one of your forms.', 'wpforms-lite' ),
  78. 'form_keywords' => [
  79. \esc_html__( 'form', 'wpforms-lite' ),
  80. \esc_html__( 'contact', 'wpforms-lite' ),
  81. \esc_html__( 'survey', 'wpforms-lite' ),
  82. 'the dude',
  83. ],
  84. 'form_select' => \esc_html__( 'Select a Form', 'wpforms-lite' ),
  85. 'form_settings' => \esc_html__( 'Form Settings', 'wpforms-lite' ),
  86. 'form_selected' => \esc_html__( 'Form', 'wpforms-lite' ),
  87. 'show_title' => \esc_html__( 'Show Title', 'wpforms-lite' ),
  88. 'show_description' => \esc_html__( 'Show Description', 'wpforms-lite' ),
  89. 'panel_notice_head' => \esc_html__( 'Heads up!', 'wpforms-lite' ),
  90. 'panel_notice_text' => \esc_html__( 'Do not forget to test your form.', 'wpforms-lite' ),
  91. 'panel_notice_link' => \esc_html__( 'Check out our complete guide!', 'wpforms-lite' ),
  92. ];
  93. if ( version_compare( $GLOBALS['wp_version'], '5.1.1', '<=' ) ) {
  94. array_pop( $i18n['form_keywords'] );
  95. }
  96. $min = wpforms_get_min_suffix();
  97. wp_enqueue_style(
  98. 'wpforms-integrations',
  99. WPFORMS_PLUGIN_URL . "assets/css/admin-integrations{$min}.css",
  100. [],
  101. WPFORMS_VERSION
  102. );
  103. $disable_css_setting = (int) wpforms_setting( 'disable-css', '1' );
  104. if ( $disable_css_setting !== 3 ) {
  105. $css_file = $disable_css_setting === 2 ? 'base' : 'full';
  106. wp_register_style(
  107. 'wpforms-gutenberg-form-selector',
  108. WPFORMS_PLUGIN_URL . "assets/css/wpforms-{$css_file}{$min}.css",
  109. [ 'wp-edit-blocks', 'wpforms-integrations' ],
  110. WPFORMS_VERSION
  111. );
  112. }
  113. wp_enqueue_script(
  114. 'wpforms-gutenberg-form-selector',
  115. // The unminified version is not supported by the browser.
  116. WPFORMS_PLUGIN_URL . 'assets/js/components/admin/gutenberg/formselector.min.js',
  117. [ 'wp-blocks', 'wp-i18n', 'wp-element' ],
  118. WPFORMS_VERSION,
  119. true
  120. );
  121. $forms = wpforms()->form->get( '', [ 'order' => 'DESC' ] );
  122. $forms = ! empty( $forms ) ? $forms : [];
  123. $forms = array_map(
  124. function( $form ) {
  125. $form->post_title = htmlspecialchars_decode( $form->post_title, ENT_QUOTES );
  126. return $form;
  127. },
  128. $forms
  129. );
  130. wp_localize_script(
  131. 'wpforms-gutenberg-form-selector',
  132. 'wpforms_gutenberg_form_selector',
  133. [
  134. 'logo_url' => WPFORMS_PLUGIN_URL . 'assets/images/sullie-alt.png',
  135. 'block_preview_url' => WPFORMS_PLUGIN_URL . 'assets/images/integrations/gutenberg/block-preview.png',
  136. 'wpnonce' => \wp_create_nonce( 'wpforms-gutenberg-form-selector' ),
  137. 'forms' => $forms,
  138. 'i18n' => $i18n,
  139. ]
  140. );
  141. }
  142. /**
  143. * Get form HTML to display in a WPForms Gutenberg block.
  144. *
  145. * @param array $attr Attributes passed by WPForms Gutenberg block.
  146. *
  147. * @since 1.4.8
  148. *
  149. * @return string
  150. */
  151. public function get_form_html( $attr ) {
  152. $id = ! empty( $attr['formId'] ) ? \absint( $attr['formId'] ) : 0;
  153. if ( empty( $id ) ) {
  154. return '';
  155. }
  156. $title = ! empty( $attr['displayTitle'] ) ? true : false;
  157. $desc = ! empty( $attr['displayDesc'] ) ? true : false;
  158. // Disable form fields if called from the Gutenberg editor.
  159. if ( $this->is_gb_editor() ) {
  160. \add_filter(
  161. 'wpforms_frontend_container_class',
  162. function ( $classes ) {
  163. $classes[] = 'wpforms-gutenberg-form-selector';
  164. $classes[] = 'wpforms-container-full';
  165. return $classes;
  166. }
  167. );
  168. \add_action(
  169. 'wpforms_frontend_output',
  170. function () {
  171. echo '<fieldset disabled>';
  172. },
  173. 3
  174. );
  175. \add_action(
  176. 'wpforms_frontend_output',
  177. function () {
  178. echo '</fieldset>';
  179. },
  180. 30
  181. );
  182. }
  183. if ( ! empty( $attr['className'] ) ) {
  184. \add_filter(
  185. 'wpforms_frontend_container_class',
  186. function ( $classes ) use ( $attr ) {
  187. $cls = array_map( 'esc_attr', explode( ' ', $attr['className'] ) );
  188. return array_unique( array_merge( $classes, $cls ) );
  189. }
  190. );
  191. }
  192. \ob_start();
  193. \do_action( 'wpforms_gutenberg_block_before' );
  194. if ( $this->is_gb_editor() ) {
  195. wpforms_display(
  196. $id,
  197. apply_filters( 'wpforms_gutenberg_block_form_title', $title, $id ),
  198. apply_filters( 'wpforms_gutenberg_block_form_desc', $desc, $id )
  199. );
  200. } else {
  201. printf(
  202. '[wpforms id="%s" title="%d" description="%d"]',
  203. absint( $id ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  204. apply_filters( 'wpforms_gutenberg_block_form_title', $title, $id ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  205. apply_filters( 'wpforms_gutenberg_block_form_desc', $desc, $id ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  206. );
  207. }
  208. \do_action( 'wpforms_gutenberg_block_after' );
  209. $content = \ob_get_clean();
  210. if ( empty( $content ) ) {
  211. $content = '<div class="components-placeholder"><div class="components-placeholder__label"></div><div class="components-placeholder__fieldset">' . \esc_html__( 'The form cannot be displayed.', 'wpforms-lite' ) . '</div></div>';
  212. }
  213. return \apply_filters( 'wpforms_gutenberg_block_form_content', $content, $id );
  214. }
  215. /**
  216. * Checking if is Gutenberg REST API call.
  217. *
  218. * @since 1.5.7
  219. *
  220. * @return bool True if is Gutenberg REST API call.
  221. */
  222. public function is_gb_editor() {
  223. // TODO: Find a better way to check if is GB editor API call.
  224. return \defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore
  225. }
  226. }