Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Error.php
201 lines
| 1 | <?php |
| 2 | // Don't load directly |
| 3 | defined( 'WPINC' ) or die; |
| 4 | |
| 5 | class Tribe__Error { |
| 6 | /** |
| 7 | * All the Errors Registered |
| 8 | * @var array |
| 9 | */ |
| 10 | private $items = []; |
| 11 | |
| 12 | /** |
| 13 | * Static Singleton Holder |
| 14 | * |
| 15 | * @var self |
| 16 | */ |
| 17 | private static $instance; |
| 18 | |
| 19 | /** |
| 20 | * Static Singleton Factory Method |
| 21 | * |
| 22 | * @return self |
| 23 | */ |
| 24 | public static function instance() { |
| 25 | if ( ! self::$instance ) { |
| 26 | self::$instance = new self; |
| 27 | } |
| 28 | |
| 29 | return self::$instance; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Setup all the hooks and filters |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | private function __construct() { |
| 38 | $this->register( 'unknown', esc_html__( 'An Unknown error occurred' ) ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Make a quickly usable method to transform code/indexes to WP_Errors |
| 43 | * |
| 44 | * @see tribe_error() |
| 45 | * |
| 46 | * @param string|array $indexes Which Error we are looking for |
| 47 | * @param array $context Gives the Error context |
| 48 | * @param array $sprintf Allows variables on the message |
| 49 | * |
| 50 | * @return WP_Error |
| 51 | */ |
| 52 | public function send( $indexes, $context = [], $sprintf = [] ) { |
| 53 | if ( ! $this->exists( $indexes ) ) { |
| 54 | $indexes = [ 'unknown' ]; |
| 55 | } |
| 56 | |
| 57 | // Fetches the Errors |
| 58 | $messages = (array) $this->get( $indexes ); |
| 59 | $error = new WP_Error; |
| 60 | |
| 61 | foreach ( $messages as $key => $message ) { |
| 62 | // Allows variables when sending the message |
| 63 | if ( ! empty( $sprintf ) ) { |
| 64 | $message = vsprintf( $message, $sprintf ); |
| 65 | } |
| 66 | // Add this Message to the WP_Error |
| 67 | $error->add( $key, $message, $context ); |
| 68 | } |
| 69 | |
| 70 | return $error; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register a new error based on a Namespace |
| 75 | * |
| 76 | * @param string|array $indexes A list of the namespaces and last item should be the error name |
| 77 | * @param string $message What is going to be the message associate with this indexes |
| 78 | * |
| 79 | * @return boolean |
| 80 | */ |
| 81 | public function register( $indexes, $message ) { |
| 82 | if ( is_string( $indexes ) ) { |
| 83 | // Each namespace should come with `:` |
| 84 | $indexes = (array) explode( ':', $indexes ); |
| 85 | } |
| 86 | |
| 87 | // Couldn't register the error |
| 88 | if ( empty( $indexes ) ) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | $variable = &$this->items; |
| 93 | $count = count( $indexes ); |
| 94 | |
| 95 | // Will create the Indexes based on the $slug |
| 96 | foreach ( $indexes as $i => $index ) { |
| 97 | if ( $count === $i + 1 ) { |
| 98 | $variable[ $index ] = $message; |
| 99 | } else { |
| 100 | $variable = &$variable[ $index ]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Allows Chain Reactions |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Removes an error from the items |
| 110 | * |
| 111 | * @param string|array $indexes A list of the namespaces and last item should be the error name |
| 112 | * |
| 113 | * @return boolean |
| 114 | */ |
| 115 | public function remove( $indexes ) { |
| 116 | if ( ! $this->exists( $indexes ) ) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if ( is_string( $indexes ) ) { |
| 121 | // Each namespace should come with `:` |
| 122 | $indexes = (array) explode( ':', $indexes ); |
| 123 | } |
| 124 | |
| 125 | // Ensures that we don't modify the original |
| 126 | $variable = &$this->items; |
| 127 | $count = count( $indexes ); |
| 128 | |
| 129 | foreach ( $indexes as $i => $index ) { |
| 130 | if ( $count === $i + 1 ) { |
| 131 | unset( $variable[ $index ] ); |
| 132 | } else { |
| 133 | $variable = &$variable[ $index ]; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Fetches the error or namespace |
| 142 | * |
| 143 | * @param string|array $indexes (optional) A list of the namespaces and last item should be the error name |
| 144 | * |
| 145 | * @return null|array|string |
| 146 | */ |
| 147 | public function get( $indexes = null ) { |
| 148 | if ( is_null( $indexes ) ) { |
| 149 | return $this->items; |
| 150 | } |
| 151 | |
| 152 | if ( is_string( $indexes ) ) { |
| 153 | // Each namespace should come with `:` |
| 154 | $indexes = (array) explode( ':', $indexes ); |
| 155 | } |
| 156 | |
| 157 | // Ensures that we don't modify the original |
| 158 | $variable = $this->items; |
| 159 | $count = count( $indexes ); |
| 160 | |
| 161 | foreach ( $indexes as $i => $index ) { |
| 162 | if ( ! isset( $variable[ $index ] ) ) { |
| 163 | // If we are on the last item and we don't have it set make it Null |
| 164 | if ( $count === $i + 1 ) { |
| 165 | return null; |
| 166 | } |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | $variable = $variable[ $index ]; |
| 171 | } |
| 172 | |
| 173 | $return = []; |
| 174 | $was_namespace = is_array( $variable ); |
| 175 | |
| 176 | /** |
| 177 | * @todo Allow fetching bigger groups |
| 178 | * Right now you can only fetch the first group of messages |
| 179 | * Trying to fetch Namespaces that contain other namespaces will bug |
| 180 | */ |
| 181 | foreach ( (array) $variable as $key => $value ) { |
| 182 | $key = implode( ':', $indexes ) . ( $was_namespace ? ':' . $key : '' ); |
| 183 | $return[ $key ] = $value; |
| 184 | } |
| 185 | |
| 186 | return $return; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Checks if a given error or namespace exists |
| 191 | * |
| 192 | * @param string|array $indexes A list of the namespaces and last item should be the error name |
| 193 | * |
| 194 | * @return boolean |
| 195 | */ |
| 196 | public function exists( $indexes ) { |
| 197 | $variable = $this->get( $indexes ); |
| 198 | return ! empty( $variable ) || is_array( $variable ) ? true : false; |
| 199 | } |
| 200 | } |
| 201 |