| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataAccess\User_Menu |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataAccess\User_Menu { |
| 9 |
|
| 10 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist; |
| 11 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 12 |
use WPDataAccess\WPDA; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WPDA_User_Menu_Model |
| 16 |
* |
| 17 |
* @package WPDataAccess\User_Menu |
| 18 |
* @author Peter Schulz |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
class WPDA_User_Menu_Model { |
| 22 |
|
| 23 |
/** |
| 24 |
* Base table name (without prefix) |
| 25 |
*/ |
| 26 |
const WPDA_TABLE_NAME = 'menu_items'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get data menu table name |
| 30 |
* |
| 31 |
* @return string Data menu table name |
| 32 |
*/ |
| 33 |
public static function get_menu_table_name() { |
| 34 |
|
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
return $wpdb->prefix . WPDA::get_option( WPDA::OPTION_WPDA_PREFIX ) . self::WPDA_TABLE_NAME; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Check if table menu_items exists |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* |
| 46 |
* @return bool TRUE = table found |
| 47 |
*/ |
| 48 |
public static function table_menu_items_exists() { |
| 49 |
|
| 50 |
$wpda_dictionary_exist = new WPDA_Dictionary_Exist( '', self::get_menu_table_name() ); |
| 51 |
|
| 52 |
return $wpda_dictionary_exist->table_exists( false ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* List of external menu items |
| 58 |
* |
| 59 |
* Used in {@see \WP_Data_Access_Admin::add_menu_my_tables()} to build user defined menus. |
| 60 |
* |
| 61 |
* Returns all external menu items. These menus are below a user defined menu. |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
* |
| 65 |
* @return array List of menu items |
| 66 |
*/ |
| 67 |
public static function list_external_menus() { |
| 68 |
|
| 69 |
global $wpdb; |
| 70 |
|
| 71 |
if ( self::table_menu_items_exists() ) { |
| 72 |
|
| 73 |
return $wpdb->get_results(' |
| 74 |
select * |
| 75 |
from ' . self::get_menu_table_name() . ' |
| 76 |
order by menu_name |
| 77 |
'); // WPCS: unprepared SQL OK; db call ok; no-cache ok. |
| 78 |
|
| 79 |
} else { |
| 80 |
|
| 81 |
return []; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if before insert trigger exists |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* |
| 92 |
* @return bool TRUE = trigger found |
| 93 |
*/ |
| 94 |
public static function triggers_menu_items_before_insert_exists() { |
| 95 |
|
| 96 |
global $wpdb; |
| 97 |
|
| 98 |
$schema_name = $wpdb->dbname; |
| 99 |
$trigger_name = |
| 100 |
$wpdb->prefix . WPDA::get_option( WPDA::OPTION_WPDA_PREFIX ) . |
| 101 |
self::WPDA_TABLE_NAME . '_before_insert'; |
| 102 |
|
| 103 |
$wpda_dictionary_exist = new WPDA_Dictionary_Exist( '', self::get_menu_table_name() ); |
| 104 |
|
| 105 |
|
| 106 |
return $wpda_dictionary_exist->trigger_exists( $schema_name, $trigger_name ); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Check if before update trigger exists |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @return bool TRUE = trigger found |
| 116 |
*/ |
| 117 |
public static function triggers_menu_items_before_update_exists() { |
| 118 |
|
| 119 |
global $wpdb; |
| 120 |
|
| 121 |
$schema_name = $wpdb->dbname; |
| 122 |
$trigger_name = |
| 123 |
$wpdb->prefix . WPDA::get_option( WPDA::OPTION_WPDA_PREFIX ) . |
| 124 |
self::WPDA_TABLE_NAME . '_before_update'; |
| 125 |
|
| 126 |
$wpda_dictionary_exist = new WPDA_Dictionary_Exist( '', self::get_menu_table_name() ); |
| 127 |
|
| 128 |
return $wpda_dictionary_exist->trigger_exists( $schema_name, $trigger_name ); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Return number of menu items in repository |
| 134 |
* |
| 135 |
* @since 1.0.0 |
| 136 |
* |
| 137 |
* @return int |
| 138 |
*/ |
| 139 |
public static function count_menu_items_stored() { |
| 140 |
|
| 141 |
if ( ! self::table_menu_items_exists() ) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
global $wpdb; |
| 146 |
|
| 147 |
$query = 'SELECT count(*) AS noitems FROM ' . self::get_menu_table_name(); |
| 148 |
|
| 149 |
$result = $wpdb->get_results( $query, 'ARRAY_A' ); // WPCS: unprepared SQL OK; db call ok; no-cache ok. |
| 150 |
|
| 151 |
if ( 1 === $wpdb->num_rows ) { |
| 152 |
return $result[0]['noitems']; |
| 153 |
} else { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|