| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Template Manager Class |
| 5 |
* |
| 6 |
* @since 1.1.0 |
| 7 |
*/ |
| 8 |
class WeForms_Template_Manager { |
| 9 |
|
| 10 |
/** |
| 11 |
* The templates |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $templates = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Get all the registered fields |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
public function get_templates() { |
| 23 |
|
| 24 |
if ( ! empty( $this->templates ) ) { |
| 25 |
return $this->templates; |
| 26 |
} |
| 27 |
|
| 28 |
$this->register_templates(); |
| 29 |
|
| 30 |
return $this->templates; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get all the templates |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function register_templates() { |
| 39 |
|
| 40 |
require_once WEFORMS_INCLUDES . '/templates/class-abstract-template.php'; |
| 41 |
|
| 42 |
require_once WEFORMS_INCLUDES . '/templates/class-template-blank.php'; |
| 43 |
require_once WEFORMS_INCLUDES . '/templates/class-template-contact.php'; |
| 44 |
require_once WEFORMS_INCLUDES . '/templates/class-template-event-registration.php'; |
| 45 |
require_once WEFORMS_INCLUDES . '/templates/class-template-support.php'; |
| 46 |
|
| 47 |
$templates = array( |
| 48 |
'blank' => new WeForms_Template_Blank(), |
| 49 |
'contact' => new WeForms_Template_Contact(), |
| 50 |
'event_registration' => new WeForms_Template_Event_Registration(), |
| 51 |
'support' => new WeForms_Template_Support() |
| 52 |
); |
| 53 |
|
| 54 |
$this->templates = apply_filters( 'weforms_get_templates', $templates ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Check if a template exists |
| 59 |
* |
| 60 |
* @param string $name |
| 61 |
* |
| 62 |
* @return boolean |
| 63 |
*/ |
| 64 |
public function exists( $name ) { |
| 65 |
if ( array_key_exists( $name, $this->get_templates() ) ) { |
| 66 |
return $this->templates[ $name ]; |
| 67 |
} |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Create a form from a template |
| 74 |
* |
| 75 |
* @param string $name |
| 76 |
* |
| 77 |
* @return integer |
| 78 |
*/ |
| 79 |
public function create( $name ) { |
| 80 |
if ( ! $template = $this->exists( $name ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$form_id = weforms()->form->create( $template->get_title(), $template->get_form_fields() ); |
| 85 |
|
| 86 |
if ( is_wp_error( $form_id ) ) { |
| 87 |
return $form_id; |
| 88 |
} |
| 89 |
|
| 90 |
$meta_updates = array( |
| 91 |
'wpuf_form_settings' => $template->get_form_settings(), |
| 92 |
'notifications' => $template->get_form_notifications(), |
| 93 |
'integrations' => array() |
| 94 |
); |
| 95 |
|
| 96 |
foreach ($meta_updates as $meta_key => $meta_value) { |
| 97 |
update_post_meta( $form_id, $meta_key, $meta_value ); |
| 98 |
} |
| 99 |
|
| 100 |
return $form_id; |
| 101 |
} |
| 102 |
} |
| 103 |
|