| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataAccess\Plugin_Table_Models |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataAccess\Plugin_Table_Models { |
| 10 |
|
| 11 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist; |
| 12 |
use WPDataAccess\WPDA; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WPDA_Plugin_Table_Base_Model |
| 16 |
* |
| 17 |
* Base class to handle standard plugin table features |
| 18 |
* |
| 19 |
* @author Peter Schulz |
| 20 |
* @since 2.6.0 |
| 21 |
*/ |
| 22 |
class WPDA_Plugin_Table_Base_Model { |
| 23 |
|
| 24 |
/** |
| 25 |
* Base table name (without prefixes): MUST BE DEFINED IN SUBCLASS!!! |
| 26 |
*/ |
| 27 |
const BASE_TABLE_NAME = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Check if const BASE_TABLE_NAME is defined (cannot proceed without) |
| 31 |
*/ |
| 32 |
public static function check_base_table_name() { |
| 33 |
if ( null === static::BASE_TABLE_NAME ) { |
| 34 |
wp_die( __( 'Wrong usage of class WPDA_Plugin_Table_Base_Model [missing BASE_TABLE_NAME]', 'wp-data-access' ) ); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Check if base table exists |
| 40 |
* |
| 41 |
* @return bool TRUE = table found |
| 42 |
*/ |
| 43 |
public static function table_exists() { |
| 44 |
static::check_base_table_name(); |
| 45 |
|
| 46 |
$wpda_dictionary_exist = new WPDA_Dictionary_Exist( '', static::get_base_table_name() ); |
| 47 |
return $wpda_dictionary_exist->table_exists( false ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get base table name |
| 52 |
* |
| 53 |
* @return string Base table name |
| 54 |
*/ |
| 55 |
public static function get_base_table_name() { |
| 56 |
static::check_base_table_name(); |
| 57 |
|
| 58 |
global $wpdb; |
| 59 |
return $wpdb->prefix . static::BASE_TABLE_NAME; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Return number of records in base table |
| 64 |
* |
| 65 |
* @return int |
| 66 |
*/ |
| 67 |
public static function count() { |
| 68 |
static::check_base_table_name(); |
| 69 |
|
| 70 |
global $wpdb; |
| 71 |
$result = $wpdb->get_results( |
| 72 |
$wpdb->prepare( |
| 73 |
'SELECT count(*) AS noitems FROM `%1s` ', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 74 |
array( |
| 75 |
WPDA::remove_backticks( static::get_base_table_name() ), |
| 76 |
) |
| 77 |
), |
| 78 |
'ARRAY_A' |
| 79 |
); // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 80 |
|
| 81 |
if ( 1 === $wpdb->num_rows ) { |
| 82 |
return $result[0]['noitems']; |
| 83 |
} else { |
| 84 |
return 0; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|