| 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( 'admin_init', array( $this, 'run_migrations' ) ); |
| 28 |
add_action( 'admin_init', array( $this, 'listen' ) ); |
| 29 |
add_action( 'admin_print_styles', array( $this, 'assets' ) ); |
| 30 |
add_action( 'admin_head', array( $this, 'add_screen_options' ) ); |
| 31 |
add_action( 'hf_admin_action_create_form', array( $this, 'process_create_form' ) ); |
| 32 |
add_action( 'hf_admin_action_save_form', array( $this, 'process_save_form' ) ); |
| 33 |
add_action( 'hf_admin_action_bulk_delete_submissions', array( $this, 'process_bulk_delete_submissions' ) ); |
| 34 |
|
| 35 |
add_action( 'hf_admin_output_form_tab_fields', array( $this, 'tab_fields' ) ); |
| 36 |
add_action( 'hf_admin_output_form_tab_messages', array( $this, 'tab_messages' ) ); |
| 37 |
add_action( 'hf_admin_output_form_tab_settings', array( $this, 'tab_settings' ) ); |
| 38 |
add_action( 'hf_admin_output_form_tab_actions', array( $this, 'tab_actions' ) ); |
| 39 |
add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_list' ) ); |
| 40 |
add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_detail' ) ); |
| 41 |
add_action('enqueue_block_editor_assets', array($this, 'enqueue_gutenberg_assets')); |
| 42 |
} |
| 43 |
|
| 44 |
public function enqueue_gutenberg_assets() |
| 45 |
{ |
| 46 |
wp_enqueue_script('html-forms-block', plugins_url( 'assets/js/gutenberg-block.js', $this->plugin_file ), array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components')); |
| 47 |
$forms = hf_get_forms(); |
| 48 |
$data = array(); |
| 49 |
foreach ($forms as $form) { |
| 50 |
$data[] = array( |
| 51 |
'title' => $form->title, |
| 52 |
'slug' => $form->slug, |
| 53 |
'id' => $form->ID |
| 54 |
); |
| 55 |
} |
| 56 |
wp_localize_script('html-forms-block', 'html_forms', $data ); |
| 57 |
} |
| 58 |
|
| 59 |
public function register_settings() { |
| 60 |
// register settings |
| 61 |
register_setting( 'hf_settings', 'hf_settings', array( $this, 'sanitize_settings' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
public function run_migrations() { |
| 65 |
$version_from = get_option( 'hf_version', '0.0' ); |
| 66 |
$version_to = HTML_FORMS_VERSION; |
| 67 |
|
| 68 |
if( version_compare( $version_from, $version_to, '>=' ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$migrations = new Migrations( $version_from, $version_to, dirname( $this->plugin_file ) . '/migrations' ); |
| 73 |
$migrations->run(); |
| 74 |
update_option( 'hf_version', HTML_FORMS_VERSION ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param array $dirty |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
public function sanitize_settings( $dirty ) { |
| 82 |
return $dirty; |
| 83 |
} |
| 84 |
|
| 85 |
public function listen() { |
| 86 |
$request = array_merge( $_GET, $_POST ); |
| 87 |
if( empty( $request['_hf_admin_action'] ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// do nothing if logged in user is not of role administrator |
| 92 |
if( ! current_user_can( 'edit_forms' ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$action = (string) $request['_hf_admin_action']; |
| 97 |
|
| 98 |
/** |
| 99 |
* Allows you to hook into requests containing `_hf_admin_action` => action name. |
| 100 |
* |
| 101 |
* The dynamic portion of the hook name, `$action`, refers to the action name. |
| 102 |
* |
| 103 |
* By the time this hook is fired, the user is already authorized. After processing all the registered hooks, |
| 104 |
* the request is redirected back to the referring URL. |
| 105 |
* |
| 106 |
* @since 3.0 |
| 107 |
*/ |
| 108 |
do_action( 'hf_admin_action_' . $action ); |
| 109 |
|
| 110 |
// redirect back to where we came from |
| 111 |
$redirect_url = ! empty( $_REQUEST['_redirect_to'] ) ? $_REQUEST['_redirect_to'] : remove_query_arg( '_hf_admin_action' ); |
| 112 |
wp_safe_redirect( $redirect_url ); |
| 113 |
exit; |
| 114 |
} |
| 115 |
|
| 116 |
public function assets() { |
| 117 |
if( empty( $_GET['page'] ) || strpos( $_GET['page'], 'html-forms' ) !== 0 ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 122 |
|
| 123 |
wp_enqueue_style( 'html-forms-admin', plugins_url( 'assets/css/admin'. $suffix .'.css', $this->plugin_file ), array(), HTML_FORMS_VERSION ); |
| 124 |
wp_enqueue_script( 'html-forms-admin', plugins_url( 'assets/js/admin'. $suffix .'.js', $this->plugin_file ), array(), HTML_FORMS_VERSION, true ); |
| 125 |
wp_localize_script( 'html-forms-admin', 'hf_options', array( |
| 126 |
'page' => $_GET['page'], |
| 127 |
'view' => empty( $_GET['view'] ) ? '' : $_GET['view'], |
| 128 |
'form_id' => empty( $_GET['form_id'] ) ? 0 : (int) $_GET['form_id'], |
| 129 |
)); |
| 130 |
} |
| 131 |
|
| 132 |
public function menu() { |
| 133 |
$capability = 'edit_forms'; |
| 134 |
$svg_icon = '<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="256.000000pt" height="256.000000pt" viewBox="0 0 256.000000 256.000000" preserveAspectRatio="xMidYMid meet"><g transform="translate(0.000000,256.000000) scale(0.100000,-0.100000)" |
| 135 |
fill="#000000" stroke="none"><path d="M0 1280 l0 -1280 1280 0 1280 0 0 1280 0 1280 -1280 0 -1280 0 0 -1280z m2031 593 c8 -8 9 -34 4 -78 -6 -56 -9 -65 -23 -60 -43 16 -98 15 -132 -2 -50 -26 -72 -72 -78 -159 l-5 -74 92 0 91 0 0 -70 0 -70 -90 0 -90 0 0 -345 0 -345 -90 0 -90 0 0 345 0 345 -55 0 -55 0 0 70 0 70 55 0 55 0 0 38 c0 63 20 153 45 202 54 105 141 152 273 147 45 -2 87 -8 93 -14z m-1291 -288 l0 -235 230 0 230 0 0 235 0 235 90 0 90 0 0 -575 0 -575 -90 0 -90 0 0 260 0 260 -230 0 -230 0 0 -260 0 -260 -90 0 -90 0 0 575 0 575 90 0 90 0 0 -235z"/></g></svg>'; |
| 136 |
add_menu_page( 'HTML Forms', 'HTML Forms', $capability, 'html-forms', array( $this, 'page_overview' ), 'data:image/svg+xml;base64,' . base64_encode( $svg_icon ), '99.88491' ); |
| 137 |
add_submenu_page( 'html-forms', __( 'Forms', 'html-forms' ), __( 'All Forms', 'html-forms' ), $capability, 'html-forms', array( $this, 'page_overview' ) ); |
| 138 |
add_submenu_page( 'html-forms', __( 'Add new form', 'html-forms' ), __( 'Add New', 'html-forms' ), $capability, 'html-forms-add-form', array( $this, 'page_new_form' ) ); |
| 139 |
add_submenu_page( 'html-forms', __( 'Settings', 'html-forms' ), __( 'Settings', 'html-forms' ), $capability, 'html-forms-settings', array( $this, 'page_settings' ) ); |
| 140 |
|
| 141 |
// if( ! defined( 'HF_PREMIUM_VERSION' ) ) { |
| 142 |
// add_submenu_page( 'html-forms', 'Premium', '<span style="color: #ea6ea6;">Premium</span>', $capability, 'html-forms-premium', array( $this, 'page_premium' ) ); |
| 143 |
// } |
| 144 |
} |
| 145 |
|
| 146 |
public function add_screen_options() { |
| 147 |
// only run on the submissions overview page (not detail) |
| 148 |
if( empty( $_GET['page'] ) || $_GET['page'] !== 'html-forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit' || empty( $_GET['form_id'] ) || ! empty( $_GET['submission_id'] ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// don't run if form does not have submissions enabled |
| 153 |
$form = hf_get_form( $_GET['form_id'] ); |
| 154 |
if( ! $form->settings['save_submissions'] ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
// tell screen options to show columns option |
| 159 |
$submissions = hf_get_form_submissions( $_GET['form_id'] ); |
| 160 |
$columns = $this->get_submission_columns( $submissions ); |
| 161 |
add_filter( 'manage_toplevel_page_html-forms_columns', function( $unused ) use( $columns ) { |
| 162 |
return $columns; |
| 163 |
}); |
| 164 |
add_screen_option( 'layout_columns' ); |
| 165 |
} |
| 166 |
|
| 167 |
public function page_overview() { |
| 168 |
if( ! empty( $_GET['view'] ) && $_GET['view'] === 'edit' ) { |
| 169 |
$this->page_edit_form(); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$settings = hf_get_settings(); |
| 174 |
|
| 175 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 176 |
$table = new Table( $settings ); |
| 177 |
|
| 178 |
require dirname( $this->plugin_file ) . '/views/page-overview.php'; |
| 179 |
} |
| 180 |
|
| 181 |
public function page_new_form() { |
| 182 |
require dirname( $this->plugin_file ) . '/views/page-add-form.php'; |
| 183 |
} |
| 184 |
|
| 185 |
public function page_settings() { |
| 186 |
$settings = hf_get_settings(); |
| 187 |
require dirname( $this->plugin_file ) . '/views/page-global-settings.php'; |
| 188 |
} |
| 189 |
|
| 190 |
public function page_premium() { |
| 191 |
require dirname( $this->plugin_file ) . '/views/page-premium.php'; |
| 192 |
} |
| 193 |
|
| 194 |
public function page_edit_form() { |
| 195 |
$active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'fields'; |
| 196 |
$form_id = (int) $_GET['form_id']; |
| 197 |
$form = hf_get_form( $form_id ); |
| 198 |
$settings = hf_get_settings(); |
| 199 |
require dirname( $this->plugin_file ) . '/views/page-edit-form.php'; |
| 200 |
} |
| 201 |
|
| 202 |
public function tab_fields( Form $form ) { |
| 203 |
$form_preview_url = add_query_arg( array( |
| 204 |
'hf_preview_form' => $form->ID, |
| 205 |
), site_url( '/', 'admin' ) ); |
| 206 |
require dirname( $this->plugin_file ) . '/views/tab-fields.php'; |
| 207 |
} |
| 208 |
|
| 209 |
public function tab_messages( Form $form ) { |
| 210 |
require dirname( $this->plugin_file ) . '/views/tab-messages.php'; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
public function tab_settings( Form $form ) { |
| 215 |
require dirname( $this->plugin_file ) . '/views/tab-settings.php'; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
public function tab_actions( Form $form ) { |
| 220 |
require dirname( $this->plugin_file ) . '/views/tab-actions.php'; |
| 221 |
} |
| 222 |
|
| 223 |
public function get_submission_columns( array $submissions ) { |
| 224 |
$columns = array(); |
| 225 |
foreach( $submissions as $s ) { |
| 226 |
if( ! is_array( $s->data ) ) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
foreach( $s->data as $field => $value ) { |
| 231 |
if( ! isset( $columns[$field] ) ) { |
| 232 |
$columns[$field] = esc_html( ucfirst( strtolower( str_replace( '_', ' ', $field ) ) ) ); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
return $columns; |
| 237 |
} |
| 238 |
|
| 239 |
public function tab_submissions_list( Form $form ) { |
| 240 |
if( ! empty( $_GET['submission_id'] ) ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
$submissions = hf_get_form_submissions( $form->ID ); |
| 245 |
$columns = $this->get_submission_columns( $submissions ); |
| 246 |
$hidden_columns = get_hidden_columns( get_current_screen() ); |
| 247 |
|
| 248 |
require dirname( $this->plugin_file ) . '/views/tab-submissions-list.php'; |
| 249 |
} |
| 250 |
|
| 251 |
public function tab_submissions_detail( Form $form ) { |
| 252 |
if( empty( $_GET['submission_id'] ) ) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
$submission = hf_get_form_submission( (int) $_GET['submission_id'] ); |
| 257 |
require dirname( $this->plugin_file ) . '/views/tab-submissions-detail.php'; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
public function process_create_form() { |
| 262 |
// Fix for MultiSite stripping KSES for roles other than administrator |
| 263 |
remove_all_filters( 'content_save_pre' ); |
| 264 |
|
| 265 |
$data = $_POST['form']; |
| 266 |
$form_title = sanitize_text_field( $data['title'] ); |
| 267 |
$form_id = wp_insert_post( |
| 268 |
array( |
| 269 |
'post_type' => 'html-form', |
| 270 |
'post_status' => 'publish', |
| 271 |
'post_title' => $form_title, |
| 272 |
'post_content' => $this->get_default_form_content(), |
| 273 |
) |
| 274 |
); |
| 275 |
|
| 276 |
wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id )); |
| 277 |
exit; |
| 278 |
} |
| 279 |
|
| 280 |
public function process_save_form() { |
| 281 |
$form_id = (int) $_POST['form_id']; |
| 282 |
$form = hf_get_form( $form_id ); |
| 283 |
$data = $_POST['form']; |
| 284 |
|
| 285 |
// Fix for MultiSite stripping KSES for roles other than administrator |
| 286 |
remove_all_filters( 'content_save_pre' ); |
| 287 |
|
| 288 |
// strip <form> tag from markup |
| 289 |
$data['markup'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['markup'] ); |
| 290 |
|
| 291 |
$form_id = wp_insert_post( array( |
| 292 |
'ID' => $form_id, |
| 293 |
'post_type' => 'html-form', |
| 294 |
'post_status' => 'publish', |
| 295 |
'post_title' => sanitize_text_field( $data['title'] ), |
| 296 |
'post_content' => $data['markup'], |
| 297 |
'post_name' => sanitize_title_with_dashes( $data['slug'] ), |
| 298 |
) ); |
| 299 |
|
| 300 |
if( ! empty( $data['settings'] ) ) { |
| 301 |
update_post_meta( $form_id, '_hf_settings', $data['settings'] ); |
| 302 |
} |
| 303 |
|
| 304 |
// save form messages in individual meta keys |
| 305 |
foreach( $data['messages'] as $key => $message ) { |
| 306 |
update_post_meta( $form_id, 'hf_message_' . $key, $message ); |
| 307 |
} |
| 308 |
|
| 309 |
$redirect_url_args = array( 'form_id' => $form_id, 'saved' => 1 ); |
| 310 |
$redirect_url = add_query_arg( $redirect_url_args, admin_url ('admin.php?page=html-forms&view=edit' ) ); |
| 311 |
wp_safe_redirect( $redirect_url ); |
| 312 |
exit; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Get URL for a tab on the current page. |
| 317 |
* |
| 318 |
* @since 3.0 |
| 319 |
* @internal |
| 320 |
* @param $tab |
| 321 |
* @return string |
| 322 |
*/ |
| 323 |
public function get_tab_url( $tab ) { |
| 324 |
return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* @return array |
| 329 |
*/ |
| 330 |
public function get_available_form_actions() { |
| 331 |
$actions = array(); |
| 332 |
|
| 333 |
/** |
| 334 |
* Filters the available form actions |
| 335 |
* |
| 336 |
* @param array $actions |
| 337 |
*/ |
| 338 |
$actions = apply_filters( 'hf_available_form_actions', $actions ); |
| 339 |
|
| 340 |
return $actions; |
| 341 |
} |
| 342 |
|
| 343 |
public function process_bulk_delete_submissions() { |
| 344 |
global $wpdb; |
| 345 |
|
| 346 |
if( empty( $_POST['id'] ) ) { |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
$ids = $_POST['id']; |
| 351 |
$table = $wpdb->prefix .'hf_submissions'; |
| 352 |
$ids = join( ',', array_map( 'esc_sql', $ids ) ); |
| 353 |
$wpdb->query( sprintf( "DELETE FROM {$table} WHERE id IN( %s );", $ids ) ); |
| 354 |
$wpdb->query( sprintf( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( %s ) AND meta_key LIKE '_hf_%%';", $ids ) ); |
| 355 |
} |
| 356 |
|
| 357 |
private function get_default_form_content() { |
| 358 |
$html = ''; |
| 359 |
$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; |
| 360 |
$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; |
| 361 |
$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; |
| 362 |
$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; |
| 363 |
$html .= sprintf( "<p>\n\t<input type=\"submit\" value=\"%s\" />\n</p>", __( 'Send', 'html-forms' ) ); |
| 364 |
return $html; |
| 365 |
} |
| 366 |
|
| 367 |
} |
| 368 |
|