@@ -64,11 +64,11 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * reset_cached_states |
|
| 68 | - * |
|
| 69 | - * @access private |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 67 | + * reset_cached_states |
|
| 68 | + * |
|
| 69 | + * @access private |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | 72 | public function reset_cached_states() { |
| 73 | 73 | EEM_State::$_active_states = array(); |
| 74 | 74 | EEM_State::$_all_states = array(); |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | - * _get_states |
|
| 82 | - * |
|
| 83 | - * @access private |
|
| 84 | - * @return array |
|
| 85 | - */ |
|
| 81 | + * _get_states |
|
| 82 | + * |
|
| 83 | + * @access private |
|
| 84 | + * @return array |
|
| 85 | + */ |
|
| 86 | 86 | public function get_all_states() { |
| 87 | 87 | if ( ! self::$_all_states ) { |
| 88 | 88 | self::$_all_states = $this->get_all( array( 'order_by'=>array( 'STA_name'=>'ASC' ), 'limit'=> array( 0, 99999 ))); |
@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * ------------------------------------------------------------------------ |
| 23 | 23 | */ |
| 24 | -require_once ( EE_MODELS . 'EEM_Base.model.php' ); |
|
| 25 | -require_once ( EE_CLASSES . 'EE_State.class.php' ); |
|
| 24 | +require_once (EE_MODELS.'EEM_Base.model.php'); |
|
| 25 | +require_once (EE_CLASSES.'EE_State.class.php'); |
|
| 26 | 26 | |
| 27 | 27 | class EEM_State extends EEM_Base { |
| 28 | 28 | |
@@ -33,9 +33,9 @@ discard block |
||
| 33 | 33 | // array of all active states |
| 34 | 34 | private static $_active_states = FALSE; |
| 35 | 35 | |
| 36 | - protected function __construct( $timezone = NULL ) { |
|
| 37 | - $this->singular_item = __('State/Province','event_espresso'); |
|
| 38 | - $this->plural_item = __('States/Provinces','event_espresso'); |
|
| 36 | + protected function __construct($timezone = NULL) { |
|
| 37 | + $this->singular_item = __('State/Province', 'event_espresso'); |
|
| 38 | + $this->plural_item = __('States/Provinces', 'event_espresso'); |
|
| 39 | 39 | |
| 40 | 40 | $this->_tables = array( |
| 41 | 41 | 'State'=> new EE_Primary_Table('esp_state', 'STA_ID') |
@@ -43,10 +43,10 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | $this->_fields = array( |
| 45 | 45 | 'State'=>array( |
| 46 | - 'STA_ID'=> new EE_Primary_Key_Int_Field('STA_ID', __('State ID','event_espresso')), |
|
| 47 | - 'CNT_ISO'=> new EE_Foreign_Key_String_Field('CNT_ISO', __('Country ISO Code','event_espresso'), false, NULL, 'Country'), |
|
| 48 | - 'STA_abbrev' => new EE_Plain_Text_Field('STA_abbrev', __('State Abbreviation','event_espresso'), false, ''), |
|
| 49 | - 'STA_name' => new EE_Plain_Text_Field('STA_name', __('State Name','event_espresso'), false, ''), |
|
| 46 | + 'STA_ID'=> new EE_Primary_Key_Int_Field('STA_ID', __('State ID', 'event_espresso')), |
|
| 47 | + 'CNT_ISO'=> new EE_Foreign_Key_String_Field('CNT_ISO', __('Country ISO Code', 'event_espresso'), false, NULL, 'Country'), |
|
| 48 | + 'STA_abbrev' => new EE_Plain_Text_Field('STA_abbrev', __('State Abbreviation', 'event_espresso'), false, ''), |
|
| 49 | + 'STA_name' => new EE_Plain_Text_Field('STA_name', __('State Name', 'event_espresso'), false, ''), |
|
| 50 | 50 | 'STA_active'=> new EE_Boolean_Field('STA_active', __('State Active Flag', 'event_espresso'), false, false) |
| 51 | 51 | )); |
| 52 | 52 | $this->_model_relations = array( |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | 'Venue'=>new EE_Has_Many_Relation(), |
| 56 | 56 | ); |
| 57 | 57 | //this model is generally available for reading |
| 58 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 58 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
| 59 | 59 | //@todo: only show STA_active |
| 60 | - parent::__construct( $timezone ); |
|
| 60 | + parent::__construct($timezone); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | |
@@ -84,8 +84,8 @@ discard block |
||
| 84 | 84 | * @return array |
| 85 | 85 | */ |
| 86 | 86 | public function get_all_states() { |
| 87 | - if ( ! self::$_all_states ) { |
|
| 88 | - self::$_all_states = $this->get_all( array( 'order_by'=>array( 'STA_name'=>'ASC' ), 'limit'=> array( 0, 99999 ))); |
|
| 87 | + if ( ! self::$_all_states) { |
|
| 88 | + self::$_all_states = $this->get_all(array('order_by'=>array('STA_name'=>'ASC'), 'limit'=> array(0, 99999))); |
|
| 89 | 89 | } |
| 90 | 90 | return self::$_all_states; |
| 91 | 91 | } |
@@ -100,14 +100,14 @@ discard block |
||
| 100 | 100 | * @param bool $flush_cache |
| 101 | 101 | * @return array |
| 102 | 102 | */ |
| 103 | - public function get_all_active_states( $countries = array(), $flush_cache = FALSE ) { |
|
| 104 | - if ( ! self::$_active_states || $flush_cache ) { |
|
| 105 | - $countries = is_array( $countries ) && ! empty( $countries ) ? $countries : EEM_Country::instance()->get_all_active_countries(); |
|
| 106 | - self::$_active_states = $this->get_all( array( |
|
| 107 | - array( 'STA_active' => TRUE, 'CNT_ISO' => array( 'IN', array_keys( $countries ))), |
|
| 108 | - 'order_by' => array( 'STA_name'=>'ASC' ), |
|
| 109 | - 'limit' => array( 0, 99999 ), |
|
| 110 | - 'force_join' => array( 'Country' ) |
|
| 103 | + public function get_all_active_states($countries = array(), $flush_cache = FALSE) { |
|
| 104 | + if ( ! self::$_active_states || $flush_cache) { |
|
| 105 | + $countries = is_array($countries) && ! empty($countries) ? $countries : EEM_Country::instance()->get_all_active_countries(); |
|
| 106 | + self::$_active_states = $this->get_all(array( |
|
| 107 | + array('STA_active' => TRUE, 'CNT_ISO' => array('IN', array_keys($countries))), |
|
| 108 | + 'order_by' => array('STA_name'=>'ASC'), |
|
| 109 | + 'limit' => array(0, 99999), |
|
| 110 | + 'force_join' => array('Country') |
|
| 111 | 111 | )); |
| 112 | 112 | } |
| 113 | 113 | return self::$_active_states; |
@@ -119,8 +119,8 @@ discard block |
||
| 119 | 119 | * get_all_states_of_active_countries |
| 120 | 120 | * @return array |
| 121 | 121 | */ |
| 122 | - public function get_all_states_of_active_countries(){ |
|
| 123 | - if ( $states = $this->get_all( array( array( 'Country.CNT_active' => TRUE, 'STA_active' => TRUE ), 'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) { |
|
| 122 | + public function get_all_states_of_active_countries() { |
|
| 123 | + if ($states = $this->get_all(array(array('Country.CNT_active' => TRUE, 'STA_active' => TRUE), 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC')))) { |
|
| 124 | 124 | return $states; |
| 125 | 125 | } |
| 126 | 126 | return FALSE; |
@@ -132,11 +132,11 @@ discard block |
||
| 132 | 132 | * get_all_states_of_active_countries |
| 133 | 133 | * @return array |
| 134 | 134 | */ |
| 135 | - public function get_all_active_states_for_these_countries( $countries ){ |
|
| 136 | - if ( ! $countries ) { |
|
| 135 | + public function get_all_active_states_for_these_countries($countries) { |
|
| 136 | + if ( ! $countries) { |
|
| 137 | 137 | return FALSE; |
| 138 | 138 | } |
| 139 | - if ( $states = $this->get_all( array( array( 'Country.CNT_ISO' => array( 'IN', array_keys( $countries )), 'STA_active' => TRUE ), 'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) { |
|
| 139 | + if ($states = $this->get_all(array(array('Country.CNT_ISO' => array('IN', array_keys($countries)), 'STA_active' => TRUE), 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC')))) { |
|
| 140 | 140 | return $states; |
| 141 | 141 | } |
| 142 | 142 | return FALSE; |
@@ -148,11 +148,11 @@ discard block |
||
| 148 | 148 | * get_all_states_of_active_countries |
| 149 | 149 | * @return array |
| 150 | 150 | */ |
| 151 | - public function get_all_states_for_these_countries( $countries ){ |
|
| 152 | - if ( ! $countries ) { |
|
| 151 | + public function get_all_states_for_these_countries($countries) { |
|
| 152 | + if ( ! $countries) { |
|
| 153 | 153 | return FALSE; |
| 154 | 154 | } |
| 155 | - if ( $states = $this->get_all( array( array( 'Country.CNT_ISO' => array( 'IN', array_keys( $countries ))), 'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) { |
|
| 155 | + if ($states = $this->get_all(array(array('Country.CNT_ISO' => array('IN', array_keys($countries))), 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC')))) { |
|
| 156 | 156 | return $states; |
| 157 | 157 | } |
| 158 | 158 | return FALSE; |
@@ -163,15 +163,15 @@ discard block |
||
| 163 | 163 | * @param string $state_ID |
| 164 | 164 | * @return string |
| 165 | 165 | */ |
| 166 | - public function get_state_name_by_ID( $state_ID ){ |
|
| 167 | - if( isset( self::$_all_states[ $state_ID ] ) && |
|
| 168 | - self::$_all_states[ $state_ID ] instanceof EE_State ){ |
|
| 169 | - return self::$_all_states[ $state_ID ]->name(); |
|
| 166 | + public function get_state_name_by_ID($state_ID) { |
|
| 167 | + if (isset(self::$_all_states[$state_ID]) && |
|
| 168 | + self::$_all_states[$state_ID] instanceof EE_State) { |
|
| 169 | + return self::$_all_states[$state_ID]->name(); |
|
| 170 | 170 | } |
| 171 | - $names = $this->get_col( array( array( 'STA_ID' => $state_ID ), 'limit' => 1), 'STA_name' ); |
|
| 172 | - if( is_array( $names ) && ! empty( $names ) ){ |
|
| 173 | - return reset( $names ); |
|
| 174 | - }else{ |
|
| 171 | + $names = $this->get_col(array(array('STA_ID' => $state_ID), 'limit' => 1), 'STA_name'); |
|
| 172 | + if (is_array($names) && ! empty($names)) { |
|
| 173 | + return reset($names); |
|
| 174 | + } else { |
|
| 175 | 175 | return ''; |
| 176 | 176 | } |
| 177 | 177 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * @param string $word |
| 55 | - * @return mixed |
|
| 55 | + * @return string |
|
| 56 | 56 | */ |
| 57 | 57 | public static function singularize_and_upper( $word ) { |
| 58 | 58 | return str_replace( ' ', '_', self::humanize( self::singularize( $word ), 'all' ) ); |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | /** |
| 243 | 243 | * Camelizes all but the first word. This is handy converting a method which followed EE4 legacy naming convention |
| 244 | 244 | * with the new PSR-based naming conventions |
| 245 | - * @param $word |
|
| 245 | + * @param string $word |
|
| 246 | 246 | * @return string |
| 247 | 247 | */ |
| 248 | 248 | public static function camelize_all_but_first($word){ |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | |
| 391 | 391 | |
| 392 | 392 | /** |
| 393 | - * @param $string |
|
| 393 | + * @param string $string |
|
| 394 | 394 | * @return string |
| 395 | 395 | */ |
| 396 | 396 | public static function add_indefinite_article($string) { |
@@ -35,368 +35,368 @@ |
||
| 35 | 35 | * @version $Revision 0.1 $ |
| 36 | 36 | */ |
| 37 | 37 | class EEH_Inflector{ |
| 38 | - // ------ CLASS METHODS ------ // |
|
| 39 | - // ---- Public methods ---- // |
|
| 40 | - // {{{ pluralize() |
|
| 38 | + // ------ CLASS METHODS ------ // |
|
| 39 | + // ---- Public methods ---- // |
|
| 40 | + // {{{ pluralize() |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Just calls self::pluralize and strtolower on $word and returns it |
| 44 | 44 | * @param string $word |
| 45 | 45 | * @return string |
| 46 | 46 | */ |
| 47 | - public static function pluralize_and_lower( $word ){ |
|
| 47 | + public static function pluralize_and_lower( $word ){ |
|
| 48 | 48 | return strtolower( self::pluralize( $word ) ); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | |
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @param string $word |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - public static function singularize_and_upper( $word ) { |
|
| 53 | + /** |
|
| 54 | + * @param string $word |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + public static function singularize_and_upper( $word ) { |
|
| 58 | 58 | return str_replace( ' ', '_', self::humanize( self::singularize( $word ), 'all' ) ); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Pluralizes English nouns. |
|
| 65 | - * |
|
| 66 | - * @access public |
|
| 67 | - * @static |
|
| 68 | - * @param string $word English noun to pluralize |
|
| 69 | - * @return string Plural noun |
|
| 70 | - */ |
|
| 71 | - public static function pluralize($word){ |
|
| 72 | - $plural = array( |
|
| 73 | - '/(quiz)$/i' => '\1zes', |
|
| 74 | - '/^(ox)$/i' => '\1en', |
|
| 75 | - '/([m|l])ouse$/i' => '\1ice', |
|
| 76 | - '/(matr|vert|ind)ix|ex$/i' => '\1ices', |
|
| 77 | - '/(x|ch|ss|sh)$/i' => '\1es', |
|
| 78 | - '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
| 79 | - '/([^aeiouy]|qu)y$/i' => '\1ies', |
|
| 80 | - '/(hive)$/i' => '\1s', |
|
| 81 | - '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', |
|
| 82 | - '/sis$/i' => 'ses', |
|
| 83 | - '/([ti])um$/i' => '\1a', |
|
| 84 | - '/(buffal|tomat)o$/i' => '\1oes', |
|
| 85 | - '/(bu)s$/i' => '\1ses', |
|
| 86 | - '/(alias|status)/i' => '\1es', |
|
| 87 | - '/(octop|vir)us$/i' => '\1i', |
|
| 88 | - '/(ax|test)is$/i' => '\1es', |
|
| 89 | - '/s$/i' => 's', |
|
| 90 | - '/$/' => 's'); |
|
| 91 | - |
|
| 92 | - $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
| 93 | - |
|
| 94 | - $irregular = array( |
|
| 95 | - 'person' => 'people', |
|
| 96 | - 'man' => 'men', |
|
| 97 | - 'child' => 'children', |
|
| 98 | - 'sex' => 'sexes', |
|
| 99 | - 'move' => 'moves'); |
|
| 100 | - |
|
| 101 | - $lowercased_word = strtolower($word); |
|
| 102 | - |
|
| 103 | - foreach($uncountable as $_uncountable){ |
|
| 104 | - if(substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && //even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
|
| 63 | + /** |
|
| 64 | + * Pluralizes English nouns. |
|
| 65 | + * |
|
| 66 | + * @access public |
|
| 67 | + * @static |
|
| 68 | + * @param string $word English noun to pluralize |
|
| 69 | + * @return string Plural noun |
|
| 70 | + */ |
|
| 71 | + public static function pluralize($word){ |
|
| 72 | + $plural = array( |
|
| 73 | + '/(quiz)$/i' => '\1zes', |
|
| 74 | + '/^(ox)$/i' => '\1en', |
|
| 75 | + '/([m|l])ouse$/i' => '\1ice', |
|
| 76 | + '/(matr|vert|ind)ix|ex$/i' => '\1ices', |
|
| 77 | + '/(x|ch|ss|sh)$/i' => '\1es', |
|
| 78 | + '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
| 79 | + '/([^aeiouy]|qu)y$/i' => '\1ies', |
|
| 80 | + '/(hive)$/i' => '\1s', |
|
| 81 | + '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', |
|
| 82 | + '/sis$/i' => 'ses', |
|
| 83 | + '/([ti])um$/i' => '\1a', |
|
| 84 | + '/(buffal|tomat)o$/i' => '\1oes', |
|
| 85 | + '/(bu)s$/i' => '\1ses', |
|
| 86 | + '/(alias|status)/i' => '\1es', |
|
| 87 | + '/(octop|vir)us$/i' => '\1i', |
|
| 88 | + '/(ax|test)is$/i' => '\1es', |
|
| 89 | + '/s$/i' => 's', |
|
| 90 | + '/$/' => 's'); |
|
| 91 | + |
|
| 92 | + $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
| 93 | + |
|
| 94 | + $irregular = array( |
|
| 95 | + 'person' => 'people', |
|
| 96 | + 'man' => 'men', |
|
| 97 | + 'child' => 'children', |
|
| 98 | + 'sex' => 'sexes', |
|
| 99 | + 'move' => 'moves'); |
|
| 100 | + |
|
| 101 | + $lowercased_word = strtolower($word); |
|
| 102 | + |
|
| 103 | + foreach($uncountable as $_uncountable){ |
|
| 104 | + if(substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && //even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
|
| 105 | 105 | ! ctype_alpha( $lowercased_word[ strlen( $lowercased_word ) - strlen($_uncountable) ] ) ){ |
| 106 | - return $word; |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - foreach($irregular as $_plural => $_singular){ |
|
| 111 | - if(preg_match('/(' . $_plural . ')$/i', $word, $arr)){ |
|
| 112 | - return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - foreach($plural as $rule => $replacement){ |
|
| 117 | - if(preg_match($rule, $word)){ |
|
| 118 | - return preg_replace($rule, $replacement, $word); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - return false; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - // }}} |
|
| 125 | - // {{{ singularize() |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Singularizes English nouns. |
|
| 129 | - * |
|
| 130 | - * @access public |
|
| 131 | - * @static |
|
| 132 | - * @param string $word English noun to singularize |
|
| 133 | - * @return string Singular noun. |
|
| 134 | - */ |
|
| 135 | - public static function singularize($word){ |
|
| 136 | - $singular = array( |
|
| 137 | - '/(quiz)zes$/i' => '\1', |
|
| 138 | - '/(matr)ices$/i' => '\1ix', |
|
| 139 | - '/(vert|ind)ices$/i' => '\1ex', |
|
| 140 | - '/^(ox)en/i' => '\1', |
|
| 141 | - '/(alias|status)es$/i' => '\1', |
|
| 142 | - '/([octop|vir])i$/i' => '\1us', |
|
| 143 | - '/(cris|ax|test)es$/i' => '\1is', |
|
| 144 | - '/(shoe)s$/i' => '\1', |
|
| 145 | - '/(o)es$/i' => '\1', |
|
| 146 | - '/(bus)es$/i' => '\1', |
|
| 147 | - '/([m|l])ice$/i' => '\1ouse', |
|
| 148 | - '/(x|ch|ss|sh)es$/i' => '\1', |
|
| 149 | - '/(m)ovies$/i' => '\1ovie', |
|
| 150 | - '/(s)eries$/i' => '\1eries', |
|
| 151 | - '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
| 152 | - '/([lr])ves$/i' => '\1f', |
|
| 153 | - '/(tive)s$/i' => '\1', |
|
| 154 | - '/(hive)s$/i' => '\1', |
|
| 155 | - '/([^f])ves$/i' => '\1fe', |
|
| 156 | - '/(^analy)ses$/i' => '\1sis', |
|
| 157 | - '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
|
| 158 | - '/([ti])a$/i' => '\1um', |
|
| 159 | - '/(n)ews$/i' => '\1ews', |
|
| 160 | - '/s$/i' => '', |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
| 164 | - |
|
| 165 | - $irregular = array( |
|
| 166 | - 'person' => 'people', |
|
| 167 | - 'man' => 'men', |
|
| 168 | - 'child' => 'children', |
|
| 169 | - 'sex' => 'sexes', |
|
| 170 | - 'move' => 'moves'); |
|
| 171 | - |
|
| 172 | - $lowercased_word = strtolower($word); |
|
| 173 | - foreach($uncountable as $_uncountable){ |
|
| 174 | - if(substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable){ |
|
| 175 | - return $word; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - foreach($irregular as $_plural => $_singular){ |
|
| 180 | - if(preg_match('/(' . $_singular . ')$/i', $word, $arr)){ |
|
| 181 | - return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - foreach($singular as $rule => $replacement){ |
|
| 186 | - if(preg_match($rule, $word)){ |
|
| 187 | - return preg_replace($rule, $replacement, $word); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $word; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // }}} |
|
| 195 | - // {{{ titleize() |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Converts an underscored or CamelCase word into a English |
|
| 199 | - * sentence. |
|
| 200 | - * |
|
| 201 | - * The titleize static function converts text like "WelcomePage", |
|
| 202 | - * "welcome_page" or "welcome page" to this "Welcome |
|
| 203 | - * Page". |
|
| 204 | - * If second parameter is set to 'first' it will only |
|
| 205 | - * capitalize the first character of the title. |
|
| 206 | - * |
|
| 207 | - * @access public |
|
| 208 | - * @static |
|
| 209 | - * @param string $word Word to format as tile |
|
| 210 | - * @param string $uppercase If set to 'first' it will only uppercase the |
|
| 211 | - * first character. Otherwise it will uppercase all |
|
| 212 | - * the words in the title. |
|
| 213 | - * @return string Text formatted as title |
|
| 214 | - */ |
|
| 215 | - public static function titleize($word, $uppercase = ''){ |
|
| 216 | - $uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords'; |
|
| 217 | - return $uppercase(EEH_Inflector::humanize(EEH_Inflector::underscore($word))); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - // }}} |
|
| 221 | - // {{{ camelize() |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Returns given word as CamelCased |
|
| 225 | - * |
|
| 226 | - * Converts a word like "send_email" to "SendEmail". It |
|
| 227 | - * will remove non alphanumeric character from the word, so |
|
| 228 | - * "who's online" will be converted to "WhoSOnline" |
|
| 229 | - * |
|
| 230 | - * @access public |
|
| 231 | - * @static |
|
| 232 | - * @see variablize |
|
| 233 | - * @param string $word Word to convert to camel case |
|
| 234 | - * @return string UpperCamelCasedWord |
|
| 235 | - */ |
|
| 236 | - public static function camelize($word){ |
|
| 237 | - return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Camelizes all but the first word. This is handy converting a method which followed EE4 legacy naming convention |
|
| 244 | - * with the new PSR-based naming conventions |
|
| 245 | - * @param $word |
|
| 246 | - * @return string |
|
| 247 | - */ |
|
| 248 | - public static function camelize_all_but_first($word){ |
|
| 249 | - return lcfirst(EEH_Inflector::camelize($word)); |
|
| 250 | - } |
|
| 251 | - // }}} |
|
| 252 | - // {{{ underscore() |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Converts a word "into_it_s_underscored_version" |
|
| 256 | - * |
|
| 257 | - * Convert any "CamelCased" or "ordinary Word" into an |
|
| 258 | - * "underscored_word". |
|
| 259 | - * |
|
| 260 | - * This can be really useful for creating friendly URLs. |
|
| 261 | - * |
|
| 262 | - * @access public |
|
| 263 | - * @static |
|
| 264 | - * @param string $word Word to underscore |
|
| 265 | - * @return string Underscored word |
|
| 266 | - */ |
|
| 267 | - public static function underscore($word){ |
|
| 268 | - return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/', '_', preg_replace('/([a-zd])([A-Z])/', '1_2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word)))); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // }}} |
|
| 272 | - // {{{ humanize() |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Returns a human-readable string from $word |
|
| 276 | - * |
|
| 277 | - * Returns a human-readable string from $word, by replacing |
|
| 278 | - * underscores with a space, and by upper-casing the initial |
|
| 279 | - * character by default. |
|
| 280 | - * |
|
| 281 | - * If you need to uppercase all the words you just have to |
|
| 282 | - * pass 'all' as a second parameter. |
|
| 283 | - * |
|
| 284 | - * @access public |
|
| 285 | - * @static |
|
| 286 | - * @param string $word String to "humanize" |
|
| 287 | - * @param string $uppercase If set to 'all' it will uppercase all the words |
|
| 288 | - * instead of just the first one. |
|
| 289 | - * @return string Human-readable word |
|
| 290 | - */ |
|
| 291 | - public static function humanize($word, $uppercase = ''){ |
|
| 106 | + return $word; |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + foreach($irregular as $_plural => $_singular){ |
|
| 111 | + if(preg_match('/(' . $_plural . ')$/i', $word, $arr)){ |
|
| 112 | + return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + foreach($plural as $rule => $replacement){ |
|
| 117 | + if(preg_match($rule, $word)){ |
|
| 118 | + return preg_replace($rule, $replacement, $word); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + return false; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + // }}} |
|
| 125 | + // {{{ singularize() |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Singularizes English nouns. |
|
| 129 | + * |
|
| 130 | + * @access public |
|
| 131 | + * @static |
|
| 132 | + * @param string $word English noun to singularize |
|
| 133 | + * @return string Singular noun. |
|
| 134 | + */ |
|
| 135 | + public static function singularize($word){ |
|
| 136 | + $singular = array( |
|
| 137 | + '/(quiz)zes$/i' => '\1', |
|
| 138 | + '/(matr)ices$/i' => '\1ix', |
|
| 139 | + '/(vert|ind)ices$/i' => '\1ex', |
|
| 140 | + '/^(ox)en/i' => '\1', |
|
| 141 | + '/(alias|status)es$/i' => '\1', |
|
| 142 | + '/([octop|vir])i$/i' => '\1us', |
|
| 143 | + '/(cris|ax|test)es$/i' => '\1is', |
|
| 144 | + '/(shoe)s$/i' => '\1', |
|
| 145 | + '/(o)es$/i' => '\1', |
|
| 146 | + '/(bus)es$/i' => '\1', |
|
| 147 | + '/([m|l])ice$/i' => '\1ouse', |
|
| 148 | + '/(x|ch|ss|sh)es$/i' => '\1', |
|
| 149 | + '/(m)ovies$/i' => '\1ovie', |
|
| 150 | + '/(s)eries$/i' => '\1eries', |
|
| 151 | + '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
| 152 | + '/([lr])ves$/i' => '\1f', |
|
| 153 | + '/(tive)s$/i' => '\1', |
|
| 154 | + '/(hive)s$/i' => '\1', |
|
| 155 | + '/([^f])ves$/i' => '\1fe', |
|
| 156 | + '/(^analy)ses$/i' => '\1sis', |
|
| 157 | + '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
|
| 158 | + '/([ti])a$/i' => '\1um', |
|
| 159 | + '/(n)ews$/i' => '\1ews', |
|
| 160 | + '/s$/i' => '', |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
| 164 | + |
|
| 165 | + $irregular = array( |
|
| 166 | + 'person' => 'people', |
|
| 167 | + 'man' => 'men', |
|
| 168 | + 'child' => 'children', |
|
| 169 | + 'sex' => 'sexes', |
|
| 170 | + 'move' => 'moves'); |
|
| 171 | + |
|
| 172 | + $lowercased_word = strtolower($word); |
|
| 173 | + foreach($uncountable as $_uncountable){ |
|
| 174 | + if(substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable){ |
|
| 175 | + return $word; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + foreach($irregular as $_plural => $_singular){ |
|
| 180 | + if(preg_match('/(' . $_singular . ')$/i', $word, $arr)){ |
|
| 181 | + return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + foreach($singular as $rule => $replacement){ |
|
| 186 | + if(preg_match($rule, $word)){ |
|
| 187 | + return preg_replace($rule, $replacement, $word); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $word; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // }}} |
|
| 195 | + // {{{ titleize() |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Converts an underscored or CamelCase word into a English |
|
| 199 | + * sentence. |
|
| 200 | + * |
|
| 201 | + * The titleize static function converts text like "WelcomePage", |
|
| 202 | + * "welcome_page" or "welcome page" to this "Welcome |
|
| 203 | + * Page". |
|
| 204 | + * If second parameter is set to 'first' it will only |
|
| 205 | + * capitalize the first character of the title. |
|
| 206 | + * |
|
| 207 | + * @access public |
|
| 208 | + * @static |
|
| 209 | + * @param string $word Word to format as tile |
|
| 210 | + * @param string $uppercase If set to 'first' it will only uppercase the |
|
| 211 | + * first character. Otherwise it will uppercase all |
|
| 212 | + * the words in the title. |
|
| 213 | + * @return string Text formatted as title |
|
| 214 | + */ |
|
| 215 | + public static function titleize($word, $uppercase = ''){ |
|
| 216 | + $uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords'; |
|
| 217 | + return $uppercase(EEH_Inflector::humanize(EEH_Inflector::underscore($word))); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + // }}} |
|
| 221 | + // {{{ camelize() |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Returns given word as CamelCased |
|
| 225 | + * |
|
| 226 | + * Converts a word like "send_email" to "SendEmail". It |
|
| 227 | + * will remove non alphanumeric character from the word, so |
|
| 228 | + * "who's online" will be converted to "WhoSOnline" |
|
| 229 | + * |
|
| 230 | + * @access public |
|
| 231 | + * @static |
|
| 232 | + * @see variablize |
|
| 233 | + * @param string $word Word to convert to camel case |
|
| 234 | + * @return string UpperCamelCasedWord |
|
| 235 | + */ |
|
| 236 | + public static function camelize($word){ |
|
| 237 | + return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Camelizes all but the first word. This is handy converting a method which followed EE4 legacy naming convention |
|
| 244 | + * with the new PSR-based naming conventions |
|
| 245 | + * @param $word |
|
| 246 | + * @return string |
|
| 247 | + */ |
|
| 248 | + public static function camelize_all_but_first($word){ |
|
| 249 | + return lcfirst(EEH_Inflector::camelize($word)); |
|
| 250 | + } |
|
| 251 | + // }}} |
|
| 252 | + // {{{ underscore() |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Converts a word "into_it_s_underscored_version" |
|
| 256 | + * |
|
| 257 | + * Convert any "CamelCased" or "ordinary Word" into an |
|
| 258 | + * "underscored_word". |
|
| 259 | + * |
|
| 260 | + * This can be really useful for creating friendly URLs. |
|
| 261 | + * |
|
| 262 | + * @access public |
|
| 263 | + * @static |
|
| 264 | + * @param string $word Word to underscore |
|
| 265 | + * @return string Underscored word |
|
| 266 | + */ |
|
| 267 | + public static function underscore($word){ |
|
| 268 | + return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/', '_', preg_replace('/([a-zd])([A-Z])/', '1_2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word)))); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // }}} |
|
| 272 | + // {{{ humanize() |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Returns a human-readable string from $word |
|
| 276 | + * |
|
| 277 | + * Returns a human-readable string from $word, by replacing |
|
| 278 | + * underscores with a space, and by upper-casing the initial |
|
| 279 | + * character by default. |
|
| 280 | + * |
|
| 281 | + * If you need to uppercase all the words you just have to |
|
| 282 | + * pass 'all' as a second parameter. |
|
| 283 | + * |
|
| 284 | + * @access public |
|
| 285 | + * @static |
|
| 286 | + * @param string $word String to "humanize" |
|
| 287 | + * @param string $uppercase If set to 'all' it will uppercase all the words |
|
| 288 | + * instead of just the first one. |
|
| 289 | + * @return string Human-readable word |
|
| 290 | + */ |
|
| 291 | + public static function humanize($word, $uppercase = ''){ |
|
| 292 | 292 | //make special exceptions for acronyms |
| 293 | 293 | $word = str_replace('wp_', 'WP_', $word ); |
| 294 | - $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst'; |
|
| 295 | - return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word))); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // }}} |
|
| 299 | - // {{{ variablize() |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Same as camelize but first char is underscored |
|
| 303 | - * |
|
| 304 | - * Converts a word like "send_email" to "sendEmail". It |
|
| 305 | - * will remove non alphanumeric character from the word, so |
|
| 306 | - * "who's online" will be converted to "whoSOnline" |
|
| 307 | - * |
|
| 308 | - * @access public |
|
| 309 | - * @static |
|
| 310 | - * @see camelize |
|
| 311 | - * @param string $word Word to lowerCamelCase |
|
| 312 | - * @return string Returns a lowerCamelCasedWord |
|
| 313 | - */ |
|
| 314 | - public static function variablize($word){ |
|
| 315 | - $word = EEH_Inflector::camelize($word); |
|
| 316 | - return strtolower($word[0]) . substr($word, 1); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // }}} |
|
| 320 | - // {{{ tableize() |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Converts a class name to its table name according to rails |
|
| 324 | - * naming conventions. |
|
| 325 | - * |
|
| 326 | - * Converts "Person" to "people" |
|
| 327 | - * |
|
| 328 | - * @access public |
|
| 329 | - * @static |
|
| 330 | - * @see classify |
|
| 331 | - * @param string $class_name Class name for getting related table_name. |
|
| 332 | - * @return string plural_table_name |
|
| 333 | - */ |
|
| 334 | - public static function tableize($class_name){ |
|
| 335 | - return EEH_Inflector::pluralize(EEH_Inflector::underscore($class_name)); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - // }}} |
|
| 339 | - // {{{ classify() |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Converts a table name to its class name according to rails |
|
| 343 | - * naming conventions. |
|
| 344 | - * |
|
| 345 | - * Converts "people" to "Person" |
|
| 346 | - * |
|
| 347 | - * @access public |
|
| 348 | - * @static |
|
| 349 | - * @see tableize |
|
| 350 | - * @param string $table_name Table name for getting related ClassName. |
|
| 351 | - * @return string SingularClassName |
|
| 352 | - */ |
|
| 353 | - public static function classify($table_name){ |
|
| 354 | - return EEH_Inflector::camelize(EEH_Inflector::singularize($table_name)); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - // }}} |
|
| 358 | - // {{{ ordinalize() |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Converts number to its ordinal English form. |
|
| 362 | - * |
|
| 363 | - * This method converts 13 to 13th, 2 to 2nd ... |
|
| 364 | - * |
|
| 365 | - * @access public |
|
| 366 | - * @static |
|
| 367 | - * @param integer $number Number to get its ordinal value |
|
| 368 | - * @return string Ordinal representation of given string. |
|
| 369 | - */ |
|
| 370 | - public static function ordinalize($number){ |
|
| 371 | - if(in_array(($number % 100), range(11, 13))){ |
|
| 372 | - return $number . 'th'; |
|
| 373 | - }else{ |
|
| 374 | - switch(($number % 10)){ |
|
| 375 | - case 1: |
|
| 376 | - return $number . 'st'; |
|
| 377 | - break; |
|
| 378 | - case 2: |
|
| 379 | - return $number . 'nd'; |
|
| 380 | - break; |
|
| 381 | - case 3: |
|
| 382 | - return $number . 'rd'; |
|
| 383 | - default: |
|
| 384 | - return $number . 'th'; |
|
| 385 | - break; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * @param $string |
|
| 394 | - * @return string |
|
| 395 | - */ |
|
| 396 | - public static function add_indefinite_article($string) { |
|
| 397 | - if (strtolower($string) === 'null') { |
|
| 398 | - return $string; |
|
| 399 | - } |
|
| 400 | - return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string; |
|
| 401 | - } |
|
| 294 | + $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst'; |
|
| 295 | + return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word))); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // }}} |
|
| 299 | + // {{{ variablize() |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Same as camelize but first char is underscored |
|
| 303 | + * |
|
| 304 | + * Converts a word like "send_email" to "sendEmail". It |
|
| 305 | + * will remove non alphanumeric character from the word, so |
|
| 306 | + * "who's online" will be converted to "whoSOnline" |
|
| 307 | + * |
|
| 308 | + * @access public |
|
| 309 | + * @static |
|
| 310 | + * @see camelize |
|
| 311 | + * @param string $word Word to lowerCamelCase |
|
| 312 | + * @return string Returns a lowerCamelCasedWord |
|
| 313 | + */ |
|
| 314 | + public static function variablize($word){ |
|
| 315 | + $word = EEH_Inflector::camelize($word); |
|
| 316 | + return strtolower($word[0]) . substr($word, 1); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // }}} |
|
| 320 | + // {{{ tableize() |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Converts a class name to its table name according to rails |
|
| 324 | + * naming conventions. |
|
| 325 | + * |
|
| 326 | + * Converts "Person" to "people" |
|
| 327 | + * |
|
| 328 | + * @access public |
|
| 329 | + * @static |
|
| 330 | + * @see classify |
|
| 331 | + * @param string $class_name Class name for getting related table_name. |
|
| 332 | + * @return string plural_table_name |
|
| 333 | + */ |
|
| 334 | + public static function tableize($class_name){ |
|
| 335 | + return EEH_Inflector::pluralize(EEH_Inflector::underscore($class_name)); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + // }}} |
|
| 339 | + // {{{ classify() |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Converts a table name to its class name according to rails |
|
| 343 | + * naming conventions. |
|
| 344 | + * |
|
| 345 | + * Converts "people" to "Person" |
|
| 346 | + * |
|
| 347 | + * @access public |
|
| 348 | + * @static |
|
| 349 | + * @see tableize |
|
| 350 | + * @param string $table_name Table name for getting related ClassName. |
|
| 351 | + * @return string SingularClassName |
|
| 352 | + */ |
|
| 353 | + public static function classify($table_name){ |
|
| 354 | + return EEH_Inflector::camelize(EEH_Inflector::singularize($table_name)); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + // }}} |
|
| 358 | + // {{{ ordinalize() |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Converts number to its ordinal English form. |
|
| 362 | + * |
|
| 363 | + * This method converts 13 to 13th, 2 to 2nd ... |
|
| 364 | + * |
|
| 365 | + * @access public |
|
| 366 | + * @static |
|
| 367 | + * @param integer $number Number to get its ordinal value |
|
| 368 | + * @return string Ordinal representation of given string. |
|
| 369 | + */ |
|
| 370 | + public static function ordinalize($number){ |
|
| 371 | + if(in_array(($number % 100), range(11, 13))){ |
|
| 372 | + return $number . 'th'; |
|
| 373 | + }else{ |
|
| 374 | + switch(($number % 10)){ |
|
| 375 | + case 1: |
|
| 376 | + return $number . 'st'; |
|
| 377 | + break; |
|
| 378 | + case 2: |
|
| 379 | + return $number . 'nd'; |
|
| 380 | + break; |
|
| 381 | + case 3: |
|
| 382 | + return $number . 'rd'; |
|
| 383 | + default: |
|
| 384 | + return $number . 'th'; |
|
| 385 | + break; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * @param $string |
|
| 394 | + * @return string |
|
| 395 | + */ |
|
| 396 | + public static function add_indefinite_article($string) { |
|
| 397 | + if (strtolower($string) === 'null') { |
|
| 398 | + return $string; |
|
| 399 | + } |
|
| 400 | + return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string; |
|
| 401 | + } |
|
| 402 | 402 | } |
| 403 | 403 | \ No newline at end of file |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
| 3 | - exit( 'No direct script access allowed' ); |
|
| 2 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 3 | + exit('No direct script access allowed'); |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | // +----------------------------------------------------------------------+ |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * @since 0.1 |
| 35 | 35 | * @version $Revision 0.1 $ |
| 36 | 36 | */ |
| 37 | -class EEH_Inflector{ |
|
| 37 | +class EEH_Inflector { |
|
| 38 | 38 | // ------ CLASS METHODS ------ // |
| 39 | 39 | // ---- Public methods ---- // |
| 40 | 40 | // {{{ pluralize() |
@@ -44,8 +44,8 @@ discard block |
||
| 44 | 44 | * @param string $word |
| 45 | 45 | * @return string |
| 46 | 46 | */ |
| 47 | - public static function pluralize_and_lower( $word ){ |
|
| 48 | - return strtolower( self::pluralize( $word ) ); |
|
| 47 | + public static function pluralize_and_lower($word) { |
|
| 48 | + return strtolower(self::pluralize($word)); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | |
@@ -54,8 +54,8 @@ discard block |
||
| 54 | 54 | * @param string $word |
| 55 | 55 | * @return mixed |
| 56 | 56 | */ |
| 57 | - public static function singularize_and_upper( $word ) { |
|
| 58 | - return str_replace( ' ', '_', self::humanize( self::singularize( $word ), 'all' ) ); |
|
| 57 | + public static function singularize_and_upper($word) { |
|
| 58 | + return str_replace(' ', '_', self::humanize(self::singularize($word), 'all')); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @param string $word English noun to pluralize |
| 69 | 69 | * @return string Plural noun |
| 70 | 70 | */ |
| 71 | - public static function pluralize($word){ |
|
| 71 | + public static function pluralize($word) { |
|
| 72 | 72 | $plural = array( |
| 73 | 73 | '/(quiz)$/i' => '\1zes', |
| 74 | 74 | '/^(ox)$/i' => '\1en', |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | $lowercased_word = strtolower($word); |
| 102 | 102 | |
| 103 | - foreach($uncountable as $_uncountable){ |
|
| 104 | - if(substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && //even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
|
| 105 | - ! ctype_alpha( $lowercased_word[ strlen( $lowercased_word ) - strlen($_uncountable) ] ) ){ |
|
| 103 | + foreach ($uncountable as $_uncountable) { |
|
| 104 | + if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && //even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
|
| 105 | + ! ctype_alpha($lowercased_word[strlen($lowercased_word) - strlen($_uncountable)])) { |
|
| 106 | 106 | return $word; |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - foreach($irregular as $_plural => $_singular){ |
|
| 111 | - if(preg_match('/(' . $_plural . ')$/i', $word, $arr)){ |
|
| 112 | - return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
| 110 | + foreach ($irregular as $_plural => $_singular) { |
|
| 111 | + if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
|
| 112 | + return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word); |
|
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - foreach($plural as $rule => $replacement){ |
|
| 117 | - if(preg_match($rule, $word)){ |
|
| 116 | + foreach ($plural as $rule => $replacement) { |
|
| 117 | + if (preg_match($rule, $word)) { |
|
| 118 | 118 | return preg_replace($rule, $replacement, $word); |
| 119 | 119 | } |
| 120 | 120 | } |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | * @param string $word English noun to singularize |
| 133 | 133 | * @return string Singular noun. |
| 134 | 134 | */ |
| 135 | - public static function singularize($word){ |
|
| 135 | + public static function singularize($word) { |
|
| 136 | 136 | $singular = array( |
| 137 | 137 | '/(quiz)zes$/i' => '\1', |
| 138 | 138 | '/(matr)ices$/i' => '\1ix', |
@@ -170,20 +170,20 @@ discard block |
||
| 170 | 170 | 'move' => 'moves'); |
| 171 | 171 | |
| 172 | 172 | $lowercased_word = strtolower($word); |
| 173 | - foreach($uncountable as $_uncountable){ |
|
| 174 | - if(substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable){ |
|
| 173 | + foreach ($uncountable as $_uncountable) { |
|
| 174 | + if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
| 175 | 175 | return $word; |
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - foreach($irregular as $_plural => $_singular){ |
|
| 180 | - if(preg_match('/(' . $_singular . ')$/i', $word, $arr)){ |
|
| 181 | - return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
|
| 179 | + foreach ($irregular as $_plural => $_singular) { |
|
| 180 | + if (preg_match('/('.$_singular.')$/i', $word, $arr)) { |
|
| 181 | + return preg_replace('/('.$_singular.')$/i', substr($arr[0], 0, 1).substr($_plural, 1), $word); |
|
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - foreach($singular as $rule => $replacement){ |
|
| 186 | - if(preg_match($rule, $word)){ |
|
| 185 | + foreach ($singular as $rule => $replacement) { |
|
| 186 | + if (preg_match($rule, $word)) { |
|
| 187 | 187 | return preg_replace($rule, $replacement, $word); |
| 188 | 188 | } |
| 189 | 189 | } |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * the words in the title. |
| 213 | 213 | * @return string Text formatted as title |
| 214 | 214 | */ |
| 215 | - public static function titleize($word, $uppercase = ''){ |
|
| 215 | + public static function titleize($word, $uppercase = '') { |
|
| 216 | 216 | $uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords'; |
| 217 | 217 | return $uppercase(EEH_Inflector::humanize(EEH_Inflector::underscore($word))); |
| 218 | 218 | } |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | * @param string $word Word to convert to camel case |
| 234 | 234 | * @return string UpperCamelCasedWord |
| 235 | 235 | */ |
| 236 | - public static function camelize($word){ |
|
| 236 | + public static function camelize($word) { |
|
| 237 | 237 | return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); |
| 238 | 238 | } |
| 239 | 239 | |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | * @param $word |
| 246 | 246 | * @return string |
| 247 | 247 | */ |
| 248 | - public static function camelize_all_but_first($word){ |
|
| 248 | + public static function camelize_all_but_first($word) { |
|
| 249 | 249 | return lcfirst(EEH_Inflector::camelize($word)); |
| 250 | 250 | } |
| 251 | 251 | // }}} |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | * @param string $word Word to underscore |
| 265 | 265 | * @return string Underscored word |
| 266 | 266 | */ |
| 267 | - public static function underscore($word){ |
|
| 267 | + public static function underscore($word) { |
|
| 268 | 268 | return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/', '_', preg_replace('/([a-zd])([A-Z])/', '1_2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word)))); |
| 269 | 269 | } |
| 270 | 270 | |
@@ -288,9 +288,9 @@ discard block |
||
| 288 | 288 | * instead of just the first one. |
| 289 | 289 | * @return string Human-readable word |
| 290 | 290 | */ |
| 291 | - public static function humanize($word, $uppercase = ''){ |
|
| 291 | + public static function humanize($word, $uppercase = '') { |
|
| 292 | 292 | //make special exceptions for acronyms |
| 293 | - $word = str_replace('wp_', 'WP_', $word ); |
|
| 293 | + $word = str_replace('wp_', 'WP_', $word); |
|
| 294 | 294 | $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst'; |
| 295 | 295 | return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word))); |
| 296 | 296 | } |
@@ -311,9 +311,9 @@ discard block |
||
| 311 | 311 | * @param string $word Word to lowerCamelCase |
| 312 | 312 | * @return string Returns a lowerCamelCasedWord |
| 313 | 313 | */ |
| 314 | - public static function variablize($word){ |
|
| 314 | + public static function variablize($word) { |
|
| 315 | 315 | $word = EEH_Inflector::camelize($word); |
| 316 | - return strtolower($word[0]) . substr($word, 1); |
|
| 316 | + return strtolower($word[0]).substr($word, 1); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | // }}} |
@@ -331,7 +331,7 @@ discard block |
||
| 331 | 331 | * @param string $class_name Class name for getting related table_name. |
| 332 | 332 | * @return string plural_table_name |
| 333 | 333 | */ |
| 334 | - public static function tableize($class_name){ |
|
| 334 | + public static function tableize($class_name) { |
|
| 335 | 335 | return EEH_Inflector::pluralize(EEH_Inflector::underscore($class_name)); |
| 336 | 336 | } |
| 337 | 337 | |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | * @param string $table_name Table name for getting related ClassName. |
| 351 | 351 | * @return string SingularClassName |
| 352 | 352 | */ |
| 353 | - public static function classify($table_name){ |
|
| 353 | + public static function classify($table_name) { |
|
| 354 | 354 | return EEH_Inflector::camelize(EEH_Inflector::singularize($table_name)); |
| 355 | 355 | } |
| 356 | 356 | |
@@ -367,21 +367,21 @@ discard block |
||
| 367 | 367 | * @param integer $number Number to get its ordinal value |
| 368 | 368 | * @return string Ordinal representation of given string. |
| 369 | 369 | */ |
| 370 | - public static function ordinalize($number){ |
|
| 371 | - if(in_array(($number % 100), range(11, 13))){ |
|
| 372 | - return $number . 'th'; |
|
| 373 | - }else{ |
|
| 374 | - switch(($number % 10)){ |
|
| 370 | + public static function ordinalize($number) { |
|
| 371 | + if (in_array(($number % 100), range(11, 13))) { |
|
| 372 | + return $number.'th'; |
|
| 373 | + } else { |
|
| 374 | + switch (($number % 10)) { |
|
| 375 | 375 | case 1: |
| 376 | - return $number . 'st'; |
|
| 376 | + return $number.'st'; |
|
| 377 | 377 | break; |
| 378 | 378 | case 2: |
| 379 | - return $number . 'nd'; |
|
| 379 | + return $number.'nd'; |
|
| 380 | 380 | break; |
| 381 | 381 | case 3: |
| 382 | - return $number . 'rd'; |
|
| 382 | + return $number.'rd'; |
|
| 383 | 383 | default: |
| 384 | - return $number . 'th'; |
|
| 384 | + return $number.'th'; |
|
| 385 | 385 | break; |
| 386 | 386 | } |
| 387 | 387 | } |
@@ -397,6 +397,6 @@ discard block |
||
| 397 | 397 | if (strtolower($string) === 'null') { |
| 398 | 398 | return $string; |
| 399 | 399 | } |
| 400 | - return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string; |
|
| 400 | + return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ').$string; |
|
| 401 | 401 | } |
| 402 | 402 | } |
| 403 | 403 | \ No newline at end of file |
@@ -5,7 +5,6 @@ |
||
| 5 | 5 | use WP_Error; |
| 6 | 6 | use WP_REST_Response; |
| 7 | 7 | use EventEspresso\core\libraries\rest_api\RestException; |
| 8 | - |
|
| 9 | 8 | use EE_Error; |
| 10 | 9 | use EED_Core_Rest_Api; |
| 11 | 10 | use EEH_Inflector; |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | use EED_Core_Rest_Api; |
| 11 | 11 | use EEH_Inflector; |
| 12 | 12 | |
| 13 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 13 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 14 | 14 | exit('No direct script access allowed'); |
| 15 | 15 | } |
| 16 | 16 | |
@@ -120,11 +120,11 @@ discard block |
||
| 120 | 120 | { |
| 121 | 121 | if (is_array($value)) { |
| 122 | 122 | foreach ($value as $value_key => $value_value) { |
| 123 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
| 123 | + $this->setResponseHeader($header_key.'['.$value_key.']', $value_value); |
|
| 124 | 124 | } |
| 125 | 125 | } else { |
| 126 | 126 | $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
| 127 | - $this->response_headers[$prefix . $header_key] = $value; |
|
| 127 | + $this->response_headers[$prefix.$header_key] = $value; |
|
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
| 157 | 157 | { |
| 158 | 158 | $notices_during_checkin = EE_Error::get_raw_notices(); |
| 159 | - if (! empty($notices_during_checkin['errors'])) { |
|
| 159 | + if ( ! empty($notices_during_checkin['errors'])) { |
|
| 160 | 160 | foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
| 161 | 161 | $wp_error_response->add( |
| 162 | 162 | sanitize_key($error_code), |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | if (is_array($debug_info)) { |
| 201 | 201 | $debug_info = wp_json_encode($debug_info); |
| 202 | 202 | } |
| 203 | - $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
| 203 | + $headers['X-EE4-Debug-'.ucwords($debug_key)] = $debug_info; |
|
| 204 | 204 | } |
| 205 | 205 | } |
| 206 | 206 | $headers = array_merge( |
@@ -231,8 +231,8 @@ discard block |
||
| 231 | 231 | $status = 500; |
| 232 | 232 | } |
| 233 | 233 | $errors = array(); |
| 234 | - foreach ((array)$wp_error->errors as $code => $messages) { |
|
| 235 | - foreach ((array)$messages as $message) { |
|
| 234 | + foreach ((array) $wp_error->errors as $code => $messages) { |
|
| 235 | + foreach ((array) $messages as $message) { |
|
| 236 | 236 | $errors[] = array( |
| 237 | 237 | 'code' => $code, |
| 238 | 238 | 'message' => $message, |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | $headers = array(); |
| 262 | 262 | $notices = EE_Error::get_raw_notices(); |
| 263 | 263 | foreach ($notices as $notice_type => $sub_notices) { |
| 264 | - if (! is_array($sub_notices)) { |
|
| 264 | + if ( ! is_array($sub_notices)) { |
|
| 265 | 265 | continue; |
| 266 | 266 | } |
| 267 | 267 | foreach ($sub_notices as $notice_code => $sub_notice) { |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | } |
| 300 | 300 | $matches = $this->parseRoute( |
| 301 | 301 | $route, |
| 302 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
| 302 | + '~'.EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', |
|
| 303 | 303 | array('version') |
| 304 | 304 | ); |
| 305 | 305 | if (isset($matches['version'])) { |
@@ -334,14 +334,14 @@ discard block |
||
| 334 | 334 | if (is_array($matches)) { |
| 335 | 335 | //skip the overall regex match. Who cares |
| 336 | 336 | for ($i = 1; $i <= count($match_keys); $i++) { |
| 337 | - if (! isset($matches[$i])) { |
|
| 337 | + if ( ! isset($matches[$i])) { |
|
| 338 | 338 | $success = false; |
| 339 | 339 | } else { |
| 340 | 340 | $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
| 341 | 341 | } |
| 342 | 342 | } |
| 343 | 343 | } |
| 344 | - if (! $success) { |
|
| 344 | + if ( ! $success) { |
|
| 345 | 345 | throw new EE_Error( |
| 346 | 346 | __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
| 347 | 347 | 'endpoint_parsing_error' |
@@ -362,8 +362,8 @@ discard block |
||
| 362 | 362 | { |
| 363 | 363 | //$request->get_params(); |
| 364 | 364 | return array_merge( |
| 365 | - (array)$request->get_body_params(), |
|
| 366 | - (array)$request->get_json_params() |
|
| 365 | + (array) $request->get_body_params(), |
|
| 366 | + (array) $request->get_json_params() |
|
| 367 | 367 | ); |
| 368 | 368 | // return array_diff_key( |
| 369 | 369 | // $request->get_params(), |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | use EEH_Inflector; |
| 12 | 12 | |
| 13 | 13 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 14 | - exit('No direct script access allowed'); |
|
| 14 | + exit('No direct script access allowed'); |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | |
@@ -27,352 +27,352 @@ discard block |
||
| 27 | 27 | class Base |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @deprecated use all-caps version |
|
| 32 | - */ |
|
| 33 | - // @codingStandardsIgnoreStart |
|
| 34 | - const header_prefix_for_ee = 'X-EE-'; |
|
| 35 | - // @codingStandardsIgnoreEnd |
|
| 36 | - |
|
| 37 | - const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @deprecated use all-caps version instead |
|
| 41 | - */ |
|
| 42 | - // @codingStandardsIgnoreStart |
|
| 43 | - const header_prefix_for_wp = 'X-WP-'; |
|
| 44 | - // @codingStandardsIgnoreEnd |
|
| 45 | - |
|
| 46 | - const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Contains debug info we'll send back in the response headers |
|
| 50 | - * |
|
| 51 | - * @var array |
|
| 52 | - */ |
|
| 53 | - protected $debug_info = array(); |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Indicates whether or not the API is in debug mode |
|
| 57 | - * |
|
| 58 | - * @var boolean |
|
| 59 | - */ |
|
| 60 | - protected $debug_mode = false; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Indicates the version that was requested |
|
| 64 | - * |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - protected $requested_version; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * flat array of headers to send in the response |
|
| 71 | - * |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - protected $response_headers = array(); |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - public function __construct() |
|
| 79 | - { |
|
| 80 | - $this->debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
| 81 | - //we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
| 82 | - add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Sets the version the user requested |
|
| 89 | - * |
|
| 90 | - * @param string $version eg '4.8' |
|
| 91 | - */ |
|
| 92 | - public function setRequestedVersion($version) |
|
| 93 | - { |
|
| 94 | - $this->requested_version = $version; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Sets some debug info that we'll send back in headers |
|
| 101 | - * |
|
| 102 | - * @param string $key |
|
| 103 | - * @param string|array $info |
|
| 104 | - */ |
|
| 105 | - protected function setDebugInfo($key, $info) |
|
| 106 | - { |
|
| 107 | - $this->debug_info[$key] = $info; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Sets headers for the response |
|
| 114 | - * |
|
| 115 | - * @param string $header_key , excluding the "X-EE-" part |
|
| 116 | - * @param array|string $value if an array, multiple headers will be added, one |
|
| 117 | - * for each key in the array |
|
| 118 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
| 119 | - * the standard WP one |
|
| 120 | - */ |
|
| 121 | - protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
| 122 | - { |
|
| 123 | - if (is_array($value)) { |
|
| 124 | - foreach ($value as $value_key => $value_value) { |
|
| 125 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
| 126 | - } |
|
| 127 | - } else { |
|
| 128 | - $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
| 129 | - $this->response_headers[$prefix . $header_key] = $value; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Returns a flat array of headers to be added to the response |
|
| 137 | - * |
|
| 138 | - * @return array |
|
| 139 | - */ |
|
| 140 | - protected function getResponseHeaders() |
|
| 141 | - { |
|
| 142 | - return apply_filters( |
|
| 143 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
| 144 | - $this->response_headers, |
|
| 145 | - $this, |
|
| 146 | - $this->requested_version |
|
| 147 | - ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Adds error notices from EE_Error onto the provided \WP_Error |
|
| 154 | - * |
|
| 155 | - * @param WP_Error $wp_error_response |
|
| 156 | - * @return WP_Error |
|
| 157 | - */ |
|
| 158 | - protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
| 159 | - { |
|
| 160 | - $notices_during_checkin = EE_Error::get_raw_notices(); |
|
| 161 | - if (! empty($notices_during_checkin['errors'])) { |
|
| 162 | - foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
| 163 | - $wp_error_response->add( |
|
| 164 | - sanitize_key($error_code), |
|
| 165 | - strip_tags($error_message) |
|
| 166 | - ); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - return $wp_error_response; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Sends a response, but also makes sure to attach headers that |
|
| 176 | - * are handy for debugging. |
|
| 177 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
| 178 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
| 179 | - * the response, others? |
|
| 180 | - * |
|
| 181 | - * @param array|WP_Error|Exception|RestException $response |
|
| 182 | - * @return WP_REST_Response |
|
| 183 | - */ |
|
| 184 | - public function sendResponse($response) |
|
| 185 | - { |
|
| 186 | - if ($response instanceof RestException) { |
|
| 187 | - $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
| 188 | - } |
|
| 189 | - if ($response instanceof Exception) { |
|
| 190 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
| 191 | - $response = new WP_Error($code, $response->getMessage()); |
|
| 192 | - } |
|
| 193 | - if ($response instanceof WP_Error) { |
|
| 194 | - $response = $this->addEeErrorsToResponse($response); |
|
| 195 | - $rest_response = $this->createRestResponseFromWpError($response); |
|
| 196 | - } else { |
|
| 197 | - $rest_response = new WP_REST_Response($response, 200); |
|
| 198 | - } |
|
| 199 | - $headers = array(); |
|
| 200 | - if ($this->debug_mode && is_array($this->debug_info)) { |
|
| 201 | - foreach ($this->debug_info as $debug_key => $debug_info) { |
|
| 202 | - if (is_array($debug_info)) { |
|
| 203 | - $debug_info = wp_json_encode($debug_info); |
|
| 204 | - } |
|
| 205 | - $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - $headers = array_merge( |
|
| 209 | - $headers, |
|
| 210 | - $this->getResponseHeaders(), |
|
| 211 | - $this->getHeadersFromEeNotices() |
|
| 212 | - ); |
|
| 213 | - $rest_response->set_headers($headers); |
|
| 214 | - return $rest_response; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Converts the \WP_Error into `WP_REST_Response. |
|
| 221 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
| 222 | - * (which is protected) |
|
| 223 | - * |
|
| 224 | - * @param WP_Error $wp_error |
|
| 225 | - * @return WP_REST_Response |
|
| 226 | - */ |
|
| 227 | - protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
| 228 | - { |
|
| 229 | - $error_data = $wp_error->get_error_data(); |
|
| 230 | - if (is_array($error_data) && isset($error_data['status'])) { |
|
| 231 | - $status = $error_data['status']; |
|
| 232 | - } else { |
|
| 233 | - $status = 500; |
|
| 234 | - } |
|
| 235 | - $errors = array(); |
|
| 236 | - foreach ((array)$wp_error->errors as $code => $messages) { |
|
| 237 | - foreach ((array)$messages as $message) { |
|
| 238 | - $errors[] = array( |
|
| 239 | - 'code' => $code, |
|
| 240 | - 'message' => $message, |
|
| 241 | - 'data' => $wp_error->get_error_data($code), |
|
| 242 | - ); |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - $data = isset($errors[0]) ? $errors[0] : array(); |
|
| 246 | - if (count($errors) > 1) { |
|
| 247 | - // Remove the primary error. |
|
| 248 | - array_shift($errors); |
|
| 249 | - $data['additional_errors'] = $errors; |
|
| 250 | - } |
|
| 251 | - return new WP_REST_Response($data, $status); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Array of headers derived from EE success, attention, and error messages |
|
| 258 | - * |
|
| 259 | - * @return array |
|
| 260 | - */ |
|
| 261 | - protected function getHeadersFromEeNotices() |
|
| 262 | - { |
|
| 263 | - $headers = array(); |
|
| 264 | - $notices = EE_Error::get_raw_notices(); |
|
| 265 | - foreach ($notices as $notice_type => $sub_notices) { |
|
| 266 | - if (! is_array($sub_notices)) { |
|
| 267 | - continue; |
|
| 268 | - } |
|
| 269 | - foreach ($sub_notices as $notice_code => $sub_notice) { |
|
| 270 | - $headers['X-EE4-Notices-' |
|
| 271 | - . EEH_Inflector::humanize($notice_type) |
|
| 272 | - . '[' |
|
| 273 | - . $notice_code |
|
| 274 | - . ']'] = strip_tags($sub_notice); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - return apply_filters( |
|
| 278 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
| 279 | - $headers, |
|
| 280 | - $this->requested_version, |
|
| 281 | - $notices |
|
| 282 | - ); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Finds which version of the API was requested given the route, and returns it. |
|
| 289 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
| 290 | - * "4.8.29". |
|
| 291 | - * We should know hte requested version in this model though, so if no route is |
|
| 292 | - * provided just use what we set earlier |
|
| 293 | - * |
|
| 294 | - * @param string $route |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - public function getRequestedVersion($route = null) |
|
| 298 | - { |
|
| 299 | - if ($route === null) { |
|
| 300 | - return $this->requested_version; |
|
| 301 | - } |
|
| 302 | - $matches = $this->parseRoute( |
|
| 303 | - $route, |
|
| 304 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
| 305 | - array('version') |
|
| 306 | - ); |
|
| 307 | - if (isset($matches['version'])) { |
|
| 308 | - return $matches['version']; |
|
| 309 | - } else { |
|
| 310 | - return EED_Core_Rest_Api::latest_rest_api_version(); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Applies the regex to the route, then creates an array using the values of |
|
| 318 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
| 319 | - * For example, if you call |
|
| 320 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
| 321 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
| 322 | - * |
|
| 323 | - * @param string $route |
|
| 324 | - * @param string $regex |
|
| 325 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
| 326 | - * @return array where $match_keys are the keys (the first value of $match_keys |
|
| 327 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
| 328 | - * array( 'model', 'id' ), will, if the regex is successful, will return |
|
| 329 | - * array( 'model' => 'foo', 'id' => 'bar' ) |
|
| 330 | - * @throws EE_Error if it couldn't be parsed |
|
| 331 | - */ |
|
| 332 | - public function parseRoute($route, $regex, $match_keys) |
|
| 333 | - { |
|
| 334 | - $indexed_matches = array(); |
|
| 335 | - $success = preg_match($regex, $route, $matches); |
|
| 336 | - if (is_array($matches)) { |
|
| 337 | - //skip the overall regex match. Who cares |
|
| 338 | - for ($i = 1; $i <= count($match_keys); $i++) { |
|
| 339 | - if (! isset($matches[$i])) { |
|
| 340 | - $success = false; |
|
| 341 | - } else { |
|
| 342 | - $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - if (! $success) { |
|
| 347 | - throw new EE_Error( |
|
| 348 | - __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
| 349 | - 'endpoint_parsing_error' |
|
| 350 | - ); |
|
| 351 | - } |
|
| 352 | - return $indexed_matches; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
| 359 | - * |
|
| 360 | - * @param \WP_REST_Request $request |
|
| 361 | - * @return array |
|
| 362 | - */ |
|
| 363 | - protected function getBodyParams(\WP_REST_Request $request) |
|
| 364 | - { |
|
| 365 | - //$request->get_params(); |
|
| 366 | - return array_merge( |
|
| 367 | - (array)$request->get_body_params(), |
|
| 368 | - (array)$request->get_json_params() |
|
| 369 | - ); |
|
| 370 | - // return array_diff_key( |
|
| 371 | - // $request->get_params(), |
|
| 372 | - // $request->get_url_params(), |
|
| 373 | - // $request->get_query_params() |
|
| 374 | - // ); |
|
| 375 | - } |
|
| 30 | + /** |
|
| 31 | + * @deprecated use all-caps version |
|
| 32 | + */ |
|
| 33 | + // @codingStandardsIgnoreStart |
|
| 34 | + const header_prefix_for_ee = 'X-EE-'; |
|
| 35 | + // @codingStandardsIgnoreEnd |
|
| 36 | + |
|
| 37 | + const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @deprecated use all-caps version instead |
|
| 41 | + */ |
|
| 42 | + // @codingStandardsIgnoreStart |
|
| 43 | + const header_prefix_for_wp = 'X-WP-'; |
|
| 44 | + // @codingStandardsIgnoreEnd |
|
| 45 | + |
|
| 46 | + const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Contains debug info we'll send back in the response headers |
|
| 50 | + * |
|
| 51 | + * @var array |
|
| 52 | + */ |
|
| 53 | + protected $debug_info = array(); |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Indicates whether or not the API is in debug mode |
|
| 57 | + * |
|
| 58 | + * @var boolean |
|
| 59 | + */ |
|
| 60 | + protected $debug_mode = false; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Indicates the version that was requested |
|
| 64 | + * |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + protected $requested_version; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * flat array of headers to send in the response |
|
| 71 | + * |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + protected $response_headers = array(); |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + public function __construct() |
|
| 79 | + { |
|
| 80 | + $this->debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
| 81 | + //we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
| 82 | + add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Sets the version the user requested |
|
| 89 | + * |
|
| 90 | + * @param string $version eg '4.8' |
|
| 91 | + */ |
|
| 92 | + public function setRequestedVersion($version) |
|
| 93 | + { |
|
| 94 | + $this->requested_version = $version; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Sets some debug info that we'll send back in headers |
|
| 101 | + * |
|
| 102 | + * @param string $key |
|
| 103 | + * @param string|array $info |
|
| 104 | + */ |
|
| 105 | + protected function setDebugInfo($key, $info) |
|
| 106 | + { |
|
| 107 | + $this->debug_info[$key] = $info; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Sets headers for the response |
|
| 114 | + * |
|
| 115 | + * @param string $header_key , excluding the "X-EE-" part |
|
| 116 | + * @param array|string $value if an array, multiple headers will be added, one |
|
| 117 | + * for each key in the array |
|
| 118 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
| 119 | + * the standard WP one |
|
| 120 | + */ |
|
| 121 | + protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
| 122 | + { |
|
| 123 | + if (is_array($value)) { |
|
| 124 | + foreach ($value as $value_key => $value_value) { |
|
| 125 | + $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
| 126 | + } |
|
| 127 | + } else { |
|
| 128 | + $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
| 129 | + $this->response_headers[$prefix . $header_key] = $value; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Returns a flat array of headers to be added to the response |
|
| 137 | + * |
|
| 138 | + * @return array |
|
| 139 | + */ |
|
| 140 | + protected function getResponseHeaders() |
|
| 141 | + { |
|
| 142 | + return apply_filters( |
|
| 143 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
| 144 | + $this->response_headers, |
|
| 145 | + $this, |
|
| 146 | + $this->requested_version |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Adds error notices from EE_Error onto the provided \WP_Error |
|
| 154 | + * |
|
| 155 | + * @param WP_Error $wp_error_response |
|
| 156 | + * @return WP_Error |
|
| 157 | + */ |
|
| 158 | + protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
| 159 | + { |
|
| 160 | + $notices_during_checkin = EE_Error::get_raw_notices(); |
|
| 161 | + if (! empty($notices_during_checkin['errors'])) { |
|
| 162 | + foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
| 163 | + $wp_error_response->add( |
|
| 164 | + sanitize_key($error_code), |
|
| 165 | + strip_tags($error_message) |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + return $wp_error_response; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Sends a response, but also makes sure to attach headers that |
|
| 176 | + * are handy for debugging. |
|
| 177 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
| 178 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
| 179 | + * the response, others? |
|
| 180 | + * |
|
| 181 | + * @param array|WP_Error|Exception|RestException $response |
|
| 182 | + * @return WP_REST_Response |
|
| 183 | + */ |
|
| 184 | + public function sendResponse($response) |
|
| 185 | + { |
|
| 186 | + if ($response instanceof RestException) { |
|
| 187 | + $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
| 188 | + } |
|
| 189 | + if ($response instanceof Exception) { |
|
| 190 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
| 191 | + $response = new WP_Error($code, $response->getMessage()); |
|
| 192 | + } |
|
| 193 | + if ($response instanceof WP_Error) { |
|
| 194 | + $response = $this->addEeErrorsToResponse($response); |
|
| 195 | + $rest_response = $this->createRestResponseFromWpError($response); |
|
| 196 | + } else { |
|
| 197 | + $rest_response = new WP_REST_Response($response, 200); |
|
| 198 | + } |
|
| 199 | + $headers = array(); |
|
| 200 | + if ($this->debug_mode && is_array($this->debug_info)) { |
|
| 201 | + foreach ($this->debug_info as $debug_key => $debug_info) { |
|
| 202 | + if (is_array($debug_info)) { |
|
| 203 | + $debug_info = wp_json_encode($debug_info); |
|
| 204 | + } |
|
| 205 | + $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + $headers = array_merge( |
|
| 209 | + $headers, |
|
| 210 | + $this->getResponseHeaders(), |
|
| 211 | + $this->getHeadersFromEeNotices() |
|
| 212 | + ); |
|
| 213 | + $rest_response->set_headers($headers); |
|
| 214 | + return $rest_response; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Converts the \WP_Error into `WP_REST_Response. |
|
| 221 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
| 222 | + * (which is protected) |
|
| 223 | + * |
|
| 224 | + * @param WP_Error $wp_error |
|
| 225 | + * @return WP_REST_Response |
|
| 226 | + */ |
|
| 227 | + protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
| 228 | + { |
|
| 229 | + $error_data = $wp_error->get_error_data(); |
|
| 230 | + if (is_array($error_data) && isset($error_data['status'])) { |
|
| 231 | + $status = $error_data['status']; |
|
| 232 | + } else { |
|
| 233 | + $status = 500; |
|
| 234 | + } |
|
| 235 | + $errors = array(); |
|
| 236 | + foreach ((array)$wp_error->errors as $code => $messages) { |
|
| 237 | + foreach ((array)$messages as $message) { |
|
| 238 | + $errors[] = array( |
|
| 239 | + 'code' => $code, |
|
| 240 | + 'message' => $message, |
|
| 241 | + 'data' => $wp_error->get_error_data($code), |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + $data = isset($errors[0]) ? $errors[0] : array(); |
|
| 246 | + if (count($errors) > 1) { |
|
| 247 | + // Remove the primary error. |
|
| 248 | + array_shift($errors); |
|
| 249 | + $data['additional_errors'] = $errors; |
|
| 250 | + } |
|
| 251 | + return new WP_REST_Response($data, $status); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Array of headers derived from EE success, attention, and error messages |
|
| 258 | + * |
|
| 259 | + * @return array |
|
| 260 | + */ |
|
| 261 | + protected function getHeadersFromEeNotices() |
|
| 262 | + { |
|
| 263 | + $headers = array(); |
|
| 264 | + $notices = EE_Error::get_raw_notices(); |
|
| 265 | + foreach ($notices as $notice_type => $sub_notices) { |
|
| 266 | + if (! is_array($sub_notices)) { |
|
| 267 | + continue; |
|
| 268 | + } |
|
| 269 | + foreach ($sub_notices as $notice_code => $sub_notice) { |
|
| 270 | + $headers['X-EE4-Notices-' |
|
| 271 | + . EEH_Inflector::humanize($notice_type) |
|
| 272 | + . '[' |
|
| 273 | + . $notice_code |
|
| 274 | + . ']'] = strip_tags($sub_notice); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + return apply_filters( |
|
| 278 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
| 279 | + $headers, |
|
| 280 | + $this->requested_version, |
|
| 281 | + $notices |
|
| 282 | + ); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Finds which version of the API was requested given the route, and returns it. |
|
| 289 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
| 290 | + * "4.8.29". |
|
| 291 | + * We should know hte requested version in this model though, so if no route is |
|
| 292 | + * provided just use what we set earlier |
|
| 293 | + * |
|
| 294 | + * @param string $route |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + public function getRequestedVersion($route = null) |
|
| 298 | + { |
|
| 299 | + if ($route === null) { |
|
| 300 | + return $this->requested_version; |
|
| 301 | + } |
|
| 302 | + $matches = $this->parseRoute( |
|
| 303 | + $route, |
|
| 304 | + '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
| 305 | + array('version') |
|
| 306 | + ); |
|
| 307 | + if (isset($matches['version'])) { |
|
| 308 | + return $matches['version']; |
|
| 309 | + } else { |
|
| 310 | + return EED_Core_Rest_Api::latest_rest_api_version(); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Applies the regex to the route, then creates an array using the values of |
|
| 318 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
| 319 | + * For example, if you call |
|
| 320 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
| 321 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
| 322 | + * |
|
| 323 | + * @param string $route |
|
| 324 | + * @param string $regex |
|
| 325 | + * @param array $match_keys EXCLUDING matching the entire regex |
|
| 326 | + * @return array where $match_keys are the keys (the first value of $match_keys |
|
| 327 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
| 328 | + * array( 'model', 'id' ), will, if the regex is successful, will return |
|
| 329 | + * array( 'model' => 'foo', 'id' => 'bar' ) |
|
| 330 | + * @throws EE_Error if it couldn't be parsed |
|
| 331 | + */ |
|
| 332 | + public function parseRoute($route, $regex, $match_keys) |
|
| 333 | + { |
|
| 334 | + $indexed_matches = array(); |
|
| 335 | + $success = preg_match($regex, $route, $matches); |
|
| 336 | + if (is_array($matches)) { |
|
| 337 | + //skip the overall regex match. Who cares |
|
| 338 | + for ($i = 1; $i <= count($match_keys); $i++) { |
|
| 339 | + if (! isset($matches[$i])) { |
|
| 340 | + $success = false; |
|
| 341 | + } else { |
|
| 342 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + if (! $success) { |
|
| 347 | + throw new EE_Error( |
|
| 348 | + __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
| 349 | + 'endpoint_parsing_error' |
|
| 350 | + ); |
|
| 351 | + } |
|
| 352 | + return $indexed_matches; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
| 359 | + * |
|
| 360 | + * @param \WP_REST_Request $request |
|
| 361 | + * @return array |
|
| 362 | + */ |
|
| 363 | + protected function getBodyParams(\WP_REST_Request $request) |
|
| 364 | + { |
|
| 365 | + //$request->get_params(); |
|
| 366 | + return array_merge( |
|
| 367 | + (array)$request->get_body_params(), |
|
| 368 | + (array)$request->get_json_params() |
|
| 369 | + ); |
|
| 370 | + // return array_diff_key( |
|
| 371 | + // $request->get_params(), |
|
| 372 | + // $request->get_url_params(), |
|
| 373 | + // $request->get_query_params() |
|
| 374 | + // ); |
|
| 375 | + } |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | // End of file Base.php |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | * @subpackage |
| 12 | 12 | * @author Mike Nelson |
| 13 | 13 | */ |
| 14 | -class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base{ |
|
| 14 | +class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base { |
|
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * @var string $_model_name |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * And the arguments accepted by EE_Form_Input_With_Options_Base |
| 51 | 51 | * @throws \EE_Error |
| 52 | 52 | */ |
| 53 | - public function __construct( $input_settings = array() ) { |
|
| 53 | + public function __construct($input_settings = array()) { |
|
| 54 | 54 | //needed input settings: |
| 55 | 55 | //select2_args |
| 56 | 56 | $this->_model_name = EEH_Array::is_set( |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | ); |
| 67 | 67 | // make sure limit and caps are always set |
| 68 | 68 | $query_params = array_merge( |
| 69 | - array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
| 69 | + array('limit' => 10, 'caps' => EEM_Base::caps_read_admin), |
|
| 70 | 70 | $query_params |
| 71 | 71 | ); |
| 72 | 72 | $this->_value_field_name = EEH_Array::is_set( |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | $this->_display_field_name = EEH_Array::is_set( |
| 78 | 78 | $input_settings, |
| 79 | 79 | 'display_field_name', |
| 80 | - $model->get_a_field_of_type( 'EE_Text_Field_Base' )->get_name() |
|
| 80 | + $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
| 81 | 81 | ); |
| 82 | 82 | $this->_extra_select_columns = EEH_Array::is_set( |
| 83 | 83 | $input_settings, |
@@ -94,24 +94,24 @@ discard block |
||
| 94 | 94 | ); |
| 95 | 95 | //get resource endpoint |
| 96 | 96 | $rest_controller = new EventEspresso\core\libraries\rest_api\controllers\model\Read(); |
| 97 | - $rest_controller->setRequestedVersion( EED_Core_Rest_Api::latest_rest_api_version() ); |
|
| 97 | + $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
| 98 | 98 | $default_select2_args = array( |
| 99 | 99 | 'ajax' => array( |
| 100 | 100 | 'url' => $rest_controller->getVersionedLinkTo( |
| 101 | - EEH_Inflector::pluralize_and_lower( $this->_model_name ) |
|
| 101 | + EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
| 102 | 102 | ), |
| 103 | 103 | 'dataType' => 'json', |
| 104 | 104 | 'delay' => '250', |
| 105 | 105 | 'data_interface' => 'EE_Select2_REST_API_Interface', |
| 106 | 106 | 'data_interface_args' => array( |
| 107 | - 'default_query_params' => (object)ModelDataTranslator::prepareQueryParamsForRestApi( |
|
| 107 | + 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
| 108 | 108 | $query_params, |
| 109 | 109 | $model |
| 110 | 110 | ), |
| 111 | 111 | 'display_field' => $this->_display_field_name, |
| 112 | 112 | 'value_field' => $this->_value_field_name, |
| 113 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 114 | - 'locale' => str_replace( '_', '-', strtolower( get_locale() ) ) |
|
| 113 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
| 114 | + 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
| 115 | 115 | ), |
| 116 | 116 | ), |
| 117 | 117 | 'cache' => true, |
@@ -119,10 +119,10 @@ discard block |
||
| 119 | 119 | ); |
| 120 | 120 | $select2_args = array_replace_recursive( |
| 121 | 121 | $default_select2_args, |
| 122 | - (array)EEH_Array::is_set( $input_settings, 'select2_args', array() ) |
|
| 122 | + (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
| 123 | 123 | ); |
| 124 | - $this->set_display_strategy( new EE_Select2_Display_Strategy( $select2_args ) ); |
|
| 125 | - parent::__construct( array(), $input_settings ); |
|
| 124 | + $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
| 125 | + parent::__construct(array(), $input_settings); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | |
@@ -140,34 +140,34 @@ discard block |
||
| 140 | 140 | * @return void |
| 141 | 141 | * @throws \EE_Error |
| 142 | 142 | */ |
| 143 | - public function _set_raw_value( $value ) { |
|
| 144 | - $values_for_options = (array)$value; |
|
| 145 | - $value_field = $this->_get_model()->field_settings_for( $this->_value_field_name ); |
|
| 146 | - $display_field = $this->_get_model()->field_settings_for( $this->_display_field_name ); |
|
| 147 | - $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
| 148 | - $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
| 143 | + public function _set_raw_value($value) { |
|
| 144 | + $values_for_options = (array) $value; |
|
| 145 | + $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
| 146 | + $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
| 147 | + $this->_extra_select_columns[] = $value_field->get_qualified_column().' AS '.$this->_value_field_name; |
|
| 148 | + $this->_extra_select_columns[] = $display_field->get_qualified_column().' AS '.$this->_display_field_name; |
|
| 149 | 149 | $display_values = $this->_get_model()->get_all_wpdb_results( |
| 150 | 150 | array( |
| 151 | 151 | array( |
| 152 | - $this->_value_field_name => array( 'IN', $values_for_options ) |
|
| 152 | + $this->_value_field_name => array('IN', $values_for_options) |
|
| 153 | 153 | ) |
| 154 | 154 | ), |
| 155 | 155 | ARRAY_A, |
| 156 | - implode( ',', $this->_extra_select_columns ) |
|
| 156 | + implode(',', $this->_extra_select_columns) |
|
| 157 | 157 | ); |
| 158 | 158 | $select_options = array(); |
| 159 | - if( is_array( $select_options ) ) { |
|
| 160 | - foreach( $display_values as $db_rows ) { |
|
| 161 | - $db_rows = (array)$db_rows; |
|
| 162 | - $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
| 159 | + if (is_array($select_options)) { |
|
| 160 | + foreach ($display_values as $db_rows) { |
|
| 161 | + $db_rows = (array) $db_rows; |
|
| 162 | + $select_options[$db_rows[$this->_value_field_name]] = apply_filters( |
|
| 163 | 163 | 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
| 164 | - $db_rows[ $this->_display_field_name ], |
|
| 164 | + $db_rows[$this->_display_field_name], |
|
| 165 | 165 | $db_rows |
| 166 | 166 | ); |
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | - $this->set_select_options( $select_options ); |
|
| 170 | - parent::_set_raw_value( $value ); |
|
| 169 | + $this->set_select_options($select_options); |
|
| 170 | + parent::_set_raw_value($value); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | * @throws EE_Error |
| 177 | 177 | */ |
| 178 | 178 | protected function _get_model() { |
| 179 | - if( ! EE_Registry::instance()->is_model_name( $this->_model_name ) ) { |
|
| 179 | + if ( ! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
| 180 | 180 | throw new EE_Error( |
| 181 | 181 | sprintf( |
| 182 | 182 | __( |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | ) |
| 188 | 188 | ); |
| 189 | 189 | } else { |
| 190 | - return EE_Registry::instance()->load_model( $this->_model_name ); |
|
| 190 | + return EE_Registry::instance()->load_model($this->_model_name); |
|
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @author Mike Nelson |
| 17 | 17 | * @since 4.8.35.rc.001 |
| 18 | 18 | */ |
| 19 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 19 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 20 | 20 | exit('No direct script access allowed'); |
| 21 | 21 | } |
| 22 | 22 | |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | public function mapping($refresh = false) |
| 46 | 46 | { |
| 47 | - if (! $this->mapping || $refresh) { |
|
| 47 | + if ( ! $this->mapping || $refresh) { |
|
| 48 | 48 | $this->mapping = $this->generateNewMapping(); |
| 49 | 49 | } |
| 50 | 50 | return $this->mapping; |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | protected function generateNewMapping() |
| 61 | 61 | { |
| 62 | 62 | $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
| 63 | - $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
| 64 | - $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
| 65 | - $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
| 63 | + $event_calculations_class = $rest_api_calculations_namespace.'Event'; |
|
| 64 | + $datetime_calculations_class = $rest_api_calculations_namespace.'Datetime'; |
|
| 65 | + $registration_class = $rest_api_calculations_namespace.'Registration'; |
|
| 66 | 66 | return apply_filters( |
| 67 | 67 | 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
| 68 | 68 | array( |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @since 4.8.35.rc.001 |
| 17 | 17 | */ |
| 18 | 18 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 19 | - exit('No direct script access allowed'); |
|
| 19 | + exit('No direct script access allowed'); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | |
@@ -24,128 +24,128 @@ discard block |
||
| 24 | 24 | class CalculatedModelFields |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $mapping; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param bool $refresh |
|
| 36 | - * @return array top-level-keys are model names (eg "Event") |
|
| 37 | - * next-level are the calculated field names AND method names on classes |
|
| 38 | - * which perform calculations, values are the fully qualified classnames which do the calculations |
|
| 39 | - * These callbacks should accept as arguments: |
|
| 40 | - * the wpdb row results, |
|
| 41 | - * the WP_Request object, |
|
| 42 | - * the controller object |
|
| 43 | - */ |
|
| 44 | - public function mapping($refresh = false) |
|
| 45 | - { |
|
| 46 | - if (! $this->mapping || $refresh) { |
|
| 47 | - $this->mapping = $this->generateNewMapping(); |
|
| 48 | - } |
|
| 49 | - return $this->mapping; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Generates anew mapping between model calculated fields and their callbacks |
|
| 56 | - * |
|
| 57 | - * @return array |
|
| 58 | - */ |
|
| 59 | - protected function generateNewMapping() |
|
| 60 | - { |
|
| 61 | - $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
| 62 | - $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
| 63 | - $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
| 64 | - $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
| 65 | - return apply_filters( |
|
| 66 | - 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
| 67 | - array( |
|
| 68 | - 'Event' => array( |
|
| 69 | - 'optimum_sales_at_start' => $event_calculations_class, |
|
| 70 | - 'optimum_sales_now' => $event_calculations_class, |
|
| 71 | - 'spots_taken' => $event_calculations_class, |
|
| 72 | - 'spots_taken_pending_payment' => $event_calculations_class, |
|
| 73 | - 'spaces_remaining' => $event_calculations_class, |
|
| 74 | - 'registrations_checked_in_count' => $event_calculations_class, |
|
| 75 | - 'registrations_checked_out_count' => $event_calculations_class, |
|
| 76 | - 'image_thumbnail' => $event_calculations_class, |
|
| 77 | - 'image_medium' => $event_calculations_class, |
|
| 78 | - 'image_medium_large' => $event_calculations_class, |
|
| 79 | - 'image_large' => $event_calculations_class, |
|
| 80 | - 'image_post_thumbnail' => $event_calculations_class, |
|
| 81 | - 'image_full' => $event_calculations_class, |
|
| 82 | - ), |
|
| 83 | - 'Datetime' => array( |
|
| 84 | - 'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
| 85 | - 'registrations_checked_in_count' => $datetime_calculations_class, |
|
| 86 | - 'registrations_checked_out_count' => $datetime_calculations_class, |
|
| 87 | - 'spots_taken_pending_payment' => $datetime_calculations_class, |
|
| 88 | - ), |
|
| 89 | - 'Registration' => array( |
|
| 90 | - 'datetime_checkin_stati' => $registration_class, |
|
| 91 | - ), |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Gets the known calculated fields for model |
|
| 100 | - * |
|
| 101 | - * @param EEM_Base $model |
|
| 102 | - * @return array allowable values for this field |
|
| 103 | - */ |
|
| 104 | - public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
| 105 | - { |
|
| 106 | - $mapping = $this->mapping(); |
|
| 107 | - if (isset($mapping[$model->get_this_model_name()])) { |
|
| 108 | - return array_keys($mapping[$model->get_this_model_name()]); |
|
| 109 | - } else { |
|
| 110 | - return array(); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Retrieves the value for this calculation |
|
| 118 | - * |
|
| 119 | - * @param EEM_Base $model |
|
| 120 | - * @param string $field_name |
|
| 121 | - * @param array $wpdb_row |
|
| 122 | - * @param \WP_REST_Request |
|
| 123 | - * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
| 124 | - * @return mixed|null |
|
| 125 | - * @throws \EE_Error |
|
| 126 | - */ |
|
| 127 | - public function retrieveCalculatedFieldValue( |
|
| 128 | - EEM_Base $model, |
|
| 129 | - $field_name, |
|
| 130 | - $wpdb_row, |
|
| 131 | - $rest_request, |
|
| 132 | - Base $controller |
|
| 133 | - ) { |
|
| 134 | - $mapping = $this->mapping(); |
|
| 135 | - if (isset($mapping[$model->get_this_model_name()]) |
|
| 136 | - && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
| 137 | - ) { |
|
| 138 | - $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
| 139 | - $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
| 140 | - return call_user_func(array($classname, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
| 141 | - } |
|
| 142 | - throw new RestException( |
|
| 143 | - 'calculated_field_does_not_exist', |
|
| 144 | - sprintf( |
|
| 145 | - __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
| 146 | - $field_name, |
|
| 147 | - $model->get_this_model_name() |
|
| 148 | - ) |
|
| 149 | - ); |
|
| 150 | - } |
|
| 27 | + /** |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $mapping; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param bool $refresh |
|
| 36 | + * @return array top-level-keys are model names (eg "Event") |
|
| 37 | + * next-level are the calculated field names AND method names on classes |
|
| 38 | + * which perform calculations, values are the fully qualified classnames which do the calculations |
|
| 39 | + * These callbacks should accept as arguments: |
|
| 40 | + * the wpdb row results, |
|
| 41 | + * the WP_Request object, |
|
| 42 | + * the controller object |
|
| 43 | + */ |
|
| 44 | + public function mapping($refresh = false) |
|
| 45 | + { |
|
| 46 | + if (! $this->mapping || $refresh) { |
|
| 47 | + $this->mapping = $this->generateNewMapping(); |
|
| 48 | + } |
|
| 49 | + return $this->mapping; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Generates anew mapping between model calculated fields and their callbacks |
|
| 56 | + * |
|
| 57 | + * @return array |
|
| 58 | + */ |
|
| 59 | + protected function generateNewMapping() |
|
| 60 | + { |
|
| 61 | + $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
| 62 | + $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
| 63 | + $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
| 64 | + $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
| 65 | + return apply_filters( |
|
| 66 | + 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
| 67 | + array( |
|
| 68 | + 'Event' => array( |
|
| 69 | + 'optimum_sales_at_start' => $event_calculations_class, |
|
| 70 | + 'optimum_sales_now' => $event_calculations_class, |
|
| 71 | + 'spots_taken' => $event_calculations_class, |
|
| 72 | + 'spots_taken_pending_payment' => $event_calculations_class, |
|
| 73 | + 'spaces_remaining' => $event_calculations_class, |
|
| 74 | + 'registrations_checked_in_count' => $event_calculations_class, |
|
| 75 | + 'registrations_checked_out_count' => $event_calculations_class, |
|
| 76 | + 'image_thumbnail' => $event_calculations_class, |
|
| 77 | + 'image_medium' => $event_calculations_class, |
|
| 78 | + 'image_medium_large' => $event_calculations_class, |
|
| 79 | + 'image_large' => $event_calculations_class, |
|
| 80 | + 'image_post_thumbnail' => $event_calculations_class, |
|
| 81 | + 'image_full' => $event_calculations_class, |
|
| 82 | + ), |
|
| 83 | + 'Datetime' => array( |
|
| 84 | + 'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
| 85 | + 'registrations_checked_in_count' => $datetime_calculations_class, |
|
| 86 | + 'registrations_checked_out_count' => $datetime_calculations_class, |
|
| 87 | + 'spots_taken_pending_payment' => $datetime_calculations_class, |
|
| 88 | + ), |
|
| 89 | + 'Registration' => array( |
|
| 90 | + 'datetime_checkin_stati' => $registration_class, |
|
| 91 | + ), |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Gets the known calculated fields for model |
|
| 100 | + * |
|
| 101 | + * @param EEM_Base $model |
|
| 102 | + * @return array allowable values for this field |
|
| 103 | + */ |
|
| 104 | + public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
| 105 | + { |
|
| 106 | + $mapping = $this->mapping(); |
|
| 107 | + if (isset($mapping[$model->get_this_model_name()])) { |
|
| 108 | + return array_keys($mapping[$model->get_this_model_name()]); |
|
| 109 | + } else { |
|
| 110 | + return array(); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Retrieves the value for this calculation |
|
| 118 | + * |
|
| 119 | + * @param EEM_Base $model |
|
| 120 | + * @param string $field_name |
|
| 121 | + * @param array $wpdb_row |
|
| 122 | + * @param \WP_REST_Request |
|
| 123 | + * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
| 124 | + * @return mixed|null |
|
| 125 | + * @throws \EE_Error |
|
| 126 | + */ |
|
| 127 | + public function retrieveCalculatedFieldValue( |
|
| 128 | + EEM_Base $model, |
|
| 129 | + $field_name, |
|
| 130 | + $wpdb_row, |
|
| 131 | + $rest_request, |
|
| 132 | + Base $controller |
|
| 133 | + ) { |
|
| 134 | + $mapping = $this->mapping(); |
|
| 135 | + if (isset($mapping[$model->get_this_model_name()]) |
|
| 136 | + && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
| 137 | + ) { |
|
| 138 | + $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
| 139 | + $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
| 140 | + return call_user_func(array($classname, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
| 141 | + } |
|
| 142 | + throw new RestException( |
|
| 143 | + 'calculated_field_does_not_exist', |
|
| 144 | + sprintf( |
|
| 145 | + __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
| 146 | + $field_name, |
|
| 147 | + $model->get_this_model_name() |
|
| 148 | + ) |
|
| 149 | + ); |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * @author Mike Nelson |
| 13 | 13 | * @since $VID:$ |
| 14 | 14 | */ |
| 15 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 15 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 16 | 16 | exit('No direct script access allowed'); |
| 17 | 17 | } |
| 18 | 18 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | protected static function verifyCurrentUserCan($required_permission, $attempted_calculation) |
| 30 | 30 | { |
| 31 | - if (! current_user_can($required_permission)) { |
|
| 31 | + if ( ! current_user_can($required_permission)) { |
|
| 32 | 32 | throw new RestException( |
| 33 | 33 | 'permission_denied', |
| 34 | 34 | sprintf( |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | * @since $VID:$ |
| 15 | 15 | */ |
| 16 | 16 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 17 | - exit('No direct script access allowed'); |
|
| 17 | + exit('No direct script access allowed'); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | |
@@ -22,41 +22,41 @@ discard block |
||
| 22 | 22 | class Base |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param $required_permission |
|
| 27 | - * @param $attempted_calculation |
|
| 28 | - * @throws \EventEspresso\core\libraries\rest_api\RestException |
|
| 29 | - */ |
|
| 30 | - protected static function verifyCurrentUserCan($required_permission, $attempted_calculation) |
|
| 31 | - { |
|
| 32 | - if (! current_user_can($required_permission)) { |
|
| 33 | - throw new RestException( |
|
| 34 | - 'permission_denied', |
|
| 35 | - sprintf( |
|
| 36 | - __( |
|
| 37 | - // @codingStandardsIgnoreStart |
|
| 38 | - 'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
| 39 | - // @codingStandardsIgnoreEnd |
|
| 40 | - 'event_espresso' |
|
| 41 | - ), |
|
| 42 | - $attempted_calculation, |
|
| 43 | - EEH_Inflector::pluralize_and_lower(self::getResourceName()), |
|
| 44 | - $required_permission |
|
| 45 | - ) |
|
| 46 | - ); |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Gets the name of the resource of the called class |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - public static function getResourceName() |
|
| 58 | - { |
|
| 59 | - $classname = get_called_class(); |
|
| 60 | - return substr($classname, strrpos($classname, '\\') + 1); |
|
| 61 | - } |
|
| 25 | + /** |
|
| 26 | + * @param $required_permission |
|
| 27 | + * @param $attempted_calculation |
|
| 28 | + * @throws \EventEspresso\core\libraries\rest_api\RestException |
|
| 29 | + */ |
|
| 30 | + protected static function verifyCurrentUserCan($required_permission, $attempted_calculation) |
|
| 31 | + { |
|
| 32 | + if (! current_user_can($required_permission)) { |
|
| 33 | + throw new RestException( |
|
| 34 | + 'permission_denied', |
|
| 35 | + sprintf( |
|
| 36 | + __( |
|
| 37 | + // @codingStandardsIgnoreStart |
|
| 38 | + 'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
| 39 | + // @codingStandardsIgnoreEnd |
|
| 40 | + 'event_espresso' |
|
| 41 | + ), |
|
| 42 | + $attempted_calculation, |
|
| 43 | + EEH_Inflector::pluralize_and_lower(self::getResourceName()), |
|
| 44 | + $required_permission |
|
| 45 | + ) |
|
| 46 | + ); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Gets the name of the resource of the called class |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + public static function getResourceName() |
|
| 58 | + { |
|
| 59 | + $classname = get_called_class(); |
|
| 60 | + return substr($classname, strrpos($classname, '\\') + 1); |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @author Mike Nelson |
| 16 | 16 | * @since $VID:$ |
| 17 | 17 | */ |
| 18 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 18 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 19 | 19 | exit('No direct script access allowed'); |
| 20 | 20 | } |
| 21 | 21 | |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | } else { |
| 41 | 41 | $reg = null; |
| 42 | 42 | } |
| 43 | - if (! $reg instanceof EE_Registration |
|
| 43 | + if ( ! $reg instanceof EE_Registration |
|
| 44 | 44 | ) { |
| 45 | 45 | throw new \EE_Error( |
| 46 | 46 | sprintf( |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | * @since $VID:$ |
| 18 | 18 | */ |
| 19 | 19 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 20 | - exit('No direct script access allowed'); |
|
| 20 | + exit('No direct script access allowed'); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
@@ -25,62 +25,62 @@ discard block |
||
| 25 | 25 | class Registration extends Calculations_Base |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Calculates the checkin status for each datetime this registration has access to |
|
| 30 | - * |
|
| 31 | - * @param array $wpdb_row |
|
| 32 | - * @param \WP_REST_Request $request |
|
| 33 | - * @param Base $controller |
|
| 34 | - * @return array |
|
| 35 | - * @throws \EE_Error |
|
| 36 | - */ |
|
| 37 | - public static function datetimeCheckinStati($wpdb_row, $request, $controller) |
|
| 38 | - { |
|
| 39 | - if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
| 40 | - $reg = EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
| 41 | - } else { |
|
| 42 | - $reg = null; |
|
| 43 | - } |
|
| 44 | - if (! $reg instanceof EE_Registration |
|
| 45 | - ) { |
|
| 46 | - throw new \EE_Error( |
|
| 47 | - sprintf( |
|
| 48 | - __( |
|
| 49 | - // @codingStandardsIgnoreStart |
|
| 50 | - 'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
| 51 | - // @codingStandardsIgnoreEnd |
|
| 52 | - 'event_espresso' |
|
| 53 | - ), |
|
| 54 | - $wpdb_row['Registration.REG_ID'], |
|
| 55 | - print_r($wpdb_row, true) |
|
| 56 | - ) |
|
| 57 | - ); |
|
| 58 | - } |
|
| 59 | - $datetime_ids = EEM_Datetime::instance()->get_col( |
|
| 60 | - array( |
|
| 61 | - array( |
|
| 62 | - 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
| 63 | - ), |
|
| 64 | - 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
| 65 | - ) |
|
| 66 | - ); |
|
| 67 | - $checkin_stati = array(); |
|
| 68 | - foreach ($datetime_ids as $datetime_id) { |
|
| 69 | - $status = $reg->check_in_status_for_datetime($datetime_id); |
|
| 70 | - switch ($status) { |
|
| 71 | - case EE_Checkin::status_checked_out: |
|
| 72 | - $status_pretty = 'OUT'; |
|
| 73 | - break; |
|
| 74 | - case EE_Checkin::status_checked_in: |
|
| 75 | - $status_pretty = 'IN'; |
|
| 76 | - break; |
|
| 77 | - case EE_Checkin::status_checked_never: |
|
| 78 | - default: |
|
| 79 | - $status_pretty = 'NEVER'; |
|
| 80 | - break; |
|
| 81 | - } |
|
| 82 | - $checkin_stati[$datetime_id] = $status_pretty; |
|
| 83 | - } |
|
| 84 | - return $checkin_stati; |
|
| 85 | - } |
|
| 28 | + /** |
|
| 29 | + * Calculates the checkin status for each datetime this registration has access to |
|
| 30 | + * |
|
| 31 | + * @param array $wpdb_row |
|
| 32 | + * @param \WP_REST_Request $request |
|
| 33 | + * @param Base $controller |
|
| 34 | + * @return array |
|
| 35 | + * @throws \EE_Error |
|
| 36 | + */ |
|
| 37 | + public static function datetimeCheckinStati($wpdb_row, $request, $controller) |
|
| 38 | + { |
|
| 39 | + if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
| 40 | + $reg = EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
| 41 | + } else { |
|
| 42 | + $reg = null; |
|
| 43 | + } |
|
| 44 | + if (! $reg instanceof EE_Registration |
|
| 45 | + ) { |
|
| 46 | + throw new \EE_Error( |
|
| 47 | + sprintf( |
|
| 48 | + __( |
|
| 49 | + // @codingStandardsIgnoreStart |
|
| 50 | + 'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
| 51 | + // @codingStandardsIgnoreEnd |
|
| 52 | + 'event_espresso' |
|
| 53 | + ), |
|
| 54 | + $wpdb_row['Registration.REG_ID'], |
|
| 55 | + print_r($wpdb_row, true) |
|
| 56 | + ) |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | + $datetime_ids = EEM_Datetime::instance()->get_col( |
|
| 60 | + array( |
|
| 61 | + array( |
|
| 62 | + 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
| 63 | + ), |
|
| 64 | + 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
| 65 | + ) |
|
| 66 | + ); |
|
| 67 | + $checkin_stati = array(); |
|
| 68 | + foreach ($datetime_ids as $datetime_id) { |
|
| 69 | + $status = $reg->check_in_status_for_datetime($datetime_id); |
|
| 70 | + switch ($status) { |
|
| 71 | + case EE_Checkin::status_checked_out: |
|
| 72 | + $status_pretty = 'OUT'; |
|
| 73 | + break; |
|
| 74 | + case EE_Checkin::status_checked_in: |
|
| 75 | + $status_pretty = 'IN'; |
|
| 76 | + break; |
|
| 77 | + case EE_Checkin::status_checked_never: |
|
| 78 | + default: |
|
| 79 | + $status_pretty = 'NEVER'; |
|
| 80 | + break; |
|
| 81 | + } |
|
| 82 | + $checkin_stati[$datetime_id] = $status_pretty; |
|
| 83 | + } |
|
| 84 | + return $checkin_stati; |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | use EE_Restriction_Generator_Base; |
| 10 | 10 | use EEH_DTT_Helper; |
| 11 | 11 | |
| 12 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 12 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 13 | 13 | exit('No direct script access allowed'); |
| 14 | 14 | } |
| 15 | 15 | |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | ), |
| 91 | 91 | 'default_currency' => EE_Config::instance()->currency, |
| 92 | 92 | 'authentication' => array( |
| 93 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
| 94 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
| 93 | + 'received_basic_auth_data' => (bool) $wp_json_basic_auth_received_data, |
|
| 94 | + 'insecure_usage_of_basic_auth' => (bool) $insecure_usage_of_basic_auth, |
|
| 95 | 95 | 'warning' => $warning |
| 96 | 96 | ) |
| 97 | 97 | ); |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | use EEH_DTT_Helper; |
| 10 | 10 | |
| 11 | 11 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 12 | - exit('No direct script access allowed'); |
|
| 12 | + exit('No direct script access allowed'); |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | |
@@ -25,82 +25,82 @@ discard block |
||
| 25 | 25 | class Read |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @param WP_REST_Request $request |
|
| 30 | - * @param string $version |
|
| 31 | - * @return EE_Config|WP_Error |
|
| 32 | - */ |
|
| 33 | - public static function handleRequest(WP_REST_Request $request, $version) |
|
| 34 | - { |
|
| 35 | - $cap = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 36 | - if (EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
| 37 | - return EE_Config::instance(); |
|
| 38 | - } else { |
|
| 39 | - return new WP_Error( |
|
| 40 | - 'cannot_read_config', |
|
| 41 | - sprintf( |
|
| 42 | - __( |
|
| 43 | - 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
| 44 | - 'event_espresso' |
|
| 45 | - ), |
|
| 46 | - $cap |
|
| 47 | - ), |
|
| 48 | - array('status' => 403) |
|
| 49 | - ); |
|
| 50 | - } |
|
| 51 | - } |
|
| 28 | + /** |
|
| 29 | + * @param WP_REST_Request $request |
|
| 30 | + * @param string $version |
|
| 31 | + * @return EE_Config|WP_Error |
|
| 32 | + */ |
|
| 33 | + public static function handleRequest(WP_REST_Request $request, $version) |
|
| 34 | + { |
|
| 35 | + $cap = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 36 | + if (EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
| 37 | + return EE_Config::instance(); |
|
| 38 | + } else { |
|
| 39 | + return new WP_Error( |
|
| 40 | + 'cannot_read_config', |
|
| 41 | + sprintf( |
|
| 42 | + __( |
|
| 43 | + 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
| 44 | + 'event_espresso' |
|
| 45 | + ), |
|
| 46 | + $cap |
|
| 47 | + ), |
|
| 48 | + array('status' => 403) |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | |
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Handles the request for public site info |
|
| 57 | - * |
|
| 58 | - * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
| 59 | - * current user could be authenticated using basic auth data |
|
| 60 | - * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
| 61 | - * basic auth data was somehow received |
|
| 62 | - * @param WP_REST_Request $request |
|
| 63 | - * @param string $version |
|
| 64 | - * @return array|WP_Error |
|
| 65 | - */ |
|
| 66 | - public static function handleRequestSiteInfo(WP_REST_Request $request, $version) |
|
| 67 | - { |
|
| 68 | - global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
| 69 | - $insecure_usage_of_basic_auth = apply_filters( |
|
| 70 | - // @codingStandardsIgnoreStart |
|
| 71 | - 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
| 72 | - // @codingStandardsIgnoreEnd |
|
| 73 | - $wp_json_basic_auth_success && ! is_ssl(), |
|
| 74 | - $request |
|
| 75 | - ); |
|
| 76 | - if ($insecure_usage_of_basic_auth) { |
|
| 77 | - $warning = sprintf( |
|
| 78 | - esc_html__( |
|
| 79 | - // @codingStandardsIgnoreStart |
|
| 80 | - 'Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
| 81 | - // @codingStandardsIgnoreEnd |
|
| 82 | - 'event_espresso' |
|
| 83 | - ), |
|
| 84 | - '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
| 85 | - '</a>' |
|
| 86 | - ); |
|
| 87 | - } else { |
|
| 88 | - $warning = ''; |
|
| 89 | - } |
|
| 90 | - return array( |
|
| 91 | - 'default_timezone' => array( |
|
| 92 | - 'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(), |
|
| 93 | - 'string' => get_option('timezone_string'), |
|
| 94 | - 'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
| 95 | - ), |
|
| 96 | - 'default_currency' => EE_Config::instance()->currency, |
|
| 97 | - 'authentication' => array( |
|
| 98 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
| 99 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
| 100 | - 'warning' => $warning |
|
| 101 | - ) |
|
| 102 | - ); |
|
| 103 | - } |
|
| 55 | + /** |
|
| 56 | + * Handles the request for public site info |
|
| 57 | + * |
|
| 58 | + * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
| 59 | + * current user could be authenticated using basic auth data |
|
| 60 | + * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
| 61 | + * basic auth data was somehow received |
|
| 62 | + * @param WP_REST_Request $request |
|
| 63 | + * @param string $version |
|
| 64 | + * @return array|WP_Error |
|
| 65 | + */ |
|
| 66 | + public static function handleRequestSiteInfo(WP_REST_Request $request, $version) |
|
| 67 | + { |
|
| 68 | + global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
| 69 | + $insecure_usage_of_basic_auth = apply_filters( |
|
| 70 | + // @codingStandardsIgnoreStart |
|
| 71 | + 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
| 72 | + // @codingStandardsIgnoreEnd |
|
| 73 | + $wp_json_basic_auth_success && ! is_ssl(), |
|
| 74 | + $request |
|
| 75 | + ); |
|
| 76 | + if ($insecure_usage_of_basic_auth) { |
|
| 77 | + $warning = sprintf( |
|
| 78 | + esc_html__( |
|
| 79 | + // @codingStandardsIgnoreStart |
|
| 80 | + 'Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
| 81 | + // @codingStandardsIgnoreEnd |
|
| 82 | + 'event_espresso' |
|
| 83 | + ), |
|
| 84 | + '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
| 85 | + '</a>' |
|
| 86 | + ); |
|
| 87 | + } else { |
|
| 88 | + $warning = ''; |
|
| 89 | + } |
|
| 90 | + return array( |
|
| 91 | + 'default_timezone' => array( |
|
| 92 | + 'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(), |
|
| 93 | + 'string' => get_option('timezone_string'), |
|
| 94 | + 'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
| 95 | + ), |
|
| 96 | + 'default_currency' => EE_Config::instance()->currency, |
|
| 97 | + 'authentication' => array( |
|
| 98 | + 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
| 99 | + 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
| 100 | + 'warning' => $warning |
|
| 101 | + ) |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // End of file Read.php |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | use EE_Registry; |
| 5 | 5 | use EED_Core_Rest_Api; |
| 6 | 6 | |
| 7 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 7 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 8 | 8 | exit('No direct script access allowed'); |
| 9 | 9 | } |
| 10 | 10 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | */ |
| 98 | 98 | public function __construct($requested_version) |
| 99 | 99 | { |
| 100 | - $this->requested_version = (string)$requested_version; |
|
| 100 | + $this->requested_version = (string) $requested_version; |
|
| 101 | 101 | $this->model_changes = array( |
| 102 | 102 | '4.8.29' => array( |
| 103 | 103 | //first version where the REST API is in EE core, so no need |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | foreach ($extra_fields as $fieldname => $field_data) { |
| 123 | 123 | $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
| 124 | 124 | foreach ($defaults as $attribute => $default_value) { |
| 125 | - if (! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
| 125 | + if ( ! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
| 126 | 126 | $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
| 127 | 127 | } |
| 128 | 128 | } |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | */ |
| 295 | 295 | public function fieldsOnModelInThisVersion($model) |
| 296 | 296 | { |
| 297 | - if (! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
| 297 | + if ( ! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
| 298 | 298 | //get all model changes between the requested version and current core version |
| 299 | 299 | $changes = $this->modelChangesBetweenRequestedVersionAndCurrent(); |
| 300 | 300 | //fetch all fields currently on this model |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | use EED_Core_Rest_Api; |
| 6 | 6 | |
| 7 | 7 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 8 | - exit('No direct script access allowed'); |
|
| 8 | + exit('No direct script access allowed'); |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | |
@@ -26,461 +26,461 @@ discard block |
||
| 26 | 26 | class ModelVersionInfo |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constant used in the $_model_changes array to indicate that a model |
|
| 31 | - * was completely new in this version |
|
| 32 | - */ |
|
| 33 | - const MODEL_ADDED = 'model_added_in_this_version'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
| 37 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
| 38 | - * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
| 39 | - * or it's an array where the values are model field names, |
|
| 40 | - * or API resource properties (ie, non-model fields that appear in REST API results) |
|
| 41 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
| 42 | - * |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - protected $model_changes = array(); |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * top-level keys are version numbers, |
|
| 49 | - * next-level keys are model CLASSNAMES (even parent classnames), |
|
| 50 | - * and next-level keys are extra resource properties to attach to those models' resources, |
|
| 51 | - * and next-level key-value pairs, where the keys are: |
|
| 52 | - * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
| 53 | - * |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $resource_changes = array(); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var string indicating what version of the API was requested |
|
| 60 | - * (eg although core might be at version 4.8.11, they may have sent a request |
|
| 61 | - * for 4.6) |
|
| 62 | - */ |
|
| 63 | - protected $requested_version = null; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Keys are model names, values are their classnames. |
|
| 67 | - * We cache this so we only need to calculate this once per request |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - protected $cached_models_for_requested_version = null; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $cached_model_changes_between_requested_version_and_current = null; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @var array |
|
| 80 | - */ |
|
| 81 | - protected $cached_resource_changes_between_requested_version_and_current = null; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
| 85 | - * and values are the actual field objects |
|
| 86 | - * |
|
| 87 | - * @var array |
|
| 88 | - */ |
|
| 89 | - protected $cached_fields_on_models = array(); |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Model_Version_Info constructor. |
|
| 95 | - * |
|
| 96 | - * @param string $requested_version |
|
| 97 | - */ |
|
| 98 | - public function __construct($requested_version) |
|
| 99 | - { |
|
| 100 | - $this->requested_version = (string)$requested_version; |
|
| 101 | - $this->model_changes = array( |
|
| 102 | - '4.8.29' => array( |
|
| 103 | - //first version where the REST API is in EE core, so no need |
|
| 104 | - //to specify how its different from the previous |
|
| 105 | - ), |
|
| 106 | - ); |
|
| 107 | - //setup data for "extra" fields added onto resources which don't actually exist on models |
|
| 108 | - $this->resource_changes = apply_filters( |
|
| 109 | - 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
| 110 | - array() |
|
| 111 | - ); |
|
| 112 | - $defaults = array( |
|
| 113 | - 'raw' => false, |
|
| 114 | - 'type' => 'N/A', |
|
| 115 | - 'nullable' => true, |
|
| 116 | - 'table_alias' => 'N/A', |
|
| 117 | - 'table_column' => 'N/A', |
|
| 118 | - 'always_available' => true, |
|
| 119 | - ); |
|
| 120 | - foreach ($this->resource_changes as $version => $model_classnames) { |
|
| 121 | - foreach ($model_classnames as $model_classname => $extra_fields) { |
|
| 122 | - foreach ($extra_fields as $fieldname => $field_data) { |
|
| 123 | - $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
| 124 | - foreach ($defaults as $attribute => $default_value) { |
|
| 125 | - if (! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
| 126 | - $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
| 138 | - * indicating exactly what changes happened between the current core version, |
|
| 139 | - * and the version requested |
|
| 140 | - * |
|
| 141 | - * @return array |
|
| 142 | - */ |
|
| 143 | - public function modelChangesBetweenRequestedVersionAndCurrent() |
|
| 144 | - { |
|
| 145 | - if ($this->cached_model_changes_between_requested_version_and_current === null) { |
|
| 146 | - $model_changes = array(); |
|
| 147 | - foreach ($this->modelChanges() as $version => $models_changed_in_version) { |
|
| 148 | - if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
| 149 | - $model_changes[$version] = $models_changed_in_version; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - $this->cached_model_changes_between_requested_version_and_current = $model_changes; |
|
| 153 | - } |
|
| 154 | - return $this->cached_model_changes_between_requested_version_and_current; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
| 161 | - * indicating exactly what changes happened between the current core version, |
|
| 162 | - * and the version requested |
|
| 163 | - * |
|
| 164 | - * @return array |
|
| 165 | - */ |
|
| 166 | - public function resourceChangesBetweenRequestedVersionAndCurrent() |
|
| 167 | - { |
|
| 168 | - if ($this->cached_resource_changes_between_requested_version_and_current === null) { |
|
| 169 | - $resource_changes = array(); |
|
| 170 | - foreach ($this->resourceChanges() as $version => $model_classnames) { |
|
| 171 | - if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
| 172 | - $resource_changes[$version] = $model_classnames; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - $this->cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
| 176 | - } |
|
| 177 | - return $this->cached_resource_changes_between_requested_version_and_current; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
| 184 | - * |
|
| 185 | - * @return string like '4.6' |
|
| 186 | - */ |
|
| 187 | - public function requestedVersion() |
|
| 188 | - { |
|
| 189 | - return $this->requested_version; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Returns an array describing how the models have changed in each version of core |
|
| 196 | - * that supports the API (starting at 4.6) |
|
| 197 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
| 198 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
| 199 | - * and the value is either NULL (indicating the model is completely NEW in this version), |
|
| 200 | - * or it's an array where fields are value names. |
|
| 201 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
| 202 | - * |
|
| 203 | - * @return array |
|
| 204 | - */ |
|
| 205 | - public function modelChanges() |
|
| 206 | - { |
|
| 207 | - return $this->model_changes; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Takes into account the requested version, and the current version, and |
|
| 214 | - * what changed between the two, and tries to return. |
|
| 215 | - * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
| 216 | - * |
|
| 217 | - * @return array keys are model names, values are their classname |
|
| 218 | - */ |
|
| 219 | - public function modelsForRequestedVersion() |
|
| 220 | - { |
|
| 221 | - if ($this->cached_models_for_requested_version === null) { |
|
| 222 | - $all_models_in_current_version = EE_Registry::instance()->non_abstract_db_models; |
|
| 223 | - foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) { |
|
| 224 | - foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
| 225 | - if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) { |
|
| 226 | - unset($all_models_in_current_version[$model_name]); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - $this->cached_models_for_requested_version = apply_filters( |
|
| 231 | - 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
| 232 | - $all_models_in_current_version, |
|
| 233 | - $this |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - return $this->cached_models_for_requested_version; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Determines if this is a valid model name in the requested version. |
|
| 243 | - * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
| 244 | - * version's models into account |
|
| 245 | - * |
|
| 246 | - * @param string $model_name eg 'Event' |
|
| 247 | - * @return boolean |
|
| 248 | - */ |
|
| 249 | - public function isModelNameInThisVersion($model_name) |
|
| 250 | - { |
|
| 251 | - $model_names = $this->modelsForRequestedVersion(); |
|
| 252 | - if (isset($model_names[$model_name])) { |
|
| 253 | - return true; |
|
| 254 | - } else { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
| 263 | - * version's models into account |
|
| 264 | - * |
|
| 265 | - * @param string $model_name |
|
| 266 | - * @return \EEM_Base |
|
| 267 | - * @throws \EE_Error |
|
| 268 | - */ |
|
| 269 | - public function loadModel($model_name) |
|
| 270 | - { |
|
| 271 | - if ($this->isModelNameInThisVersion($model_name)) { |
|
| 272 | - return EE_Registry::instance()->load_model($model_name); |
|
| 273 | - } else { |
|
| 274 | - throw new \EE_Error( |
|
| 275 | - sprintf( |
|
| 276 | - __( |
|
| 277 | - 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
| 278 | - 'event_espresso' |
|
| 279 | - ), |
|
| 280 | - $model_name, |
|
| 281 | - $this->requestedVersion() |
|
| 282 | - ) |
|
| 283 | - ); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Gets all the fields that should exist on this model right now |
|
| 291 | - * |
|
| 292 | - * @param \EEM_Base $model |
|
| 293 | - * @return array|\EE_Model_Field_Base[] |
|
| 294 | - */ |
|
| 295 | - public function fieldsOnModelInThisVersion($model) |
|
| 296 | - { |
|
| 297 | - if (! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
| 298 | - //get all model changes between the requested version and current core version |
|
| 299 | - $changes = $this->modelChangesBetweenRequestedVersionAndCurrent(); |
|
| 300 | - //fetch all fields currently on this model |
|
| 301 | - $current_fields = $model->field_settings(); |
|
| 302 | - //remove all fields that have been added since |
|
| 303 | - foreach ($changes as $version => $changes_in_version) { |
|
| 304 | - if (isset($changes_in_version[$model->get_this_model_name()]) |
|
| 305 | - && $changes_in_version[$model->get_this_model_name()] !== ModelVersionInfo::MODEL_ADDED |
|
| 306 | - ) { |
|
| 307 | - $current_fields = array_diff_key( |
|
| 308 | - $current_fields, |
|
| 309 | - array_flip($changes_in_version[$model->get_this_model_name()]) |
|
| 310 | - ); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - $this->cached_fields_on_models = $current_fields; |
|
| 314 | - } |
|
| 315 | - return $this->cached_fields_on_models; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
| 322 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
| 323 | - * in $classnames |
|
| 324 | - * |
|
| 325 | - * @param object $object |
|
| 326 | - * @param array $classnames |
|
| 327 | - * @return boolean |
|
| 328 | - */ |
|
| 329 | - public function isSubclassOfOne($object, $classnames) |
|
| 330 | - { |
|
| 331 | - foreach ($classnames as $classname) { |
|
| 332 | - if (is_a($object, $classname)) { |
|
| 333 | - return true; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - return false; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Returns the list of model field classes that that the API basically ignores |
|
| 343 | - * |
|
| 344 | - * @return array |
|
| 345 | - */ |
|
| 346 | - public function fieldsIgnored() |
|
| 347 | - { |
|
| 348 | - return apply_filters( |
|
| 349 | - 'FHEE__Controller_Model_Read_fields_ignored', |
|
| 350 | - array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
| 351 | - ); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * If this field one that should be ignored by the API? |
|
| 358 | - * |
|
| 359 | - * @param EE_Model_Field_Base |
|
| 360 | - * @return boolean |
|
| 361 | - */ |
|
| 362 | - public function fieldIsIgnored($field_obj) |
|
| 363 | - { |
|
| 364 | - return $this->isSubclassOfOne($field_obj, $this->fieldsIgnored()); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
| 371 | - * Normally the "raw" versions are only accessible to those who can edit them. |
|
| 372 | - * |
|
| 373 | - * @return array an array of EE_Model_Field_Base child classnames |
|
| 374 | - */ |
|
| 375 | - public function fieldsThatHaveRenderedFormat() |
|
| 376 | - { |
|
| 377 | - return apply_filters( |
|
| 378 | - 'FHEE__Controller_Model_Read__fields_raw', |
|
| 379 | - array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
| 380 | - ); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * If this field one that has a raw format |
|
| 387 | - * |
|
| 388 | - * @param EE_Model_Field_Base |
|
| 389 | - * @return boolean |
|
| 390 | - */ |
|
| 391 | - public function fieldHasRenderedFormat($field_obj) |
|
| 392 | - { |
|
| 393 | - return $this->isSubclassOfOne($field_obj, $this->fieldsThatHaveRenderedFormat()); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
| 400 | - * The pretty version of the field is NOT query-able or editable, but requires no extra permissions |
|
| 401 | - * to view |
|
| 402 | - * |
|
| 403 | - * @return array an array of EE_Model_Field_Base child classnames |
|
| 404 | - */ |
|
| 405 | - public function fieldsThatHavePrettyFormat() |
|
| 406 | - { |
|
| 407 | - return apply_filters( |
|
| 408 | - 'FHEE__Controller_Model_Read__fields_pretty', |
|
| 409 | - array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
| 410 | - ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * If this field one that has a pretty equivalent |
|
| 417 | - * |
|
| 418 | - * @param EE_Model_Field_Base |
|
| 419 | - * @return boolean |
|
| 420 | - */ |
|
| 421 | - public function fieldHasPrettyFormat($field_obj) |
|
| 422 | - { |
|
| 423 | - return $this->isSubclassOfOne($field_obj, $this->fieldsThatHavePrettyFormat()); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Returns an array describing what extra API resource properties have been added through the versions |
|
| 430 | - * |
|
| 431 | - * @return array @see $this->_extra_resource_properties_for_models |
|
| 432 | - */ |
|
| 433 | - public function resourceChanges() |
|
| 434 | - { |
|
| 435 | - return $this->resource_changes; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Returns an array where keys are extra resource properties in this version of the API, |
|
| 442 | - * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
| 443 | - * |
|
| 444 | - * @param \EEM_Base $model |
|
| 445 | - * @return array |
|
| 446 | - */ |
|
| 447 | - public function extraResourcePropertiesForModel($model) |
|
| 448 | - { |
|
| 449 | - $extra_properties = array(); |
|
| 450 | - foreach ($this->resourceChangesBetweenRequestedVersionAndCurrent() as $version => $model_classnames) { |
|
| 451 | - foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
| 452 | - if (is_subclass_of($model, $model_classname)) { |
|
| 453 | - $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - } |
|
| 457 | - return $extra_properties; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Gets all the related models for the specified model. It's good to use this |
|
| 464 | - * in case this model didn't exist for this version or something |
|
| 465 | - * |
|
| 466 | - * @param \EEM_Base $model |
|
| 467 | - * @return \EE_Model_Relation_Base[] |
|
| 468 | - */ |
|
| 469 | - public function relationSettings(\EEM_Base $model) |
|
| 470 | - { |
|
| 471 | - $relations = array(); |
|
| 472 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 473 | - if ($this->isModelNameInThisVersion($relation_name)) { |
|
| 474 | - $relations[$relation_name] = $relation_obj; |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - //filter the results, but use the old filter name |
|
| 478 | - return apply_filters( |
|
| 479 | - 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
| 480 | - $relations, |
|
| 481 | - $model |
|
| 482 | - ); |
|
| 483 | - } |
|
| 29 | + /** |
|
| 30 | + * Constant used in the $_model_changes array to indicate that a model |
|
| 31 | + * was completely new in this version |
|
| 32 | + */ |
|
| 33 | + const MODEL_ADDED = 'model_added_in_this_version'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
| 37 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
| 38 | + * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
| 39 | + * or it's an array where the values are model field names, |
|
| 40 | + * or API resource properties (ie, non-model fields that appear in REST API results) |
|
| 41 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
| 42 | + * |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + protected $model_changes = array(); |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * top-level keys are version numbers, |
|
| 49 | + * next-level keys are model CLASSNAMES (even parent classnames), |
|
| 50 | + * and next-level keys are extra resource properties to attach to those models' resources, |
|
| 51 | + * and next-level key-value pairs, where the keys are: |
|
| 52 | + * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
| 53 | + * |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $resource_changes = array(); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var string indicating what version of the API was requested |
|
| 60 | + * (eg although core might be at version 4.8.11, they may have sent a request |
|
| 61 | + * for 4.6) |
|
| 62 | + */ |
|
| 63 | + protected $requested_version = null; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Keys are model names, values are their classnames. |
|
| 67 | + * We cache this so we only need to calculate this once per request |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + protected $cached_models_for_requested_version = null; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $cached_model_changes_between_requested_version_and_current = null; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @var array |
|
| 80 | + */ |
|
| 81 | + protected $cached_resource_changes_between_requested_version_and_current = null; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
| 85 | + * and values are the actual field objects |
|
| 86 | + * |
|
| 87 | + * @var array |
|
| 88 | + */ |
|
| 89 | + protected $cached_fields_on_models = array(); |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Model_Version_Info constructor. |
|
| 95 | + * |
|
| 96 | + * @param string $requested_version |
|
| 97 | + */ |
|
| 98 | + public function __construct($requested_version) |
|
| 99 | + { |
|
| 100 | + $this->requested_version = (string)$requested_version; |
|
| 101 | + $this->model_changes = array( |
|
| 102 | + '4.8.29' => array( |
|
| 103 | + //first version where the REST API is in EE core, so no need |
|
| 104 | + //to specify how its different from the previous |
|
| 105 | + ), |
|
| 106 | + ); |
|
| 107 | + //setup data for "extra" fields added onto resources which don't actually exist on models |
|
| 108 | + $this->resource_changes = apply_filters( |
|
| 109 | + 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
| 110 | + array() |
|
| 111 | + ); |
|
| 112 | + $defaults = array( |
|
| 113 | + 'raw' => false, |
|
| 114 | + 'type' => 'N/A', |
|
| 115 | + 'nullable' => true, |
|
| 116 | + 'table_alias' => 'N/A', |
|
| 117 | + 'table_column' => 'N/A', |
|
| 118 | + 'always_available' => true, |
|
| 119 | + ); |
|
| 120 | + foreach ($this->resource_changes as $version => $model_classnames) { |
|
| 121 | + foreach ($model_classnames as $model_classname => $extra_fields) { |
|
| 122 | + foreach ($extra_fields as $fieldname => $field_data) { |
|
| 123 | + $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
| 124 | + foreach ($defaults as $attribute => $default_value) { |
|
| 125 | + if (! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
| 126 | + $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
| 138 | + * indicating exactly what changes happened between the current core version, |
|
| 139 | + * and the version requested |
|
| 140 | + * |
|
| 141 | + * @return array |
|
| 142 | + */ |
|
| 143 | + public function modelChangesBetweenRequestedVersionAndCurrent() |
|
| 144 | + { |
|
| 145 | + if ($this->cached_model_changes_between_requested_version_and_current === null) { |
|
| 146 | + $model_changes = array(); |
|
| 147 | + foreach ($this->modelChanges() as $version => $models_changed_in_version) { |
|
| 148 | + if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
| 149 | + $model_changes[$version] = $models_changed_in_version; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + $this->cached_model_changes_between_requested_version_and_current = $model_changes; |
|
| 153 | + } |
|
| 154 | + return $this->cached_model_changes_between_requested_version_and_current; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
| 161 | + * indicating exactly what changes happened between the current core version, |
|
| 162 | + * and the version requested |
|
| 163 | + * |
|
| 164 | + * @return array |
|
| 165 | + */ |
|
| 166 | + public function resourceChangesBetweenRequestedVersionAndCurrent() |
|
| 167 | + { |
|
| 168 | + if ($this->cached_resource_changes_between_requested_version_and_current === null) { |
|
| 169 | + $resource_changes = array(); |
|
| 170 | + foreach ($this->resourceChanges() as $version => $model_classnames) { |
|
| 171 | + if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
| 172 | + $resource_changes[$version] = $model_classnames; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + $this->cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
| 176 | + } |
|
| 177 | + return $this->cached_resource_changes_between_requested_version_and_current; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
| 184 | + * |
|
| 185 | + * @return string like '4.6' |
|
| 186 | + */ |
|
| 187 | + public function requestedVersion() |
|
| 188 | + { |
|
| 189 | + return $this->requested_version; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Returns an array describing how the models have changed in each version of core |
|
| 196 | + * that supports the API (starting at 4.6) |
|
| 197 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
| 198 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
| 199 | + * and the value is either NULL (indicating the model is completely NEW in this version), |
|
| 200 | + * or it's an array where fields are value names. |
|
| 201 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
| 202 | + * |
|
| 203 | + * @return array |
|
| 204 | + */ |
|
| 205 | + public function modelChanges() |
|
| 206 | + { |
|
| 207 | + return $this->model_changes; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Takes into account the requested version, and the current version, and |
|
| 214 | + * what changed between the two, and tries to return. |
|
| 215 | + * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
| 216 | + * |
|
| 217 | + * @return array keys are model names, values are their classname |
|
| 218 | + */ |
|
| 219 | + public function modelsForRequestedVersion() |
|
| 220 | + { |
|
| 221 | + if ($this->cached_models_for_requested_version === null) { |
|
| 222 | + $all_models_in_current_version = EE_Registry::instance()->non_abstract_db_models; |
|
| 223 | + foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) { |
|
| 224 | + foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
| 225 | + if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) { |
|
| 226 | + unset($all_models_in_current_version[$model_name]); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + $this->cached_models_for_requested_version = apply_filters( |
|
| 231 | + 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
| 232 | + $all_models_in_current_version, |
|
| 233 | + $this |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + return $this->cached_models_for_requested_version; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Determines if this is a valid model name in the requested version. |
|
| 243 | + * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
| 244 | + * version's models into account |
|
| 245 | + * |
|
| 246 | + * @param string $model_name eg 'Event' |
|
| 247 | + * @return boolean |
|
| 248 | + */ |
|
| 249 | + public function isModelNameInThisVersion($model_name) |
|
| 250 | + { |
|
| 251 | + $model_names = $this->modelsForRequestedVersion(); |
|
| 252 | + if (isset($model_names[$model_name])) { |
|
| 253 | + return true; |
|
| 254 | + } else { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
| 263 | + * version's models into account |
|
| 264 | + * |
|
| 265 | + * @param string $model_name |
|
| 266 | + * @return \EEM_Base |
|
| 267 | + * @throws \EE_Error |
|
| 268 | + */ |
|
| 269 | + public function loadModel($model_name) |
|
| 270 | + { |
|
| 271 | + if ($this->isModelNameInThisVersion($model_name)) { |
|
| 272 | + return EE_Registry::instance()->load_model($model_name); |
|
| 273 | + } else { |
|
| 274 | + throw new \EE_Error( |
|
| 275 | + sprintf( |
|
| 276 | + __( |
|
| 277 | + 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
| 278 | + 'event_espresso' |
|
| 279 | + ), |
|
| 280 | + $model_name, |
|
| 281 | + $this->requestedVersion() |
|
| 282 | + ) |
|
| 283 | + ); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Gets all the fields that should exist on this model right now |
|
| 291 | + * |
|
| 292 | + * @param \EEM_Base $model |
|
| 293 | + * @return array|\EE_Model_Field_Base[] |
|
| 294 | + */ |
|
| 295 | + public function fieldsOnModelInThisVersion($model) |
|
| 296 | + { |
|
| 297 | + if (! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
| 298 | + //get all model changes between the requested version and current core version |
|
| 299 | + $changes = $this->modelChangesBetweenRequestedVersionAndCurrent(); |
|
| 300 | + //fetch all fields currently on this model |
|
| 301 | + $current_fields = $model->field_settings(); |
|
| 302 | + //remove all fields that have been added since |
|
| 303 | + foreach ($changes as $version => $changes_in_version) { |
|
| 304 | + if (isset($changes_in_version[$model->get_this_model_name()]) |
|
| 305 | + && $changes_in_version[$model->get_this_model_name()] !== ModelVersionInfo::MODEL_ADDED |
|
| 306 | + ) { |
|
| 307 | + $current_fields = array_diff_key( |
|
| 308 | + $current_fields, |
|
| 309 | + array_flip($changes_in_version[$model->get_this_model_name()]) |
|
| 310 | + ); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + $this->cached_fields_on_models = $current_fields; |
|
| 314 | + } |
|
| 315 | + return $this->cached_fields_on_models; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
| 322 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
| 323 | + * in $classnames |
|
| 324 | + * |
|
| 325 | + * @param object $object |
|
| 326 | + * @param array $classnames |
|
| 327 | + * @return boolean |
|
| 328 | + */ |
|
| 329 | + public function isSubclassOfOne($object, $classnames) |
|
| 330 | + { |
|
| 331 | + foreach ($classnames as $classname) { |
|
| 332 | + if (is_a($object, $classname)) { |
|
| 333 | + return true; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + return false; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Returns the list of model field classes that that the API basically ignores |
|
| 343 | + * |
|
| 344 | + * @return array |
|
| 345 | + */ |
|
| 346 | + public function fieldsIgnored() |
|
| 347 | + { |
|
| 348 | + return apply_filters( |
|
| 349 | + 'FHEE__Controller_Model_Read_fields_ignored', |
|
| 350 | + array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
| 351 | + ); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * If this field one that should be ignored by the API? |
|
| 358 | + * |
|
| 359 | + * @param EE_Model_Field_Base |
|
| 360 | + * @return boolean |
|
| 361 | + */ |
|
| 362 | + public function fieldIsIgnored($field_obj) |
|
| 363 | + { |
|
| 364 | + return $this->isSubclassOfOne($field_obj, $this->fieldsIgnored()); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
| 371 | + * Normally the "raw" versions are only accessible to those who can edit them. |
|
| 372 | + * |
|
| 373 | + * @return array an array of EE_Model_Field_Base child classnames |
|
| 374 | + */ |
|
| 375 | + public function fieldsThatHaveRenderedFormat() |
|
| 376 | + { |
|
| 377 | + return apply_filters( |
|
| 378 | + 'FHEE__Controller_Model_Read__fields_raw', |
|
| 379 | + array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
| 380 | + ); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * If this field one that has a raw format |
|
| 387 | + * |
|
| 388 | + * @param EE_Model_Field_Base |
|
| 389 | + * @return boolean |
|
| 390 | + */ |
|
| 391 | + public function fieldHasRenderedFormat($field_obj) |
|
| 392 | + { |
|
| 393 | + return $this->isSubclassOfOne($field_obj, $this->fieldsThatHaveRenderedFormat()); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
| 400 | + * The pretty version of the field is NOT query-able or editable, but requires no extra permissions |
|
| 401 | + * to view |
|
| 402 | + * |
|
| 403 | + * @return array an array of EE_Model_Field_Base child classnames |
|
| 404 | + */ |
|
| 405 | + public function fieldsThatHavePrettyFormat() |
|
| 406 | + { |
|
| 407 | + return apply_filters( |
|
| 408 | + 'FHEE__Controller_Model_Read__fields_pretty', |
|
| 409 | + array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
| 410 | + ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * If this field one that has a pretty equivalent |
|
| 417 | + * |
|
| 418 | + * @param EE_Model_Field_Base |
|
| 419 | + * @return boolean |
|
| 420 | + */ |
|
| 421 | + public function fieldHasPrettyFormat($field_obj) |
|
| 422 | + { |
|
| 423 | + return $this->isSubclassOfOne($field_obj, $this->fieldsThatHavePrettyFormat()); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Returns an array describing what extra API resource properties have been added through the versions |
|
| 430 | + * |
|
| 431 | + * @return array @see $this->_extra_resource_properties_for_models |
|
| 432 | + */ |
|
| 433 | + public function resourceChanges() |
|
| 434 | + { |
|
| 435 | + return $this->resource_changes; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Returns an array where keys are extra resource properties in this version of the API, |
|
| 442 | + * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
| 443 | + * |
|
| 444 | + * @param \EEM_Base $model |
|
| 445 | + * @return array |
|
| 446 | + */ |
|
| 447 | + public function extraResourcePropertiesForModel($model) |
|
| 448 | + { |
|
| 449 | + $extra_properties = array(); |
|
| 450 | + foreach ($this->resourceChangesBetweenRequestedVersionAndCurrent() as $version => $model_classnames) { |
|
| 451 | + foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
| 452 | + if (is_subclass_of($model, $model_classname)) { |
|
| 453 | + $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + } |
|
| 457 | + return $extra_properties; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Gets all the related models for the specified model. It's good to use this |
|
| 464 | + * in case this model didn't exist for this version or something |
|
| 465 | + * |
|
| 466 | + * @param \EEM_Base $model |
|
| 467 | + * @return \EE_Model_Relation_Base[] |
|
| 468 | + */ |
|
| 469 | + public function relationSettings(\EEM_Base $model) |
|
| 470 | + { |
|
| 471 | + $relations = array(); |
|
| 472 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 473 | + if ($this->isModelNameInThisVersion($relation_name)) { |
|
| 474 | + $relations[$relation_name] = $relation_obj; |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + //filter the results, but use the old filter name |
|
| 478 | + return apply_filters( |
|
| 479 | + 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
| 480 | + $relations, |
|
| 481 | + $model |
|
| 482 | + ); |
|
| 483 | + } |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | // End of file Model_Version_Info.php |