| @@ -1,0 +1,154 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Templately; | |
| 4 | + | |
| 5 | +class DB { | |
| 6 | + /** | |
| 7 | + * Get an option | |
| 8 | + * | |
| 9 | + * @param string $key | |
| 10 | + * @param mixed $default | |
| 11 | + * | |
| 12 | + * @return void | |
| 13 | + */ | |
| 14 | + public static function get_option( $key, $default = false ) { | |
| 15 | + $trimmed_key = trim( $key ); | |
| 16 | + | |
| 17 | + return empty( $trimmed_key ) ? false : \get_option( $trimmed_key, $default ); | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Update an option | |
| 22 | + * | |
| 23 | + * @param string $key | |
| 24 | + * @param mixed $value | |
| 25 | + * @param string|boolean $autoload | |
| 26 | + * | |
| 27 | + * @return void | |
| 28 | + */ | |
| 29 | + public static function update_option( $key, $value, $autoload = null ) { | |
| 30 | + $trimmed_key = trim( $key ); | |
| 31 | + | |
| 32 | + return empty( $trimmed_key ) ? false : \update_option( $trimmed_key, $value, $autoload ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Update|Set user meta. | |
| 37 | + * | |
| 38 | + * @param int $user_id | |
| 39 | + * @param string $meta_key | |
| 40 | + * @param mixed $meta_value | |
| 41 | + */ | |
| 42 | + public static function update_user_meta( $user_id = 0, $meta_key, $meta_value ) { | |
| 43 | + | |
| 44 | + if ( ! $user_id ) { | |
| 45 | + $user_id = get_current_user_id(); | |
| 46 | + } | |
| 47 | + | |
| 48 | + $user_id = absint( $user_id ); | |
| 49 | + $meta_key = trim( $meta_key ); | |
| 50 | + | |
| 51 | + if ( ! $user_id && empty( $meta_key ) ) { | |
| 52 | + return false; | |
| 53 | + } | |
| 54 | + | |
| 55 | + return \update_user_meta( $user_id, $meta_key, $meta_value ); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Get an option | |
| 60 | + * | |
| 61 | + * @param int $user_id | |
| 62 | + * @param string $meta_key | |
| 63 | + * @param bool $single | |
| 64 | + */ | |
| 65 | + public static function get_user_meta( $user_id = 0, $meta_key, $default = false, $single = true ) { | |
| 66 | + | |
| 67 | + if ( ! $user_id ) { | |
| 68 | + $user_id = get_current_user_id(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + $user_id = absint( $user_id ); | |
| 72 | + $meta_key = trim( $meta_key ); | |
| 73 | + | |
| 74 | + if ( ! $user_id && empty( $meta_key ) ) { | |
| 75 | + return false; | |
| 76 | + } | |
| 77 | + | |
| 78 | + $res = \get_user_meta( $user_id, $meta_key, $single ); | |
| 79 | + | |
| 80 | + return ( ! $res ) ? $default : $res; | |
| 81 | + | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Delete a user meta. | |
| 86 | + * | |
| 87 | + * @param int $user_id | |
| 88 | + * @param string $meta_key | |
| 89 | + */ | |
| 90 | + public static function delete_user_meta( $user_id, $meta_key ) { | |
| 91 | + | |
| 92 | + if ( ! $user_id ) { | |
| 93 | + return false; | |
| 94 | + } | |
| 95 | + | |
| 96 | + $user_id = absint( $user_id ); | |
| 97 | + $meta_key = trim( $meta_key ); | |
| 98 | + | |
| 99 | + if ( ! $user_id && empty( $meta_key ) ) { | |
| 100 | + return false; | |
| 101 | + } | |
| 102 | + | |
| 103 | + return \delete_user_meta( $user_id, $meta_key ); | |
| 104 | + | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Delete an option | |
| 109 | + * | |
| 110 | + * @param string $key | |
| 111 | + * | |
| 112 | + * @return void | |
| 113 | + */ | |
| 114 | + public static function delete_option( $key ) { | |
| 115 | + $trimmed_key = trim( $key ); | |
| 116 | + if ( ! empty( $trimmed_key ) ) { | |
| 117 | + return \delete_option( $trimmed_key ); | |
| 118 | + } | |
| 119 | + | |
| 120 | + return false; | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Update user specif data based on login choice $global|$local. | |
| 125 | + * | |
| 126 | + * @param string $meta_key | |
| 127 | + * @param string $value; | |
| 128 | + * @return void | |
| 129 | + */ | |
| 130 | + public static function update_user_specific_login_meta( $meta_key, $value ) { | |
| 131 | + return Login::get_link_my_account() != '' && Login::get_link_my_account() ? self::update_user_meta( get_current_user_id(), $meta_key, $value ) : self::update_option( $meta_key, $value ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Get user speficic data based on login choiche $global|$local | |
| 136 | + * | |
| 137 | + * @param string $meta_key | |
| 138 | + * @return mixed | |
| 139 | + */ | |
| 140 | + public static function get_user_specific_login_meta( $meta_key, $default = false, $single = true ) { | |
| 141 | + return Login::get_link_my_account() != '' && Login::get_link_my_account() ? self::get_user_meta( get_current_user_id(), $meta_key, $default, $single ) : self::get_option( $meta_key, $default ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Delete user speficic data based on login choiche $global|$local | |
| 146 | + * | |
| 147 | + * @param string $meta_key | |
| 148 | + * @return mixed | |
| 149 | + */ | |
| 150 | + public static function delete_user_specific_login_meta( $meta_key ) { | |
| 151 | + return Login::get_link_my_account() != '' && Login::get_link_my_account() ? self::delete_user_meta( get_current_user_id(), $meta_key ) : self::delete_option( $meta_key ); | |
| 152 | + } | |
| 153 | + | |
| 154 | +} | |