[ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Functions to deal with the option API 4 * 5 */ 6 7 /** 8 * Read an option from DB (or from cache if available). Return value or $default if not found 9 * 10 * Pretty much stolen from WordPress 11 * 12 * @since 1.4 13 * @param string $option_name Option name. Expected to not be SQL-escaped. 14 * @param mixed $default Optional value to return if option doesn't exist. Default false. 15 * @return mixed Value set for the option. 16 */ 17 function yourls_get_option( $option_name, $default = false ) { 18 // Allow plugins to short-circuit options 19 $pre = yourls_apply_filter( 'shunt_option_'.$option_name, false ); 20 if ( false !== $pre ) { 21 return $pre; 22 } 23 24 $option = new \YOURLS\Database\Options(yourls_get_db()); 25 $value = $option->get($option_name, $default); 26 27 return yourls_apply_filter( 'get_option_'.$option_name, $value ); 28 } 29 30 /** 31 * Read all options from DB at once 32 * 33 * The goal is to read all options at once and then populate array $ydb->option, to prevent further 34 * SQL queries if we need to read an option value later. 35 * It's also a simple check whether YOURLS is installed or not (no option = assuming not installed) after 36 * a check for DB server reachability has been performed 37 * 38 * @since 1.4 39 * @return void 40 */ 41 function yourls_get_all_options() { 42 // Allow plugins to short-circuit all options. (Note: regular plugins are loaded after all options) 43 $pre = yourls_apply_filter( 'shunt_all_options', false ); 44 if ( false !== $pre ) { 45 return $pre; 46 } 47 48 $options = new \YOURLS\Database\Options(yourls_get_db()); 49 50 if ($options->get_all_options() === false) { 51 // Zero option found but no unexpected error so far: YOURLS isn't installed 52 yourls_set_installed(false); 53 return; 54 } 55 56 yourls_set_installed(true); 57 } 58 59 /** 60 * Update (add if doesn't exist) an option to DB 61 * 62 * Pretty much stolen from WordPress 63 * 64 * @since 1.4 65 * @param string $option_name Option name. Expected to not be SQL-escaped. 66 * @param mixed $newvalue Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. 67 * @return bool False if value was not updated, true otherwise. 68 */ 69 function yourls_update_option( $option_name, $newvalue ) { 70 $option = new \YOURLS\Database\Options(yourls_get_db()); 71 $update = $option->update($option_name, $newvalue); 72 73 return $update; 74 } 75 76 /** 77 * Add an option to the DB 78 * 79 * Pretty much stolen from WordPress 80 * 81 * @since 1.4 82 * @param string $name Name of option to add. Expected to not be SQL-escaped. 83 * @param mixed $value Optional option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. 84 * @return bool False if option was not added and true otherwise. 85 */ 86 function yourls_add_option( $name, $value = '' ) { 87 $option = new \YOURLS\Database\Options(yourls_get_db()); 88 $add = $option->add($name, $value); 89 90 return $add; 91 } 92 93 /** 94 * Delete an option from the DB 95 * 96 * Pretty much stolen from WordPress 97 * 98 * @since 1.4 99 * @param string $name Option name to delete. Expected to not be SQL-escaped. 100 * @return bool True, if option is successfully deleted. False on failure. 101 */ 102 function yourls_delete_option( $name ) { 103 $option = new \YOURLS\Database\Options(yourls_get_db()); 104 $delete = $option->delete($name); 105 106 return $delete; 107 } 108 109 /** 110 * Serialize data if needed. Stolen from WordPress 111 * 112 * @since 1.4 113 * @param mixed $data Data that might be serialized. 114 * @return mixed A scalar data 115 */ 116 function yourls_maybe_serialize( $data ) { 117 if ( is_array( $data ) || is_object( $data ) ) 118 return serialize( $data ); 119 120 if ( yourls_is_serialized( $data, false ) ) 121 return serialize( $data ); 122 123 return $data; 124 } 125 126 /** 127 * Unserialize value only if it was serialized. Stolen from WP 128 * 129 * @since 1.4 130 * @param string $original Maybe unserialized original, if is needed. 131 * @return mixed Unserialized data can be any type. 132 */ 133 function yourls_maybe_unserialize( $original ) { 134 if ( yourls_is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in 135 return @unserialize( $original ); 136 return $original; 137 } 138 139 /** 140 * Check value to find if it was serialized. Stolen from WordPress 141 * 142 * @since 1.4 143 * @param mixed $data Value to check to see if was serialized. 144 * @param bool $strict Optional. Whether to be strict about the end of the string. Defaults true. 145 * @return bool False if not serialized and true if it was. 146 */ 147 function yourls_is_serialized( $data, $strict = true ) { 148 // if it isn't a string, it isn't serialized 149 if ( ! is_string( $data ) ) 150 return false; 151 $data = trim( $data ); 152 if ( 'N;' == $data ) 153 return true; 154 $length = strlen( $data ); 155 if ( $length < 4 ) 156 return false; 157 if ( ':' !== $data[1] ) 158 return false; 159 if ( $strict ) { 160 $lastc = $data[ $length - 1 ]; 161 if ( ';' !== $lastc && '}' !== $lastc ) 162 return false; 163 } else { 164 $semicolon = strpos( $data, ';' ); 165 $brace = strpos( $data, '}' ); 166 // Either ; or } must exist. 167 if ( false === $semicolon && false === $brace ) 168 return false; 169 // But neither must be in the first X characters. 170 if ( false !== $semicolon && $semicolon < 3 ) 171 return false; 172 if ( false !== $brace && $brace < 4 ) 173 return false; 174 } 175 $token = $data[0]; 176 switch ( $token ) { 177 case 's' : 178 if ( $strict ) { 179 if ( '"' !== $data[ $length - 2 ] ) 180 return false; 181 } elseif ( false === strpos( $data, '"' ) ) { 182 return false; 183 } 184 // or else fall through 185 case 'a' : 186 case 'O' : 187 return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); 188 case 'b' : 189 case 'i' : 190 case 'd' : 191 $end = $strict ? '$' : ''; 192 return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data ); 193 } 194 return false; 195 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Feb 22 05:10:06 2025 | Cross-referenced by PHPXref 0.7.1 |