| 1 |
<?php |
| 2 |
|
| 3 |
namespace HTML_Forms\Admin; |
| 4 |
|
| 5 |
use HTML_Forms\Form; |
| 6 |
use HTML_Forms\Submission; |
| 7 |
|
| 8 |
class Admin { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
private $plugin_file; |
| 14 |
|
| 15 |
/** |
| 16 |
* Admin constructor. |
| 17 |
* |
| 18 |
* @param string $plugin_file |
| 19 |
*/ |
| 20 |
public function __construct( $plugin_file ) { |
| 21 |
$this->plugin_file = $plugin_file; |
| 22 |
} |
| 23 |
|
| 24 |
public function hook() { |
| 25 |
add_action( 'admin_menu', array( $this, 'menu' ) ); |
| 26 |
add_action( 'init', array( $this, 'register_settings' ) ); |
| 27 |
add_action( 'init', array( $this, 'listen' ) ); |
| 28 |
add_action( 'admin_print_styles', array( $this, 'assets' ) ); |
| 29 |
add_action( 'hf_admin_action_create_form', array( $this, 'process_create_form' ) ); |
| 30 |
add_action( 'hf_admin_action_save_form', array( $this, 'process_save_form' ) ); |
| 31 |
add_action( 'hf_admin_action_bulk_delete_submissions', array( $this, 'process_bulk_delete_submissions' ) ); |
| 32 |
|
| 33 |
add_action( 'hf_admin_output_form_tab_fields', array( $this, 'tab_fields' ) ); |
| 34 |
add_action( 'hf_admin_output_form_tab_messages', array( $this, 'tab_messages' ) ); |
| 35 |
add_action( 'hf_admin_output_form_tab_settings', array( $this, 'tab_settings' ) ); |
| 36 |
add_action( 'hf_admin_output_form_tab_actions', array( $this, 'tab_actions' ) ); |
| 37 |
add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_list' ) ); |
| 38 |
add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_detail' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function register_settings() { |
| 44 |
// register settings |
| 45 |
register_setting( 'hf_settings', 'hf_settings', array( $this, 'sanitize_settings' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param array $dirty |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function sanitize_settings( $dirty ) { |
| 53 |
return $dirty; |
| 54 |
} |
| 55 |
|
| 56 |
public function listen() { |
| 57 |
$request = array_merge( $_GET, $_POST ); |
| 58 |
if( empty( $request['_hf_admin_action'] ) ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
// do nothing if logged in user is not of role administrator |
| 63 |
if( ! current_user_can( 'manage_options' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$action = (string) $request['_hf_admin_action']; |
| 68 |
|
| 69 |
/** |
| 70 |
* Allows you to hook into requests containing `_hf_admin_action` => action name. |
| 71 |
* |
| 72 |
* The dynamic portion of the hook name, `$action`, refers to the action name. |
| 73 |
* |
| 74 |
* By the time this hook is fired, the user is already authorized. After processing all the registered hooks, |
| 75 |
* the request is redirected back to the referring URL. |
| 76 |
* |
| 77 |
* @since 3.0 |
| 78 |
*/ |
| 79 |
do_action( 'hf_admin_action_' . $action ); |
| 80 |
|
| 81 |
// redirect back to where we came from |
| 82 |
$redirect_url = ! empty( $_REQUEST['_redirect_to'] ) ? $_REQUEST['_redirect_to'] : remove_query_arg( '_hf_admin_action' ); |
| 83 |
wp_safe_redirect( $redirect_url ); |
| 84 |
exit; |
| 85 |
} |
| 86 |
|
| 87 |
public function assets() { |
| 88 |
if( empty( $_GET['page'] ) || strpos( $_GET['page'], 'html-forms' ) !== 0 ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 93 |
|
| 94 |
wp_enqueue_style( 'hf-admin', plugins_url( 'assets/css/admin'. $suffix .'.css', $this->plugin_file ), array(), HTML_FORMS_VERSION ); |
| 95 |
wp_enqueue_script( 'hf-admin', plugins_url( 'assets/js/admin'. $suffix .'.js', $this->plugin_file ), array(), HTML_FORMS_VERSION, true ); |
| 96 |
} |
| 97 |
|
| 98 |
public function menu() { |
| 99 |
// add top menu item |
| 100 |
add_menu_page( 'HTML Forms', 'HTML Forms', 'manage_options', 'html-forms', array( $this, 'page_overview' ), plugins_url('assets/img/icon.svg', $this->plugin_file ), '99.88491' ); |
| 101 |
add_submenu_page( 'html-forms', 'Forms', 'All Forms', 'manage_options', 'html-forms', array( $this, 'page_overview' ) ); |
| 102 |
add_submenu_page( 'html-forms', 'Add new form', 'Add New', 'manage_options', 'html-forms-add-form', array( $this, 'page_new_form' ) ); |
| 103 |
add_submenu_page( 'html-forms', 'Settings', 'Settings', 'manage_options', 'html-forms-settings', array( $this, 'page_settings' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
public function page_overview() { |
| 107 |
if( ! empty( $_GET['view'] ) && $_GET['view'] === 'edit' ) { |
| 108 |
$this->page_edit_form(); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$table = new Table(); |
| 113 |
$table->prepare_items(); |
| 114 |
|
| 115 |
require __DIR__ . '/views/overview.php'; |
| 116 |
} |
| 117 |
|
| 118 |
public function page_new_form() { |
| 119 |
require __DIR__ . '/views/add-form.php'; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public function page_settings() { |
| 124 |
$settings = hf_get_settings(); |
| 125 |
require __DIR__ . '/views/global-settings.php'; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
public function page_edit_form() { |
| 130 |
$active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'fields'; |
| 131 |
$form_id = (int) $_GET['form_id']; |
| 132 |
$form = hf_get_form( $form_id ); |
| 133 |
|
| 134 |
require __DIR__ . '/views/edit-form.php'; |
| 135 |
} |
| 136 |
|
| 137 |
public function tab_fields( Form $form ) { |
| 138 |
require __DIR__ . '/views/tab-fields.php'; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
public function tab_messages( Form $form ) { |
| 143 |
require __DIR__ . '/views/tab-messages.php'; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
public function tab_settings( Form $form ) { |
| 148 |
require __DIR__ . '/views/tab-settings.php'; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
public function tab_actions( Form $form ) { |
| 153 |
require __DIR__ . '/views/tab-actions.php'; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
public function tab_submissions_list( Form $form ) { |
| 158 |
if( ! empty( $_GET['submission_id'] ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
$submissions = hf_get_form_submissions( $form->ID ); |
| 163 |
|
| 164 |
// create array of columns for submissions tab |
| 165 |
$columns = array(); |
| 166 |
foreach( $submissions as $s ) { |
| 167 |
foreach( $s->data as $field => $value ) { |
| 168 |
if (!array_key_exists($field, $columns)) { |
| 169 |
$columns[$field] = true; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
$columns = array_keys( $columns ); |
| 174 |
|
| 175 |
require __DIR__ . '/views/tab-submissions-list.php'; |
| 176 |
} |
| 177 |
|
| 178 |
public function tab_submissions_detail( Form $form ) { |
| 179 |
if( empty( $_GET['submission_id'] ) ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
$submission = hf_get_form_submission( (int) $_GET['submission_id'] ); |
| 184 |
require __DIR__ . '/views/tab-submissions-detail.php'; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
public function process_create_form() { |
| 189 |
// Fix for MultiSite stripping KSES for roles other than administrator |
| 190 |
remove_all_filters( 'content_save_pre' ); |
| 191 |
|
| 192 |
$data = $_POST['form']; |
| 193 |
$form_title = sanitize_text_field( $data['title'] ); |
| 194 |
$form_id = wp_insert_post( |
| 195 |
array( |
| 196 |
'post_type' => 'html-form', |
| 197 |
'post_status' => 'publish', |
| 198 |
'post_title' => $form_title, |
| 199 |
'post_content' => $this->get_default_form_content(), |
| 200 |
) |
| 201 |
); |
| 202 |
|
| 203 |
wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id )); |
| 204 |
exit; |
| 205 |
} |
| 206 |
|
| 207 |
public function process_save_form() { |
| 208 |
$form_id = (int) $_POST['form_id']; |
| 209 |
$form = hf_get_form( $form_id ); |
| 210 |
$data = $_POST['form']; |
| 211 |
|
| 212 |
// Fix for MultiSite stripping KSES for roles other than administrator |
| 213 |
remove_all_filters( 'content_save_pre' ); |
| 214 |
|
| 215 |
// strip <form> tag from markup |
| 216 |
$data['markup'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['markup'] ); |
| 217 |
|
| 218 |
$form_id = wp_insert_post( array( |
| 219 |
'ID' => $form_id, |
| 220 |
'post_type' => 'html-form', |
| 221 |
'post_status' => 'publish', |
| 222 |
'post_title' => sanitize_text_field( $data['title'] ), |
| 223 |
'post_content' => $data['markup'], |
| 224 |
'post_name' => sanitize_title_with_dashes( $data['slug'] ), |
| 225 |
) ); |
| 226 |
|
| 227 |
if( ! empty( $data['settings'] ) ) { |
| 228 |
update_post_meta( $form_id, '_hf_settings', $data['settings'] ); |
| 229 |
} |
| 230 |
|
| 231 |
// save form messages in individual meta keys |
| 232 |
foreach( $data['messages'] as $key => $message ) { |
| 233 |
update_post_meta( $form_id, 'hf_message_' . $key, $message ); |
| 234 |
} |
| 235 |
|
| 236 |
$redirect_url = add_query_arg( array( 'form_id' => $form_id, 'saved' => 1 ), admin_url ('admin.php?page=html-forms&view=edit' ) ); |
| 237 |
wp_safe_redirect( $redirect_url ); |
| 238 |
exit; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get URL for a tab on the current page. |
| 243 |
* |
| 244 |
* @since 3.0 |
| 245 |
* @internal |
| 246 |
* @param $tab |
| 247 |
* @return string |
| 248 |
*/ |
| 249 |
public function get_tab_url( $tab ) { |
| 250 |
return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
public function get_available_form_actions() { |
| 257 |
$actions = array(); |
| 258 |
|
| 259 |
/** |
| 260 |
* Filters the available form actions |
| 261 |
* |
| 262 |
* @param array $actions |
| 263 |
*/ |
| 264 |
$actions = apply_filters( 'hf_available_form_actions', $actions ); |
| 265 |
|
| 266 |
return $actions; |
| 267 |
} |
| 268 |
|
| 269 |
public function process_bulk_delete_submissions() { |
| 270 |
global $wpdb; |
| 271 |
|
| 272 |
if( empty( $_POST['id'] ) ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
$table = $wpdb->prefix .'hf_submissions'; |
| 277 |
$ids = join( ',', array_map( 'esc_sql', $_POST['id'] ) ); |
| 278 |
$wpdb->query( sprintf( "DELETE FROM {$table} WHERE id IN( %s );", $ids ) ); |
| 279 |
} |
| 280 |
|
| 281 |
private function get_default_form_content() { |
| 282 |
$html = ''; |
| 283 |
$html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<input type=\"text\" name=\"NAME\" placeholder=\"%1\$s\" required />\n</p>", __( 'Your name', 'html-forms' ) ) . PHP_EOL; |
| 284 |
$html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<input type=\"email\" name=\"EMAIL\" placeholder=\"%1\$s\" required />\n</p>", __( 'Your email', 'html-forms' ) ) . PHP_EOL; |
| 285 |
$html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<input type=\"text\" name=\"SUBJECT\" placeholder=\"%1\$s\" required />\n</p>", __( 'Subject', 'html-forms' ) ) . PHP_EOL; |
| 286 |
$html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<textarea name=\"MESSAGE\" placeholder=\"%1\$s\" required></textarea>\n</p>", __( 'Message', 'html-forms' ) ). PHP_EOL; |
| 287 |
$html .= sprintf( "<p>\n\t<input type=\"submit\" value=\"%s\" />\n</p>", __( 'Send', 'html-forms' ) ); |
| 288 |
return $html; |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|