DragDropBuilder
1 month ago
EmailSettings
1 year ago
Membership
1 month ago
AbstractSettingsPage.php
2 years ago
AddNewForm.php
1 year ago
AdminFooter.php
4 years ago
ExtensionsSettingsPage.php
1 year ago
FormList.php
4 months ago
Forms.php
1 year ago
FuseWP.php
3 years ago
GeneralSettings.php
9 months ago
IDUserColumn.php
5 years ago
LicenseUpgrader.php
3 years ago
MailOptin.php
3 years ago
MemberDirectories.php
1 year ago
MembersDirectoryList.php
4 years ago
ToolsSettingsPage.php
4 years ago
index.php
3 years ago
FormList.php
442 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Classes\ExtensionManager; |
| 7 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 8 | use ProfilePress\Core\Themes\Shortcode\ThemesRepository as ShortcodeThemesRepository; |
| 9 | use ProfilePress\Core\Themes\DragDrop\ThemesRepository as DragDropThemesRepository; |
| 10 | |
| 11 | if ( ! class_exists('WP_List_Table')) { |
| 12 | require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
| 13 | } |
| 14 | |
| 15 | class FormList extends \WP_List_Table |
| 16 | { |
| 17 | private $table; |
| 18 | |
| 19 | /** @var \wpdb */ |
| 20 | private $wpdb; |
| 21 | |
| 22 | public function __construct($wpdb) |
| 23 | { |
| 24 | $this->wpdb = $wpdb; |
| 25 | $this->table = Base::form_db_table(); |
| 26 | parent::__construct(array( |
| 27 | 'singular' => esc_html__('form', 'wp-user-avatar'), //singular name of the listed records |
| 28 | 'plural' => esc_html__('forms', 'wp-user-avatar'), //plural name of the listed records |
| 29 | 'ajax' => false //does this table support ajax? |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public function get_forms($per_page, $current_page = 1, $form_type = '') |
| 35 | { |
| 36 | $per_page = absint($per_page); |
| 37 | $current_page = absint($current_page); |
| 38 | $form_type = ! empty($form_type) ? sanitize_text_field($form_type) : FR::LOGIN_TYPE; |
| 39 | |
| 40 | $offset = ($current_page - 1) * $per_page; |
| 41 | $sql = "SELECT * FROM $this->table"; |
| 42 | $args = []; |
| 43 | |
| 44 | $sql .= " WHERE form_type = %s"; |
| 45 | $args[] = $form_type; |
| 46 | |
| 47 | $sql .= " ORDER BY date DESC"; |
| 48 | |
| 49 | $args[] = $per_page; |
| 50 | |
| 51 | $sql .= " LIMIT %d"; |
| 52 | if ($current_page > 1) { |
| 53 | $args[] = $offset; |
| 54 | $sql .= " OFFSET %d"; |
| 55 | } |
| 56 | |
| 57 | $result = $this->wpdb->get_results( |
| 58 | $this->wpdb->prepare($sql, $args), |
| 59 | 'ARRAY_A' |
| 60 | ); |
| 61 | |
| 62 | if ( ! ExtensionManager::is_premium()) { |
| 63 | $shortcode_premium_theme_classes = wp_list_pluck(ShortcodeThemesRepository::premiumThemes(), 'theme_class'); |
| 64 | $dnd_premium_theme_classes = wp_list_pluck(DragDropThemesRepository::premiumThemes(), 'theme_class'); |
| 65 | |
| 66 | $filtered = []; |
| 67 | foreach ($result as $form) { |
| 68 | |
| 69 | $builder_type = $form['builder_type']; |
| 70 | $form_type = $form['form_type']; |
| 71 | $form_id = $form['form_id']; |
| 72 | |
| 73 | $form_class = FR::get_form_class($form_id, $form_type); |
| 74 | |
| 75 | if ($builder_type == FR::SHORTCODE_BUILDER_TYPE && |
| 76 | ! in_array($form_class, $shortcode_premium_theme_classes) |
| 77 | ) { |
| 78 | $filtered[] = $form; |
| 79 | } |
| 80 | |
| 81 | if ($builder_type == FR::DRAG_DROP_BUILDER_TYPE && |
| 82 | ! in_array($form_class, $dnd_premium_theme_classes) |
| 83 | ) { |
| 84 | $filtered[] = $form; |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | $result = $filtered; |
| 90 | } |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the count of records in the database. |
| 97 | * |
| 98 | * @param string $form_type |
| 99 | * |
| 100 | * @return null|string |
| 101 | */ |
| 102 | public function record_count($form_type = '') |
| 103 | { |
| 104 | $form_type = ! empty($form_type) ? sanitize_text_field($form_type) : FR::LOGIN_TYPE; |
| 105 | |
| 106 | $sql = "SELECT COUNT(*) FROM $this->table"; |
| 107 | if ( ! empty($form_type)) { |
| 108 | $form_type = esc_sql($form_type); |
| 109 | $sql .= " WHERE form_type = '$form_type'"; |
| 110 | } |
| 111 | |
| 112 | return $this->wpdb->get_var($sql); |
| 113 | } |
| 114 | |
| 115 | public static function customize_url($form_id, $form_type, $builder_type = FR::SHORTCODE_BUILDER_TYPE) |
| 116 | { |
| 117 | $slug = $form_type == FR::MEMBERS_DIRECTORY_TYPE ? PPRESS_MEMBER_DIRECTORIES_SLUG : PPRESS_FORMS_SETTINGS_SLUG; |
| 118 | |
| 119 | $url = admin_url( |
| 120 | sprintf( |
| 121 | 'admin.php?page=%s&view=edit-shortcode-%s&id=%d', |
| 122 | $slug, |
| 123 | sanitize_text_field($form_type), |
| 124 | absint($form_id) |
| 125 | ) |
| 126 | ); |
| 127 | |
| 128 | if ($builder_type == FR::DRAG_DROP_BUILDER_TYPE) { |
| 129 | |
| 130 | $url = admin_url( |
| 131 | sprintf( |
| 132 | 'admin.php?page=%s&view=drag-drop-builder&form-type=%s&id=%d', |
| 133 | $slug, |
| 134 | sanitize_text_field($form_type), |
| 135 | absint($form_id) |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | return esc_url_raw($url); |
| 141 | } |
| 142 | |
| 143 | public static function delete_url($form_id, $form_type) |
| 144 | { |
| 145 | return esc_url_raw(admin_url( |
| 146 | sprintf( |
| 147 | 'admin.php?page=%s&action=delete&form_type=%s&id=%d&_wpnonce=%s', |
| 148 | PPRESS_FORMS_SETTINGS_SLUG, |
| 149 | sanitize_text_field($form_type), |
| 150 | absint($form_id), |
| 151 | ppress_create_nonce() |
| 152 | ) |
| 153 | )); |
| 154 | } |
| 155 | |
| 156 | public static function clone_url($form_id, $form_type) |
| 157 | { |
| 158 | return esc_url_raw(admin_url( |
| 159 | sprintf( |
| 160 | 'admin.php?page=%s&action=clone&form_type=%s&id=%d&_wpnonce=%s', |
| 161 | PPRESS_FORMS_SETTINGS_SLUG, |
| 162 | sanitize_text_field($form_type), |
| 163 | absint($form_id), |
| 164 | ppress_create_nonce() |
| 165 | ) |
| 166 | )); |
| 167 | } |
| 168 | |
| 169 | public static function preview_url($form_id, $form_type) |
| 170 | { |
| 171 | return esc_url_raw(add_query_arg(['pp_preview_form' => $form_id, 'type' => $form_type], home_url())); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Text displayed when no email optin form is available |
| 176 | */ |
| 177 | public function no_items() |
| 178 | { |
| 179 | printf( |
| 180 | esc_html__('No form is currently available. %sConsider creating one%s', 'wp-user-avatar'), |
| 181 | '<a href="' . add_query_arg('view', 'add-new-form', PPRESS_FORMS_SETTINGS_PAGE) . '">', |
| 182 | '</a>' |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Associative array of columns |
| 188 | * |
| 189 | * @return array |
| 190 | */ |
| 191 | public function get_columns() |
| 192 | { |
| 193 | $columns = array( |
| 194 | 'cb' => '<input type="checkbox" />', |
| 195 | 'title' => esc_html__('Title', 'wp-user-avatar'), |
| 196 | 'shortcode' => esc_html__('Shortcode', 'wp-user-avatar'), |
| 197 | 'builder' => esc_html__('Builder Type', 'wp-user-avatar'), |
| 198 | 'date' => esc_html__('Date', 'wp-user-avatar') |
| 199 | ); |
| 200 | |
| 201 | if ( ! class_exists('ProfilePress\Libsodium\Libsodium')) { |
| 202 | unset($columns['builder']); |
| 203 | } |
| 204 | |
| 205 | return $columns; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Render the bulk edit checkbox |
| 210 | * |
| 211 | * @param array $item |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | function column_cb($item) |
| 216 | { |
| 217 | return sprintf( |
| 218 | '<input type="checkbox" name="form_id[]" value="%s" />', $item['form_id'] |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Method for Title column |
| 224 | * |
| 225 | * @param array $item an array of DB data |
| 226 | * |
| 227 | * @return string |
| 228 | */ |
| 229 | public function column_title($item) |
| 230 | { |
| 231 | $form_id = absint($item['form_id']); |
| 232 | $form_type = sanitize_text_field($item['form_type']); |
| 233 | $builder_type = sanitize_text_field($item['builder_type']); |
| 234 | $customize_url = esc_url(self::customize_url($form_id, $form_type, $builder_type)); |
| 235 | $delete_url = esc_url(self::delete_url($form_id, $form_type)); |
| 236 | $clone_url = esc_url(self::clone_url($form_id, $form_type)); |
| 237 | $preview_url = esc_url(self::preview_url($form_id, $form_type)); |
| 238 | |
| 239 | $actions = array( |
| 240 | 'edit' => sprintf("<a href='%s'>%s</a>", $customize_url, esc_attr__('Edit', 'wp-user-avatar')), |
| 241 | 'delete' => sprintf("<a class='pp-form-delete' href='%s'>%s</a>", $delete_url, esc_attr__('Delete', 'wp-user-avatar')), |
| 242 | 'clone' => sprintf("<a href='%s'>%s</a>", $clone_url, esc_attr__('Duplicate', 'wp-user-avatar')), |
| 243 | // using form prefix cos there is a preview admin class that floats element to the right |
| 244 | 'form-preview' => sprintf("<a target='_blank' href='%s'>%s</a>", $preview_url, esc_attr__('Preview', 'wp-user-avatar')) |
| 245 | ); |
| 246 | |
| 247 | $name = '<strong><a href="' . $customize_url . '">' . $item['name'] . '</a></strong>'; |
| 248 | |
| 249 | |
| 250 | return $name . $this->row_actions($actions); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Method for Shortcode column |
| 255 | * |
| 256 | * @param array $item an array of DB data |
| 257 | * |
| 258 | * @return string |
| 259 | */ |
| 260 | public function column_shortcode($item) |
| 261 | { |
| 262 | $form_type = sanitize_text_field($item['form_type']); |
| 263 | |
| 264 | $shortcode = sprintf('[profilepress-%s id="%s"]', $form_type, absint($item['form_id'])); |
| 265 | |
| 266 | ob_start(); |
| 267 | ?> |
| 268 | <div style="display: flex; align-items: center; gap: 5px;"> |
| 269 | <input type="text" |
| 270 | class="shortcode-in-list-table" |
| 271 | onfocus="this.select();" |
| 272 | readonly="readonly" |
| 273 | value="<?php echo esc_attr($shortcode); ?>" |
| 274 | style="min-width: 200px;" /> |
| 275 | |
| 276 | <button type="button" class="button ppress-copy-url-icon" title="<?php esc_attr_e( 'Copy Shortcode', 'wp-user-avatar' ); ?>"> |
| 277 | <span class="dashicons dashicons-admin-page"></span> |
| 278 | </button> |
| 279 | |
| 280 | <span class="ppress-copy-msg" style="display:none; color:green;"><?php esc_html_e( 'Copied!', 'wp-user-avatar' ); ?></span> |
| 281 | </div> |
| 282 | <?php |
| 283 | return ob_get_clean(); |
| 284 | } |
| 285 | |
| 286 | public function column_builder($item) |
| 287 | { |
| 288 | $builder_type = esc_html__('Shortcode', 'wp-user-avatar'); |
| 289 | if ($item['builder_type'] == FR::DRAG_DROP_BUILDER_TYPE) { |
| 290 | $builder_type = esc_html__('Drag & Drop', 'wp-user-avatar'); |
| 291 | } |
| 292 | |
| 293 | return $builder_type; |
| 294 | } |
| 295 | |
| 296 | public function column_default($item, $column_name) |
| 297 | { |
| 298 | switch ($column_name) { |
| 299 | case 'date' : |
| 300 | $value = mysql2date('F j, Y', $item['date']); |
| 301 | break; |
| 302 | default: |
| 303 | $value = $item[$column_name]; |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | return apply_filters('ppress_forms_table_column', $value, $item, $column_name); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Columns to make sortable. |
| 312 | * |
| 313 | * @return array |
| 314 | */ |
| 315 | public function get_sortable_columns() |
| 316 | { |
| 317 | $sortable_columns = array( |
| 318 | 'name' => array('name', true), |
| 319 | ); |
| 320 | |
| 321 | return $sortable_columns; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns an associative array containing the bulk action |
| 326 | * |
| 327 | * @return array |
| 328 | */ |
| 329 | public function get_bulk_actions() |
| 330 | { |
| 331 | $actions = array( |
| 332 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar'), |
| 333 | ); |
| 334 | |
| 335 | return $actions; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Handles data query and filter, sorting, and pagination. |
| 340 | * |
| 341 | * @param string $form_type |
| 342 | */ |
| 343 | public function prepare_items($form_type = '') |
| 344 | { |
| 345 | if (isset($_GET['page']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && ! empty($_GET['form-type'])) { |
| 346 | $form_type = sanitize_text_field($_GET['form-type']); |
| 347 | } |
| 348 | |
| 349 | $this->_column_headers = $this->get_column_info(); |
| 350 | /** Process bulk action */ |
| 351 | $this->process_actions(); |
| 352 | $per_page = $this->get_items_per_page('forms_per_page', 10); |
| 353 | $current_page = $this->get_pagenum(); |
| 354 | $total_items = self::record_count($form_type); |
| 355 | $this->set_pagination_args([ |
| 356 | 'total_items' => $total_items, //WE have to calculate the total number of items |
| 357 | 'per_page' => $per_page //WE have to determine how many items to show on a page |
| 358 | ]); |
| 359 | |
| 360 | $this->items = $this->get_forms($per_page, $current_page, $form_type); |
| 361 | } |
| 362 | |
| 363 | public function process_actions() |
| 364 | { |
| 365 | // Bail if user is not an admin or without admin privileges. |
| 366 | if ( ! current_user_can('manage_options')) return; |
| 367 | |
| 368 | if ('delete' === $this->current_action()) { |
| 369 | |
| 370 | if ( ! ppress_verify_nonce()) wp_nonce_ays(ppress_nonce_action_string()); |
| 371 | |
| 372 | $form_id = absint($_GET['id']); |
| 373 | $form_type = sanitize_text_field($_GET['form_type']); |
| 374 | |
| 375 | FR::delete_form($form_id, $form_type); |
| 376 | |
| 377 | $url = PPRESS_FORMS_SETTINGS_PAGE; |
| 378 | |
| 379 | if (isset($_GET['form_type'])) { |
| 380 | |
| 381 | $url = add_query_arg('form-type', sanitize_text_field($_GET['form_type']), $url); |
| 382 | |
| 383 | if (FR::MEMBERS_DIRECTORY_TYPE == $_GET['form_type']) { |
| 384 | $url = PPRESS_MEMBER_DIRECTORIES_SETTINGS_PAGE; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | ppress_do_admin_redirect($url); |
| 389 | } |
| 390 | |
| 391 | if ('clone' === $this->current_action()) { |
| 392 | |
| 393 | if ( ! ppress_verify_nonce()) wp_nonce_ays(ppress_nonce_action_string()); |
| 394 | |
| 395 | $form_id = absint($_GET['id']); |
| 396 | $form_type = sanitize_text_field($_GET['form_type']); |
| 397 | |
| 398 | FR::clone_form($form_id, $form_type); |
| 399 | |
| 400 | $url = PPRESS_FORMS_SETTINGS_PAGE; |
| 401 | |
| 402 | if (isset($_GET['form_type'])) { |
| 403 | $url = add_query_arg('form-type', sanitize_text_field($_GET['form_type']), $url); |
| 404 | |
| 405 | if (FR::MEMBERS_DIRECTORY_TYPE == $_GET['form_type']) { |
| 406 | $url = PPRESS_MEMBER_DIRECTORIES_SETTINGS_PAGE; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | ppress_do_admin_redirect($url); |
| 411 | } |
| 412 | |
| 413 | // Detect when a bulk action is being triggered... |
| 414 | if ('bulk-delete' == $this->current_action()) { |
| 415 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 416 | $form_ids = array_map('absint', $_POST['form_id']); |
| 417 | |
| 418 | foreach ($form_ids as $form_id) { |
| 419 | $form_type = ! empty($_GET['form-type']) ? sanitize_text_field($_GET['form-type']) : FR::LOGIN_TYPE; |
| 420 | if (isset($_GET['page']) && $_GET['page'] == PPRESS_MEMBER_DIRECTORIES_SLUG) { |
| 421 | $form_type = FR::MEMBERS_DIRECTORY_TYPE; |
| 422 | } |
| 423 | |
| 424 | FR::delete_form($form_id, $form_type); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * @return FormList |
| 431 | */ |
| 432 | public static function get_instance() |
| 433 | { |
| 434 | static $instance = null; |
| 435 | |
| 436 | if (is_null($instance)) { |
| 437 | $instance = new static($GLOBALS['wpdb']); |
| 438 | } |
| 439 | |
| 440 | return $instance; |
| 441 | } |
| 442 | } |