PluginProbe
The Innovative Form Builder – IvyForms / 0.5
The Innovative Form Builder – IvyForms v0.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / InstallActions / DB / AbstractDatabaseTable.php

AbstractDatabaseTable.php in The Innovative Form Builder – IvyForms 0.5, at backend/src/Services/InstallActions/DB/AbstractDatabaseTable.php

96 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace IvyForms\Services\InstallActions\DB;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\InvalidArgumentException;
16 use IvyForms\Services\Translations\BackendStrings;
17
18 /**
19 * Class AbstractDatabaseTable
20 *
21 * @package IvyForms\Services\InstallActions\DB
22 */
23 class AbstractDatabaseTable
24 {
25 public const TABLE = '';
26
27 /**
28 * @return string
29 * @throws InvalidArgumentException
30 */
31 public static function getTableName(): string
32 {
33 if (!static::TABLE) {
34 throw new InvalidArgumentException(
35 BackendStrings::getExceptionStrings()['table_name_not_provided']
36 );
37 }
38
39 global $wpdb;
40
41 return $wpdb->prefix . 'ivyforms_' . static::TABLE;
42 }
43
44 /**
45 * Create new table in the database
46 */
47 public static function init(): void
48 {
49 if (file_exists(ABSPATH . 'wp-admin/includes/upgrade.php')) {
50 // @phpstan-ignore-next-line
51 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
52 }
53 // @phpstan-ignore staticMethod.notFound
54 dbDelta(static::buildTable());
55
56 global $wpdb;
57
58 foreach (static::alterTable() as $command) {
59 $wpdb->query($wpdb->prepare($command));
60 }
61 }
62
63 /**
64 * Delete table from the database
65 *
66 * @throws InvalidArgumentException
67 */
68 public static function delete(): void
69 {
70 global $wpdb;
71
72 $table = self::getTableName();
73
74 $sql = "DROP TABLE IF EXISTS $table;";
75 $wpdb->query($wpdb->prepare($sql));
76 }
77
78 /**
79 * @return boolean
80 */
81 public static function isValidTablePrefix(): bool
82 {
83 global $wpdb;
84
85 return strlen($wpdb->prefix) <= 16;
86 }
87
88 /**
89 * @return mixed[]
90 */
91 public static function alterTable(): array
92 {
93 return [];
94 }
95 }
96