| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataAccess\Utilities |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataAccess\Utilities { |
| 10 |
|
| 11 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist; |
| 12 |
use WPDataAccess\WPDA; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WPDA_Favourites |
| 16 |
* |
| 17 |
* @author Peter Schulz |
| 18 |
* @since 2.0.13 |
| 19 |
*/ |
| 20 |
class WPDA_Favourites { |
| 21 |
|
| 22 |
const FAVOURITES_OPTION_NAME = 'wpda_favourites'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Database schema name |
| 26 |
* |
| 27 |
* @var string|null |
| 28 |
*/ |
| 29 |
protected $schema_name = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Database table name |
| 33 |
* |
| 34 |
* @var string|null |
| 35 |
*/ |
| 36 |
protected $table_name = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* WPDA_Favourites constructor |
| 40 |
* |
| 41 |
* Checks if the given tables exists. |
| 42 |
* |
| 43 |
* @since 1.2.0 |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
if ( isset( $_REQUEST['wpdaschema_name'] ) && isset( $_REQUEST['table_name'] ) ) { |
| 47 |
$this->schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) ); // input var okay. |
| 48 |
$this->table_name = sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ); // input var okay. |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Add table to favourites |
| 54 |
* |
| 55 |
* @return string '0' = failed; '1' = succeeded |
| 56 |
* |
| 57 |
* @since 1.2.0 |
| 58 |
*/ |
| 59 |
public function add() { |
| 60 |
if ( ! $this->check_dictionary() ) { |
| 61 |
echo '0'; // Failed. |
| 62 |
|
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if ( null === $this->table_name ) { |
| 67 |
echo '0'; // Failed. |
| 68 |
|
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$option_value = get_option( self::FAVOURITES_OPTION_NAME ); |
| 73 |
|
| 74 |
if ( false === $option_value ) { |
| 75 |
$favourites_array = array(); |
| 76 |
} else { |
| 77 |
$favourites_array = $option_value; |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! isset( $favourites_array[ $this->table_name ] ) ) { |
| 81 |
$favourites_array[ $this->table_name ] = $this->table_name; |
| 82 |
update_option( self::FAVOURITES_OPTION_NAME, $favourites_array ); |
| 83 |
echo '1'; |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
echo '0'; // Failed. |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Remove table from favourites |
| 93 |
* |
| 94 |
* @return string '0' = failed; '1' = succeeded |
| 95 |
* |
| 96 |
* @since 1.2.0 |
| 97 |
*/ |
| 98 |
public function rem() { |
| 99 |
if ( ! $this->check_dictionary() ) { |
| 100 |
echo '0'; // Failed. |
| 101 |
|
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
if ( null === $this->table_name ) { |
| 106 |
echo '0'; // Failed. |
| 107 |
|
| 108 |
return; |
| 109 |
} |
| 110 |
$option_value = get_option( self::FAVOURITES_OPTION_NAME ); |
| 111 |
|
| 112 |
if ( false !== $option_value ) { |
| 113 |
$favourites_array = $option_value; |
| 114 |
if ( isset( $favourites_array[ $this->table_name ] ) ) { |
| 115 |
unset( $favourites_array[ $this->table_name ] ); |
| 116 |
update_option( self::FAVOURITES_OPTION_NAME, $favourites_array ); |
| 117 |
echo '1'; |
| 118 |
|
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
echo '0'; // Failed. |
| 124 |
} |
| 125 |
|
| 126 |
private function check_dictionary() { |
| 127 |
if ( ! WPDA::schema_exists( $this->schema_name ) ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
$wpda_dictionary_exist = new WPDA_Dictionary_Exist( $this->schema_name, $this->table_name ); |
| 132 |
if ( ! $wpda_dictionary_exist->table_exists( $this->table_name ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|