class-base.php
9 years ago
class-blank.php
9 years ago
class-contact.php
9 years ago
class-suggestion.php
9 years ago
class-base.php
206 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base form template. |
| 4 | * |
| 5 | * @package WPForms |
| 6 | * @author WPForms |
| 7 | * @since 1.0.0 |
| 8 | * @license GPL-2.0+ |
| 9 | * @copyright Copyright (c) 2016, WPForms LLC |
| 10 | */ |
| 11 | abstract class WPForms_Template { |
| 12 | |
| 13 | /** |
| 14 | * Full name of the template, eg "Contact Form". |
| 15 | * |
| 16 | * @since 1.0.0 |
| 17 | * @var string |
| 18 | */ |
| 19 | public $name; |
| 20 | |
| 21 | /** |
| 22 | * Slug of the template, eg "contact-form" - no spaces. |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | * @var string |
| 26 | */ |
| 27 | public $slug; |
| 28 | |
| 29 | /** |
| 30 | * Short description the template. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @var string |
| 34 | */ |
| 35 | public $description; |
| 36 | |
| 37 | /** |
| 38 | * Short description of the fields included with the template. |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | * @var string |
| 42 | */ |
| 43 | public $includes; |
| 44 | |
| 45 | /** |
| 46 | * URL of the icon to display in the admin area. |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | * @var string |
| 50 | */ |
| 51 | public $icon; |
| 52 | |
| 53 | /** |
| 54 | * Array of data that is assigned to the post_content on form creation. |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | * @var array |
| 58 | */ |
| 59 | public $data; |
| 60 | |
| 61 | /** |
| 62 | * Priority to show in the list of available templates. |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | * @var int |
| 66 | */ |
| 67 | public $priority = 20; |
| 68 | |
| 69 | /** |
| 70 | * Modal message to display when the template is applied. |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | * @var array |
| 74 | */ |
| 75 | public $modal = ''; |
| 76 | |
| 77 | /** |
| 78 | * Primary class constructor. |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | */ |
| 82 | public function __construct() { |
| 83 | |
| 84 | // Bootstrap |
| 85 | $this->init(); |
| 86 | |
| 87 | add_filter( 'wpforms_form_templates', array( $this , 'template_details' ), $this->priority ); |
| 88 | add_filter( 'wpforms_create_form_args', array( $this, 'template_data' ), 10, 2 ); |
| 89 | add_filter( 'wpforms_save_form_args', array( $this, 'template_replace' ), 10, 4 ); |
| 90 | add_filter( 'wpforms_builder_template_active', array( $this, 'template_active' ), 10, 2 ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Let's get started. |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | public function init() { |
| 99 | |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Add basic template details to the Add New Form admin screen. |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | * @param array $templates |
| 107 | * @return array |
| 108 | */ |
| 109 | function template_details( $templates ) { |
| 110 | $template = array( |
| 111 | 'name' => $this->name, |
| 112 | 'slug' => $this->slug, |
| 113 | 'description' => $this->description, |
| 114 | 'includes' => $this->includes, |
| 115 | 'icon' => $this->icon, |
| 116 | ); |
| 117 | $templates[] = $template; |
| 118 | return $templates; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Add template data when form is created. |
| 123 | * |
| 124 | * @since 1.0.0 |
| 125 | * @param array $args |
| 126 | * @param array $data |
| 127 | * @return array |
| 128 | */ |
| 129 | function template_data( $args, $data ) { |
| 130 | |
| 131 | if ( !empty( $data ) && !empty( $data['template'] ) ) { |
| 132 | if ( $data['template'] == $this->slug ) { |
| 133 | $args['post_content'] = wp_slash( json_encode( $this->data ) ); |
| 134 | } |
| 135 | } |
| 136 | return $args; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Replace template on post update if triggered. |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | * @param array $form |
| 144 | * @param array $data |
| 145 | * @param array $args |
| 146 | * @return array |
| 147 | */ |
| 148 | function template_replace( $form, $data, $args ) { |
| 149 | |
| 150 | if ( !empty( $args['template'] ) ) { |
| 151 | if ( $args['template'] == $this->slug ) { |
| 152 | $new = $this->data; |
| 153 | $new['settings'] = !empty( $form['post_content']['settings'] ) ? $form['post_content']['settings'] : array(); |
| 154 | $form['post_content'] = wp_slash( json_encode( $new ) ); |
| 155 | } |
| 156 | } |
| 157 | return $form; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Pass information about the active template back to the builder. |
| 162 | * |
| 163 | * @since 1.0.0 |
| 164 | * @param array $details |
| 165 | * @param object $form |
| 166 | * @return array |
| 167 | */ |
| 168 | function template_active( $details, $form ) { |
| 169 | |
| 170 | if ( empty( $form ) ) |
| 171 | return; |
| 172 | |
| 173 | $form_data = wpforms_decode( $form->post_content ); |
| 174 | |
| 175 | if ( empty( $this->modal ) || empty( $form_data['meta']['template'] ) || $this->slug != $form_data['meta']['template'] ) { |
| 176 | return $details; |
| 177 | } else { |
| 178 | $display = $this->template_modal_conditional( $form_data ); |
| 179 | } |
| 180 | |
| 181 | $template = array( |
| 182 | 'name' => $this->name, |
| 183 | 'slug' => $this->slug, |
| 184 | 'description' => $this->description, |
| 185 | 'includes' => $this->includes, |
| 186 | 'icon' => $this->icon, |
| 187 | 'modal' => $this->modal, |
| 188 | 'modal_display' => $display, |
| 189 | ); |
| 190 | |
| 191 | return $template; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Conditional to determine if the template informational modal screens |
| 196 | * should display. |
| 197 | * |
| 198 | * @since 1.0.0 |
| 199 | * @param array $form_data |
| 200 | * @return boolean |
| 201 | */ |
| 202 | function template_modal_conditional( $form_data ) { |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | } |