Brak opisu

class-base.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Base form template.
  4. *
  5. * @since 1.0.0
  6. */
  7. abstract class WPForms_Template {
  8. /**
  9. * Full name of the template, eg "Contact Form".
  10. *
  11. * @since 1.0.0
  12. *
  13. * @var string
  14. */
  15. public $name;
  16. /**
  17. * Slug of the template, eg "contact-form" - no spaces.
  18. *
  19. * @since 1.0.0
  20. *
  21. * @var string
  22. */
  23. public $slug;
  24. /**
  25. * Source of the template.
  26. *
  27. * @since 1.6.8
  28. *
  29. * @var array
  30. */
  31. public $source;
  32. /**
  33. * Categories array.
  34. *
  35. * @since 1.6.8
  36. *
  37. * @var array
  38. */
  39. public $categories;
  40. /**
  41. * Short description the template.
  42. *
  43. * @since 1.0.0
  44. *
  45. * @var string
  46. */
  47. public $description = '';
  48. /**
  49. * Short description of the fields included with the template.
  50. *
  51. * @since 1.0.0
  52. *
  53. * @var string
  54. */
  55. public $includes = '';
  56. /**
  57. * URL of the icon to display in the admin area.
  58. *
  59. * @since 1.0.0
  60. *
  61. * @var string
  62. */
  63. public $icon = '';
  64. /**
  65. * Array of data that is assigned to the post_content on form creation.
  66. *
  67. * @since 1.0.0
  68. *
  69. * @var array
  70. */
  71. public $data;
  72. /**
  73. * Priority to show in the list of available templates.
  74. *
  75. * @since 1.0.0
  76. *
  77. * @var int
  78. */
  79. public $priority = 20;
  80. /**
  81. * Core or additional template.
  82. *
  83. * @since 1.4.0
  84. *
  85. * @var bool
  86. */
  87. public $core = false;
  88. /**
  89. * Modal message to display when the template is applied.
  90. *
  91. * @since 1.0.0
  92. *
  93. * @var array
  94. */
  95. public $modal = '';
  96. /**
  97. * Primary class constructor.
  98. *
  99. * @since 1.0.0
  100. */
  101. public function __construct() {
  102. // Bootstrap.
  103. $this->init();
  104. $type = $this->core ? '_core' : '';
  105. add_filter( "wpforms_form_templates{$type}", [ $this, 'template_details' ], $this->priority );
  106. add_filter( 'wpforms_create_form_args', [ $this, 'template_data' ], 10, 2 );
  107. add_filter( 'wpforms_save_form_args', [ $this, 'template_replace' ], 10, 3 );
  108. add_filter( 'wpforms_builder_template_active', [ $this, 'template_active' ], 10, 2 );
  109. }
  110. /**
  111. * Let's get started.
  112. *
  113. * @since 1.0.0
  114. */
  115. public function init() {}
  116. /**
  117. * Add basic template details to the Add New Form admin screen.
  118. *
  119. * @since 1.0.0
  120. *
  121. * @param array $templates Templates array.
  122. *
  123. * @return array
  124. */
  125. public function template_details( $templates ) {
  126. $templates[] = [
  127. 'name' => $this->name,
  128. 'slug' => $this->slug,
  129. 'source' => $this->source,
  130. 'categories' => $this->categories,
  131. 'description' => $this->description,
  132. 'includes' => $this->includes,
  133. 'icon' => $this->icon,
  134. 'plugin_dir' => $this->get_plugin_dir(),
  135. ];
  136. return $templates;
  137. }
  138. /**
  139. * Get the directory name of the plugin in which current template resides.
  140. *
  141. * @since 1.6.9
  142. *
  143. * @return string
  144. */
  145. private function get_plugin_dir() {
  146. $reflection = new \ReflectionClass( $this );
  147. $template_file_path = wp_normalize_path( $reflection->getFileName() );
  148. // Cutting out the WP_PLUGIN_DIR from the beginning of the template file path.
  149. $template_file_path = preg_replace( '{^' . wp_slash( wp_normalize_path( WP_PLUGIN_DIR ) ) . '}', '', $template_file_path );
  150. $template_file_chunks = explode( '/', $template_file_path );
  151. return $template_file_chunks[1];
  152. }
  153. /**
  154. * Add template data when form is created.
  155. *
  156. * @since 1.0.0
  157. *
  158. * @param array $args Create form arguments.
  159. * @param array $data Template data.
  160. *
  161. * @return array
  162. */
  163. public function template_data( $args, $data ) {
  164. if ( ! empty( $data ) && ! empty( $data['template'] ) ) {
  165. if ( $data['template'] === $this->slug ) {
  166. // Enable Notifications by default.
  167. $this->data['settings']['notification_enable'] = isset( $this->data['settings']['notification_enable'] )
  168. ? $this->data['settings']['notification_enable']
  169. : 1;
  170. $args['post_content'] = wpforms_encode( $this->data );
  171. }
  172. }
  173. return $args;
  174. }
  175. /**
  176. * Replace template on post update if triggered.
  177. *
  178. * @since 1.0.0
  179. *
  180. * @param array $form Form post data.
  181. * @param array $data Form data.
  182. * @param array $args Update form arguments.
  183. *
  184. * @return array
  185. */
  186. public function template_replace( $form, $data, $args ) {
  187. if ( ! empty( $args['template'] ) ) {
  188. if ( $args['template'] === $this->slug ) {
  189. $new = $this->data;
  190. $new['settings'] = ! empty( $form['post_content']['settings'] ) ? $form['post_content']['settings'] : [];
  191. $form['post_content'] = wpforms_encode( $new );
  192. }
  193. }
  194. return $form;
  195. }
  196. /**
  197. * Pass information about the active template back to the builder.
  198. *
  199. * @since 1.0.0
  200. *
  201. * @param array $details Details.
  202. * @param object $form Form data.
  203. *
  204. * @return array|void
  205. */
  206. public function template_active( $details, $form ) {
  207. if ( empty( $form ) ) {
  208. return;
  209. }
  210. $form_data = wpforms_decode( $form->post_content );
  211. if ( empty( $this->modal ) || empty( $form_data['meta']['template'] ) || $this->slug !== $form_data['meta']['template'] ) {
  212. return $details;
  213. } else {
  214. $display = $this->template_modal_conditional( $form_data );
  215. }
  216. return [
  217. 'name' => $this->name,
  218. 'slug' => $this->slug,
  219. 'description' => $this->description,
  220. 'includes' => $this->includes,
  221. 'icon' => $this->icon,
  222. 'modal' => $this->modal,
  223. 'modal_display' => $display,
  224. ];
  225. }
  226. /**
  227. * Conditional to determine if the template informational modal screens
  228. * should display.
  229. *
  230. * @since 1.0.0
  231. *
  232. * @param array $form_data Form data and settings.
  233. *
  234. * @return bool
  235. */
  236. public function template_modal_conditional( $form_data ) {
  237. return false;
  238. }
  239. }