@@ -62,49 +62,49 @@ discard block |
||
| 62 | 62 | ); |
| 63 | 63 | $cpt_models = array_keys(EE_Registry::instance()->cpt_models()); |
| 64 | 64 | foreach ($cpt_models as $model_name) { |
| 65 | - $this->_model_relations[ $model_name ] = new EE_HABTM_Relation('Term_Relationship'); |
|
| 65 | + $this->_model_relations[$model_name] = new EE_HABTM_Relation('Term_Relationship'); |
|
| 66 | 66 | } |
| 67 | 67 | $this->_wp_core_model = true; |
| 68 | 68 | $this->_indexes = array( |
| 69 | 69 | 'term_id_taxonomy' => new EE_Unique_Index(array('term_id', 'taxonomy')), |
| 70 | 70 | ); |
| 71 | 71 | $path_to_tax_model = ''; |
| 72 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 73 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Taxonomy_Protected( |
|
| 72 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
| 73 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Taxonomy_Protected( |
|
| 74 | 74 | $path_to_tax_model |
| 75 | 75 | ); |
| 76 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = false; |
|
| 77 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = false; |
|
| 76 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = false; |
|
| 77 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = false; |
|
| 78 | 78 | // add cap restrictions for editing relating to the "ee_edit_*" |
| 79 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_category'] = new EE_Default_Where_Conditions( |
|
| 79 | + $this->_cap_restrictions[EEM_Base::caps_edit]['ee_edit_event_category'] = new EE_Default_Where_Conditions( |
|
| 80 | 80 | array( |
| 81 | - $path_to_tax_model . 'taxonomy*ee_edit_event_category' => array('!=', 'espresso_event_categories'), |
|
| 81 | + $path_to_tax_model.'taxonomy*ee_edit_event_category' => array('!=', 'espresso_event_categories'), |
|
| 82 | 82 | ) |
| 83 | 83 | ); |
| 84 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_venue_category'] = new EE_Default_Where_Conditions( |
|
| 84 | + $this->_cap_restrictions[EEM_Base::caps_edit]['ee_edit_venue_category'] = new EE_Default_Where_Conditions( |
|
| 85 | 85 | array( |
| 86 | - $path_to_tax_model . 'taxonomy*ee_edit_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 86 | + $path_to_tax_model.'taxonomy*ee_edit_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 87 | 87 | ) |
| 88 | 88 | ); |
| 89 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_type'] = new EE_Default_Where_Conditions( |
|
| 89 | + $this->_cap_restrictions[EEM_Base::caps_edit]['ee_edit_event_type'] = new EE_Default_Where_Conditions( |
|
| 90 | 90 | array( |
| 91 | - $path_to_tax_model . 'taxonomy*ee_edit_event_type' => array('!=', 'espresso_event_type'), |
|
| 91 | + $path_to_tax_model.'taxonomy*ee_edit_event_type' => array('!=', 'espresso_event_type'), |
|
| 92 | 92 | ) |
| 93 | 93 | ); |
| 94 | 94 | // add cap restrictions for deleting relating to the "ee_deleting_*" |
| 95 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_category'] = new EE_Default_Where_Conditions( |
|
| 95 | + $this->_cap_restrictions[EEM_Base::caps_delete]['ee_delete_event_category'] = new EE_Default_Where_Conditions( |
|
| 96 | 96 | array( |
| 97 | - $path_to_tax_model . 'taxonomy*ee_delete_event_category' => array('!=', 'espresso_event_categories'), |
|
| 97 | + $path_to_tax_model.'taxonomy*ee_delete_event_category' => array('!=', 'espresso_event_categories'), |
|
| 98 | 98 | ) |
| 99 | 99 | ); |
| 100 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_venue_category'] = new EE_Default_Where_Conditions( |
|
| 100 | + $this->_cap_restrictions[EEM_Base::caps_delete]['ee_delete_venue_category'] = new EE_Default_Where_Conditions( |
|
| 101 | 101 | array( |
| 102 | - $path_to_tax_model . 'taxonomy*ee_delete_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 102 | + $path_to_tax_model.'taxonomy*ee_delete_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 103 | 103 | ) |
| 104 | 104 | ); |
| 105 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_type'] = new EE_Default_Where_Conditions( |
|
| 105 | + $this->_cap_restrictions[EEM_Base::caps_delete]['ee_delete_event_type'] = new EE_Default_Where_Conditions( |
|
| 106 | 106 | array( |
| 107 | - $path_to_tax_model . 'taxonomy*ee_delete_event_type' => array('!=', 'espresso_event_type'), |
|
| 107 | + $path_to_tax_model.'taxonomy*ee_delete_event_type' => array('!=', 'espresso_event_type'), |
|
| 108 | 108 | ) |
| 109 | 109 | ); |
| 110 | 110 | parent::__construct($timezone); |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | { |
| 127 | 127 | if ($model === EEM_Term_Taxonomy::instance()) { |
| 128 | 128 | $taxonomies = get_taxonomies(array('show_in_rest' => true)); |
| 129 | - if (! empty($taxonomies)) { |
|
| 129 | + if ( ! empty($taxonomies)) { |
|
| 130 | 130 | $model_query_params[0]['taxonomy'] = array('IN', $taxonomies); |
| 131 | 131 | } |
| 132 | 132 | } |
@@ -10,133 +10,133 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class EEM_Term_Taxonomy extends EEM_Base |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var EEM_Term_Taxonomy |
|
| 15 | - */ |
|
| 16 | - protected static $_instance = null; |
|
| 13 | + /** |
|
| 14 | + * @var EEM_Term_Taxonomy |
|
| 15 | + */ |
|
| 16 | + protected static $_instance = null; |
|
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param $timezone |
|
| 21 | - * @throws EE_Error |
|
| 22 | - */ |
|
| 23 | - protected function __construct($timezone = null) |
|
| 24 | - { |
|
| 25 | - $this->singular_item = esc_html__('Term Taxonomy', 'event_espresso'); |
|
| 26 | - $this->plural_item = esc_html__('Term Taxonomy', 'event_espresso'); |
|
| 27 | - $this->_tables = array( |
|
| 28 | - 'Term_Taxonomy' => new EE_Primary_Table('term_taxonomy', 'term_taxonomy_id'), |
|
| 29 | - ); |
|
| 30 | - $this->_fields = array( |
|
| 31 | - 'Term_Taxonomy' => array( |
|
| 32 | - 'term_taxonomy_id' => new EE_Primary_Key_Int_Field( |
|
| 33 | - 'term_taxonomy_id', |
|
| 34 | - esc_html__('Term-Taxonomy ID', 'event_espresso') |
|
| 35 | - ), |
|
| 36 | - 'term_id' => new EE_Foreign_Key_Int_Field( |
|
| 37 | - 'term_id', |
|
| 38 | - esc_html__("Term Id", "event_espresso"), |
|
| 39 | - false, |
|
| 40 | - 0, |
|
| 41 | - 'Term' |
|
| 42 | - ), |
|
| 43 | - 'taxonomy' => new EE_Plain_Text_Field( |
|
| 44 | - 'taxonomy', |
|
| 45 | - esc_html__('Taxonomy Name', 'event_espresso'), |
|
| 46 | - false, |
|
| 47 | - 'category' |
|
| 48 | - ), |
|
| 49 | - 'description' => new EE_Post_Content_Field( |
|
| 50 | - 'description', |
|
| 51 | - esc_html__("Description of Term", "event_espresso"), |
|
| 52 | - false, |
|
| 53 | - '' |
|
| 54 | - ), |
|
| 55 | - 'parent' => new EE_Integer_Field('parent', esc_html__("Parent Term ID", "event_espresso"), false, 0), |
|
| 56 | - 'term_count' => new EE_Integer_Field( |
|
| 57 | - 'count', |
|
| 58 | - esc_html__("Count of Objects attached", 'event_espresso'), |
|
| 59 | - false, |
|
| 60 | - 0 |
|
| 61 | - ), |
|
| 62 | - ), |
|
| 63 | - ); |
|
| 64 | - $this->_model_relations = array( |
|
| 65 | - 'Term_Relationship' => new EE_Has_Many_Relation(), |
|
| 66 | - 'Term' => new EE_Belongs_To_Relation(), |
|
| 67 | - ); |
|
| 68 | - $cpt_models = array_keys(EE_Registry::instance()->cpt_models()); |
|
| 69 | - foreach ($cpt_models as $model_name) { |
|
| 70 | - $this->_model_relations[ $model_name ] = new EE_HABTM_Relation('Term_Relationship'); |
|
| 71 | - } |
|
| 72 | - $this->_wp_core_model = true; |
|
| 73 | - $this->_indexes = array( |
|
| 74 | - 'term_id_taxonomy' => new EE_Unique_Index(array('term_id', 'taxonomy')), |
|
| 75 | - ); |
|
| 76 | - $path_to_tax_model = ''; |
|
| 77 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 78 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Taxonomy_Protected( |
|
| 79 | - $path_to_tax_model |
|
| 80 | - ); |
|
| 81 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = false; |
|
| 82 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = false; |
|
| 83 | - // add cap restrictions for editing relating to the "ee_edit_*" |
|
| 84 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_category'] = new EE_Default_Where_Conditions( |
|
| 85 | - array( |
|
| 86 | - $path_to_tax_model . 'taxonomy*ee_edit_event_category' => array('!=', 'espresso_event_categories'), |
|
| 87 | - ) |
|
| 88 | - ); |
|
| 89 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_venue_category'] = new EE_Default_Where_Conditions( |
|
| 90 | - array( |
|
| 91 | - $path_to_tax_model . 'taxonomy*ee_edit_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_type'] = new EE_Default_Where_Conditions( |
|
| 95 | - array( |
|
| 96 | - $path_to_tax_model . 'taxonomy*ee_edit_event_type' => array('!=', 'espresso_event_type'), |
|
| 97 | - ) |
|
| 98 | - ); |
|
| 99 | - // add cap restrictions for deleting relating to the "ee_deleting_*" |
|
| 100 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_category'] = new EE_Default_Where_Conditions( |
|
| 101 | - array( |
|
| 102 | - $path_to_tax_model . 'taxonomy*ee_delete_event_category' => array('!=', 'espresso_event_categories'), |
|
| 103 | - ) |
|
| 104 | - ); |
|
| 105 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_venue_category'] = new EE_Default_Where_Conditions( |
|
| 106 | - array( |
|
| 107 | - $path_to_tax_model . 'taxonomy*ee_delete_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 108 | - ) |
|
| 109 | - ); |
|
| 110 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_type'] = new EE_Default_Where_Conditions( |
|
| 111 | - array( |
|
| 112 | - $path_to_tax_model . 'taxonomy*ee_delete_event_type' => array('!=', 'espresso_event_type'), |
|
| 113 | - ) |
|
| 114 | - ); |
|
| 115 | - parent::__construct($timezone); |
|
| 116 | - add_filter('FHEE__Read__create_model_query_params', array('EEM_Term_Taxonomy', 'rest_api_query_params'), 10, 3); |
|
| 117 | - } |
|
| 19 | + /** |
|
| 20 | + * @param $timezone |
|
| 21 | + * @throws EE_Error |
|
| 22 | + */ |
|
| 23 | + protected function __construct($timezone = null) |
|
| 24 | + { |
|
| 25 | + $this->singular_item = esc_html__('Term Taxonomy', 'event_espresso'); |
|
| 26 | + $this->plural_item = esc_html__('Term Taxonomy', 'event_espresso'); |
|
| 27 | + $this->_tables = array( |
|
| 28 | + 'Term_Taxonomy' => new EE_Primary_Table('term_taxonomy', 'term_taxonomy_id'), |
|
| 29 | + ); |
|
| 30 | + $this->_fields = array( |
|
| 31 | + 'Term_Taxonomy' => array( |
|
| 32 | + 'term_taxonomy_id' => new EE_Primary_Key_Int_Field( |
|
| 33 | + 'term_taxonomy_id', |
|
| 34 | + esc_html__('Term-Taxonomy ID', 'event_espresso') |
|
| 35 | + ), |
|
| 36 | + 'term_id' => new EE_Foreign_Key_Int_Field( |
|
| 37 | + 'term_id', |
|
| 38 | + esc_html__("Term Id", "event_espresso"), |
|
| 39 | + false, |
|
| 40 | + 0, |
|
| 41 | + 'Term' |
|
| 42 | + ), |
|
| 43 | + 'taxonomy' => new EE_Plain_Text_Field( |
|
| 44 | + 'taxonomy', |
|
| 45 | + esc_html__('Taxonomy Name', 'event_espresso'), |
|
| 46 | + false, |
|
| 47 | + 'category' |
|
| 48 | + ), |
|
| 49 | + 'description' => new EE_Post_Content_Field( |
|
| 50 | + 'description', |
|
| 51 | + esc_html__("Description of Term", "event_espresso"), |
|
| 52 | + false, |
|
| 53 | + '' |
|
| 54 | + ), |
|
| 55 | + 'parent' => new EE_Integer_Field('parent', esc_html__("Parent Term ID", "event_espresso"), false, 0), |
|
| 56 | + 'term_count' => new EE_Integer_Field( |
|
| 57 | + 'count', |
|
| 58 | + esc_html__("Count of Objects attached", 'event_espresso'), |
|
| 59 | + false, |
|
| 60 | + 0 |
|
| 61 | + ), |
|
| 62 | + ), |
|
| 63 | + ); |
|
| 64 | + $this->_model_relations = array( |
|
| 65 | + 'Term_Relationship' => new EE_Has_Many_Relation(), |
|
| 66 | + 'Term' => new EE_Belongs_To_Relation(), |
|
| 67 | + ); |
|
| 68 | + $cpt_models = array_keys(EE_Registry::instance()->cpt_models()); |
|
| 69 | + foreach ($cpt_models as $model_name) { |
|
| 70 | + $this->_model_relations[ $model_name ] = new EE_HABTM_Relation('Term_Relationship'); |
|
| 71 | + } |
|
| 72 | + $this->_wp_core_model = true; |
|
| 73 | + $this->_indexes = array( |
|
| 74 | + 'term_id_taxonomy' => new EE_Unique_Index(array('term_id', 'taxonomy')), |
|
| 75 | + ); |
|
| 76 | + $path_to_tax_model = ''; |
|
| 77 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 78 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Taxonomy_Protected( |
|
| 79 | + $path_to_tax_model |
|
| 80 | + ); |
|
| 81 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = false; |
|
| 82 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = false; |
|
| 83 | + // add cap restrictions for editing relating to the "ee_edit_*" |
|
| 84 | + $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_category'] = new EE_Default_Where_Conditions( |
|
| 85 | + array( |
|
| 86 | + $path_to_tax_model . 'taxonomy*ee_edit_event_category' => array('!=', 'espresso_event_categories'), |
|
| 87 | + ) |
|
| 88 | + ); |
|
| 89 | + $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_venue_category'] = new EE_Default_Where_Conditions( |
|
| 90 | + array( |
|
| 91 | + $path_to_tax_model . 'taxonomy*ee_edit_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_type'] = new EE_Default_Where_Conditions( |
|
| 95 | + array( |
|
| 96 | + $path_to_tax_model . 'taxonomy*ee_edit_event_type' => array('!=', 'espresso_event_type'), |
|
| 97 | + ) |
|
| 98 | + ); |
|
| 99 | + // add cap restrictions for deleting relating to the "ee_deleting_*" |
|
| 100 | + $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_category'] = new EE_Default_Where_Conditions( |
|
| 101 | + array( |
|
| 102 | + $path_to_tax_model . 'taxonomy*ee_delete_event_category' => array('!=', 'espresso_event_categories'), |
|
| 103 | + ) |
|
| 104 | + ); |
|
| 105 | + $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_venue_category'] = new EE_Default_Where_Conditions( |
|
| 106 | + array( |
|
| 107 | + $path_to_tax_model . 'taxonomy*ee_delete_venue_category' => array('!=', 'espresso_venue_categories'), |
|
| 108 | + ) |
|
| 109 | + ); |
|
| 110 | + $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_type'] = new EE_Default_Where_Conditions( |
|
| 111 | + array( |
|
| 112 | + $path_to_tax_model . 'taxonomy*ee_delete_event_type' => array('!=', 'espresso_event_type'), |
|
| 113 | + ) |
|
| 114 | + ); |
|
| 115 | + parent::__construct($timezone); |
|
| 116 | + add_filter('FHEE__Read__create_model_query_params', array('EEM_Term_Taxonomy', 'rest_api_query_params'), 10, 3); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Makes sure that during REST API queries, we only return term-taxonomies |
|
| 122 | - * for term taxonomies which should be shown in the rest api |
|
| 123 | - * |
|
| 124 | - * @param array $model_query_params |
|
| 125 | - * @param array $querystring_query_params |
|
| 126 | - * @param EEM_Base $model |
|
| 127 | - * @return array |
|
| 128 | - * @throws EE_Error |
|
| 129 | - */ |
|
| 130 | - public static function rest_api_query_params($model_query_params, $querystring_query_params, $model) |
|
| 131 | - { |
|
| 132 | - if ($model === EEM_Term_Taxonomy::instance()) { |
|
| 133 | - $taxonomies = get_taxonomies(array('show_in_rest' => true)); |
|
| 134 | - if (! empty($taxonomies)) { |
|
| 135 | - $model_query_params[0]['taxonomy'] = array('IN', $taxonomies); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - return $model_query_params; |
|
| 139 | - } |
|
| 120 | + /** |
|
| 121 | + * Makes sure that during REST API queries, we only return term-taxonomies |
|
| 122 | + * for term taxonomies which should be shown in the rest api |
|
| 123 | + * |
|
| 124 | + * @param array $model_query_params |
|
| 125 | + * @param array $querystring_query_params |
|
| 126 | + * @param EEM_Base $model |
|
| 127 | + * @return array |
|
| 128 | + * @throws EE_Error |
|
| 129 | + */ |
|
| 130 | + public static function rest_api_query_params($model_query_params, $querystring_query_params, $model) |
|
| 131 | + { |
|
| 132 | + if ($model === EEM_Term_Taxonomy::instance()) { |
|
| 133 | + $taxonomies = get_taxonomies(array('show_in_rest' => true)); |
|
| 134 | + if (! empty($taxonomies)) { |
|
| 135 | + $model_query_params[0]['taxonomy'] = array('IN', $taxonomies); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + return $model_query_params; |
|
| 139 | + } |
|
| 140 | 140 | } |
| 141 | 141 | // End of file EEM_Term_Taxonomy.model.php |
| 142 | 142 | // Location: /includes/models/EEM_Term_Taxonomy.model.php |
@@ -38,10 +38,10 @@ |
||
| 38 | 38 | )); |
| 39 | 39 | $this->_model_relations = array(); |
| 40 | 40 | foreach ($models_this_can_attach_to as $model) { |
| 41 | - $this->_model_relations[ $model ] = new EE_Belongs_To_Any_Relation(); |
|
| 41 | + $this->_model_relations[$model] = new EE_Belongs_To_Any_Relation(); |
|
| 42 | 42 | } |
| 43 | 43 | foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
| 44 | - $this->_cap_restriction_generators[ $cap_context ] = new EE_Restriction_Generator_Meta('EXM_key', 'EXM_value'); |
|
| 44 | + $this->_cap_restriction_generators[$cap_context] = new EE_Restriction_Generator_Meta('EXM_key', 'EXM_value'); |
|
| 45 | 45 | } |
| 46 | 46 | parent::__construct($timezone); |
| 47 | 47 | } |
@@ -16,33 +16,33 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class EEM_Extra_Meta extends EEM_Base |
| 18 | 18 | { |
| 19 | - // private instance of the Attendee object |
|
| 20 | - protected static $_instance = null; |
|
| 19 | + // private instance of the Attendee object |
|
| 20 | + protected static $_instance = null; |
|
| 21 | 21 | |
| 22 | - protected function __construct($timezone = null) |
|
| 23 | - { |
|
| 24 | - $this->singular_item = esc_html__('Extra Meta', 'event_espresso'); |
|
| 25 | - $this->plural_item = esc_html__('Extra Metas', 'event_espresso'); |
|
| 26 | - $this->_tables = array( |
|
| 27 | - 'Extra_Meta' => new EE_Primary_Table('esp_extra_meta', 'EXM_ID') |
|
| 28 | - ); |
|
| 29 | - $models_this_can_attach_to = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
| 30 | - $this->_fields = array( |
|
| 31 | - 'Extra_Meta' => array( |
|
| 32 | - 'EXM_ID' => new EE_Primary_Key_Int_Field('EXM_ID', esc_html__("Extra Meta ID", "event_espresso")), |
|
| 33 | - 'OBJ_ID' => new EE_Foreign_Key_Int_Field('OBJ_ID', esc_html__("Primary Key of Attached Thing", "event_espresso"), false, 0, $models_this_can_attach_to), |
|
| 34 | - 'EXM_type' => new EE_Any_Foreign_Model_Name_Field('EXM_type', esc_html__("Model of Attached Thing", "event_espresso"), false, 'Transaction', $models_this_can_attach_to), |
|
| 35 | - 'EXM_key' => new EE_Plain_Text_Field('EXM_key', esc_html__("Meta Key", "event_espresso"), false, ''), |
|
| 36 | - 'EXM_value' => new EE_Maybe_Serialized_Text_Field('EXM_value', esc_html__("Meta Value", "event_espresso"), true) |
|
| 22 | + protected function __construct($timezone = null) |
|
| 23 | + { |
|
| 24 | + $this->singular_item = esc_html__('Extra Meta', 'event_espresso'); |
|
| 25 | + $this->plural_item = esc_html__('Extra Metas', 'event_espresso'); |
|
| 26 | + $this->_tables = array( |
|
| 27 | + 'Extra_Meta' => new EE_Primary_Table('esp_extra_meta', 'EXM_ID') |
|
| 28 | + ); |
|
| 29 | + $models_this_can_attach_to = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
| 30 | + $this->_fields = array( |
|
| 31 | + 'Extra_Meta' => array( |
|
| 32 | + 'EXM_ID' => new EE_Primary_Key_Int_Field('EXM_ID', esc_html__("Extra Meta ID", "event_espresso")), |
|
| 33 | + 'OBJ_ID' => new EE_Foreign_Key_Int_Field('OBJ_ID', esc_html__("Primary Key of Attached Thing", "event_espresso"), false, 0, $models_this_can_attach_to), |
|
| 34 | + 'EXM_type' => new EE_Any_Foreign_Model_Name_Field('EXM_type', esc_html__("Model of Attached Thing", "event_espresso"), false, 'Transaction', $models_this_can_attach_to), |
|
| 35 | + 'EXM_key' => new EE_Plain_Text_Field('EXM_key', esc_html__("Meta Key", "event_espresso"), false, ''), |
|
| 36 | + 'EXM_value' => new EE_Maybe_Serialized_Text_Field('EXM_value', esc_html__("Meta Value", "event_espresso"), true) |
|
| 37 | 37 | |
| 38 | - )); |
|
| 39 | - $this->_model_relations = array(); |
|
| 40 | - foreach ($models_this_can_attach_to as $model) { |
|
| 41 | - $this->_model_relations[ $model ] = new EE_Belongs_To_Any_Relation(); |
|
| 42 | - } |
|
| 43 | - foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 44 | - $this->_cap_restriction_generators[ $cap_context ] = new EE_Restriction_Generator_Meta('EXM_key', 'EXM_value'); |
|
| 45 | - } |
|
| 46 | - parent::__construct($timezone); |
|
| 47 | - } |
|
| 38 | + )); |
|
| 39 | + $this->_model_relations = array(); |
|
| 40 | + foreach ($models_this_can_attach_to as $model) { |
|
| 41 | + $this->_model_relations[ $model ] = new EE_Belongs_To_Any_Relation(); |
|
| 42 | + } |
|
| 43 | + foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 44 | + $this->_cap_restriction_generators[ $cap_context ] = new EE_Restriction_Generator_Meta('EXM_key', 'EXM_value'); |
|
| 45 | + } |
|
| 46 | + parent::__construct($timezone); |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -45,10 +45,10 @@ discard block |
||
| 45 | 45 | 'WP_User' => new EE_Belongs_To_Relation(), |
| 46 | 46 | ); |
| 47 | 47 | // this model is generally available for reading |
| 48 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 49 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 50 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 51 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 48 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
| 49 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 50 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 51 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 52 | 52 | parent::__construct($timezone); |
| 53 | 53 | } |
| 54 | 54 | /** |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | public function get_latest_question_group_order() |
| 60 | 60 | { |
| 61 | 61 | $columns_to_select = array( |
| 62 | - 'max_order' => array("MAX(QSG_order)","%d") |
|
| 62 | + 'max_order' => array("MAX(QSG_order)", "%d") |
|
| 63 | 63 | ); |
| 64 | 64 | $max = $this->_get_all_wpdb_results(array(), ARRAY_A, $columns_to_select); |
| 65 | 65 | return $max[0]['max_order']; |
@@ -11,58 +11,58 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class EEM_Question_Group extends EEM_Soft_Delete_Base |
| 13 | 13 | { |
| 14 | - const system_personal = 1; |
|
| 15 | - const system_address = 2; |
|
| 16 | - // private instance of the Attendee object |
|
| 17 | - protected static $_instance = null; |
|
| 14 | + const system_personal = 1; |
|
| 15 | + const system_address = 2; |
|
| 16 | + // private instance of the Attendee object |
|
| 17 | + protected static $_instance = null; |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | - protected function __construct($timezone = null) |
|
| 21 | - { |
|
| 22 | - $this->singular_item = esc_html__('Question Group', 'event_espresso'); |
|
| 23 | - $this->plural_item = esc_html__('Question Groups', 'event_espresso'); |
|
| 20 | + protected function __construct($timezone = null) |
|
| 21 | + { |
|
| 22 | + $this->singular_item = esc_html__('Question Group', 'event_espresso'); |
|
| 23 | + $this->plural_item = esc_html__('Question Groups', 'event_espresso'); |
|
| 24 | 24 | |
| 25 | - $this->_tables = array( |
|
| 26 | - 'Question_Group' => new EE_Primary_Table('esp_question_group', 'QSG_ID') |
|
| 27 | - ); |
|
| 28 | - $this->_fields = array( |
|
| 29 | - 'Question_Group' => array( |
|
| 30 | - 'QSG_ID' => new EE_Primary_Key_Int_Field('QSG_ID', esc_html__('Question Group ID', 'event_espresso')), |
|
| 31 | - 'QSG_name' => new EE_Plain_Text_Field('QSG_name', esc_html__('Question Group Name', 'event_espresso'), false, ''), |
|
| 32 | - 'QSG_identifier' => new EE_Plain_Text_Field('QSG_identifier', esc_html__('Text ID for question Group', 'event_espresso'), false, ''), |
|
| 33 | - 'QSG_desc' => new EE_Post_Content_Field('QSG_desc', esc_html__('Description of Question Group', 'event_espresso'), true, ''), |
|
| 34 | - 'QSG_order' => new EE_Integer_Field('QSG_order', esc_html__('Order in which to show the question group', 'event_espresso'), true, 0), |
|
| 35 | - 'QSG_show_group_name' => new EE_Boolean_Field('QSG_show_group_name', esc_html__('Flag indicating whether to show the group\'s name on the registration page', 'event_espresso'), false, true), |
|
| 36 | - 'QSG_show_group_desc' => new EE_Boolean_Field('QSG_show_group_desc', esc_html__('Flag indicating whether to show the group\s description on the registration page', 'event_espresso'), false, false), |
|
| 37 | - 'QSG_wp_user' => new EE_WP_User_Field('QSG_wp_user', esc_html__('Question Group Creator ID', 'event_espresso'), false), |
|
| 38 | - 'QSG_system' => new EE_Integer_Field('QSG_system', esc_html__('Indicate IF this is a system group and if it is what system group it corresponds to.', 'event_espresso'), false, 0), |
|
| 39 | - 'QSG_deleted' => new EE_Trashed_Flag_Field('QSG_deleted', esc_html__('Flag indicating this question group was deleted', 'event_espresso'), false, false) |
|
| 40 | - ) |
|
| 41 | - ); |
|
| 42 | - $this->_model_relations = array( |
|
| 43 | - 'Question' => new EE_HABTM_Relation('Question_Group_Question'), |
|
| 44 | - 'Event' => new EE_HABTM_Relation('Event_Question_Group'), |
|
| 45 | - 'Event_Question_Group' => new EE_Has_Many_Relation(), |
|
| 46 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
| 47 | - ); |
|
| 48 | - // this model is generally available for reading |
|
| 49 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 50 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 51 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 52 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 53 | - parent::__construct($timezone); |
|
| 54 | - } |
|
| 55 | - /** |
|
| 56 | - * searches the db for the question group with the latest question order and returns that value. |
|
| 57 | - * @access public |
|
| 58 | - * @return int |
|
| 59 | - */ |
|
| 60 | - public function get_latest_question_group_order() |
|
| 61 | - { |
|
| 62 | - $columns_to_select = array( |
|
| 63 | - 'max_order' => array("MAX(QSG_order)","%d") |
|
| 64 | - ); |
|
| 65 | - $max = $this->_get_all_wpdb_results(array(), ARRAY_A, $columns_to_select); |
|
| 66 | - return $max[0]['max_order']; |
|
| 67 | - } |
|
| 25 | + $this->_tables = array( |
|
| 26 | + 'Question_Group' => new EE_Primary_Table('esp_question_group', 'QSG_ID') |
|
| 27 | + ); |
|
| 28 | + $this->_fields = array( |
|
| 29 | + 'Question_Group' => array( |
|
| 30 | + 'QSG_ID' => new EE_Primary_Key_Int_Field('QSG_ID', esc_html__('Question Group ID', 'event_espresso')), |
|
| 31 | + 'QSG_name' => new EE_Plain_Text_Field('QSG_name', esc_html__('Question Group Name', 'event_espresso'), false, ''), |
|
| 32 | + 'QSG_identifier' => new EE_Plain_Text_Field('QSG_identifier', esc_html__('Text ID for question Group', 'event_espresso'), false, ''), |
|
| 33 | + 'QSG_desc' => new EE_Post_Content_Field('QSG_desc', esc_html__('Description of Question Group', 'event_espresso'), true, ''), |
|
| 34 | + 'QSG_order' => new EE_Integer_Field('QSG_order', esc_html__('Order in which to show the question group', 'event_espresso'), true, 0), |
|
| 35 | + 'QSG_show_group_name' => new EE_Boolean_Field('QSG_show_group_name', esc_html__('Flag indicating whether to show the group\'s name on the registration page', 'event_espresso'), false, true), |
|
| 36 | + 'QSG_show_group_desc' => new EE_Boolean_Field('QSG_show_group_desc', esc_html__('Flag indicating whether to show the group\s description on the registration page', 'event_espresso'), false, false), |
|
| 37 | + 'QSG_wp_user' => new EE_WP_User_Field('QSG_wp_user', esc_html__('Question Group Creator ID', 'event_espresso'), false), |
|
| 38 | + 'QSG_system' => new EE_Integer_Field('QSG_system', esc_html__('Indicate IF this is a system group and if it is what system group it corresponds to.', 'event_espresso'), false, 0), |
|
| 39 | + 'QSG_deleted' => new EE_Trashed_Flag_Field('QSG_deleted', esc_html__('Flag indicating this question group was deleted', 'event_espresso'), false, false) |
|
| 40 | + ) |
|
| 41 | + ); |
|
| 42 | + $this->_model_relations = array( |
|
| 43 | + 'Question' => new EE_HABTM_Relation('Question_Group_Question'), |
|
| 44 | + 'Event' => new EE_HABTM_Relation('Event_Question_Group'), |
|
| 45 | + 'Event_Question_Group' => new EE_Has_Many_Relation(), |
|
| 46 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
| 47 | + ); |
|
| 48 | + // this model is generally available for reading |
|
| 49 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 50 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 51 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 52 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('QSG_system'); |
|
| 53 | + parent::__construct($timezone); |
|
| 54 | + } |
|
| 55 | + /** |
|
| 56 | + * searches the db for the question group with the latest question order and returns that value. |
|
| 57 | + * @access public |
|
| 58 | + * @return int |
|
| 59 | + */ |
|
| 60 | + public function get_latest_question_group_order() |
|
| 61 | + { |
|
| 62 | + $columns_to_select = array( |
|
| 63 | + 'max_order' => array("MAX(QSG_order)","%d") |
|
| 64 | + ); |
|
| 65 | + $max = $this->_get_all_wpdb_results(array(), ARRAY_A, $columns_to_select); |
|
| 66 | + return $max[0]['max_order']; |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -284,8 +284,8 @@ discard block |
||
| 284 | 284 | */ |
| 285 | 285 | public function get_attendee_field_for_system_question($system_question_string) |
| 286 | 286 | { |
| 287 | - return isset($this->_system_question_to_attendee_field_name[ $system_question_string ]) |
|
| 288 | - ? $this->_system_question_to_attendee_field_name[ $system_question_string ] |
|
| 287 | + return isset($this->_system_question_to_attendee_field_name[$system_question_string]) |
|
| 288 | + ? $this->_system_question_to_attendee_field_name[$system_question_string] |
|
| 289 | 289 | : null; |
| 290 | 290 | } |
| 291 | 291 | |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | return false; |
| 354 | 354 | } |
| 355 | 355 | $attendee = $this->get_all(array($where_cols_n_values)); |
| 356 | - if (! empty($attendee)) { |
|
| 356 | + if ( ! empty($attendee)) { |
|
| 357 | 357 | return array_shift($attendee); |
| 358 | 358 | } |
| 359 | 359 | return false; |
@@ -12,432 +12,432 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class EEM_Attendee extends EEM_CPT_Base |
| 14 | 14 | { |
| 15 | - // private instance of the Attendee object |
|
| 16 | - protected static $_instance = null; |
|
| 15 | + // private instance of the Attendee object |
|
| 16 | + protected static $_instance = null; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * QST_system for questions are strings not integers now, |
|
| 20 | - * so these constants are deprecated. |
|
| 21 | - * Please instead use the EEM_Attendee::system_question_* constants |
|
| 22 | - * |
|
| 23 | - * @deprecated |
|
| 24 | - */ |
|
| 25 | - const fname_question_id = 1; |
|
| 18 | + /** |
|
| 19 | + * QST_system for questions are strings not integers now, |
|
| 20 | + * so these constants are deprecated. |
|
| 21 | + * Please instead use the EEM_Attendee::system_question_* constants |
|
| 22 | + * |
|
| 23 | + * @deprecated |
|
| 24 | + */ |
|
| 25 | + const fname_question_id = 1; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @deprecated |
|
| 29 | - */ |
|
| 30 | - const lname_question_id = 2; |
|
| 27 | + /** |
|
| 28 | + * @deprecated |
|
| 29 | + */ |
|
| 30 | + const lname_question_id = 2; |
|
| 31 | 31 | |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @deprecated |
|
| 35 | - */ |
|
| 36 | - const email_question_id = 3; |
|
| 33 | + /** |
|
| 34 | + * @deprecated |
|
| 35 | + */ |
|
| 36 | + const email_question_id = 3; |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @deprecated |
|
| 41 | - */ |
|
| 42 | - const address_question_id = 4; |
|
| 39 | + /** |
|
| 40 | + * @deprecated |
|
| 41 | + */ |
|
| 42 | + const address_question_id = 4; |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @deprecated |
|
| 47 | - */ |
|
| 48 | - const address2_question_id = 5; |
|
| 45 | + /** |
|
| 46 | + * @deprecated |
|
| 47 | + */ |
|
| 48 | + const address2_question_id = 5; |
|
| 49 | 49 | |
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @deprecated |
|
| 53 | - */ |
|
| 54 | - const city_question_id = 6; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @deprecated |
|
| 59 | - */ |
|
| 60 | - const state_question_id = 7; |
|
| 61 | - |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @deprecated |
|
| 65 | - */ |
|
| 66 | - const country_question_id = 8; |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @deprecated |
|
| 71 | - */ |
|
| 72 | - const zip_question_id = 9; |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @deprecated |
|
| 77 | - */ |
|
| 78 | - const phone_question_id = 10; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * When looking for questions that correspond to attendee fields, |
|
| 82 | - * look for the question with this QST_system value. |
|
| 83 | - * These replace the old constants like EEM_Attendee::*_question_id |
|
| 84 | - */ |
|
| 85 | - const system_question_fname = 'fname'; |
|
| 86 | - |
|
| 87 | - const system_question_lname = 'lname'; |
|
| 88 | - |
|
| 89 | - const system_question_email = 'email'; |
|
| 90 | - |
|
| 91 | - const system_question_email_confirm = 'email_confirm'; |
|
| 92 | - |
|
| 93 | - const system_question_address = 'address'; |
|
| 94 | - |
|
| 95 | - const system_question_address2 = 'address2'; |
|
| 96 | - |
|
| 97 | - const system_question_city = 'city'; |
|
| 98 | - |
|
| 99 | - const system_question_state = 'state'; |
|
| 100 | - |
|
| 101 | - const system_question_country = 'country'; |
|
| 102 | - |
|
| 103 | - const system_question_zip = 'zip'; |
|
| 104 | - |
|
| 105 | - const system_question_phone = 'phone'; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Keys are all the EEM_Attendee::system_question_* constants, which are |
|
| 109 | - * also all the values of QST_system in the questions table, and values |
|
| 110 | - * are their corresponding Attendee field names |
|
| 111 | - * |
|
| 112 | - * @var array |
|
| 113 | - */ |
|
| 114 | - protected $_system_question_to_attendee_field_name = array( |
|
| 115 | - EEM_Attendee::system_question_fname => 'ATT_fname', |
|
| 116 | - EEM_Attendee::system_question_lname => 'ATT_lname', |
|
| 117 | - EEM_Attendee::system_question_email => 'ATT_email', |
|
| 118 | - EEM_Attendee::system_question_address => 'ATT_address', |
|
| 119 | - EEM_Attendee::system_question_address2 => 'ATT_address2', |
|
| 120 | - EEM_Attendee::system_question_city => 'ATT_city', |
|
| 121 | - EEM_Attendee::system_question_state => 'STA_ID', |
|
| 122 | - EEM_Attendee::system_question_country => 'CNT_ISO', |
|
| 123 | - EEM_Attendee::system_question_zip => 'ATT_zip', |
|
| 124 | - EEM_Attendee::system_question_phone => 'ATT_phone', |
|
| 125 | - ); |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * EEM_Attendee constructor. |
|
| 131 | - * |
|
| 132 | - * @param null $timezone |
|
| 133 | - * @param ModelFieldFactory $model_field_factory |
|
| 134 | - * @throws EE_Error |
|
| 135 | - * @throws InvalidArgumentException |
|
| 136 | - */ |
|
| 137 | - protected function __construct($timezone, ModelFieldFactory $model_field_factory) |
|
| 138 | - { |
|
| 139 | - $this->singular_item = esc_html__('Attendee', 'event_espresso'); |
|
| 140 | - $this->plural_item = esc_html__('Attendees', 'event_espresso'); |
|
| 141 | - $this->_tables = array( |
|
| 142 | - 'Attendee_CPT' => new EE_Primary_Table('posts', 'ID'), |
|
| 143 | - 'Attendee_Meta' => new EE_Secondary_Table( |
|
| 144 | - 'esp_attendee_meta', |
|
| 145 | - 'ATTM_ID', |
|
| 146 | - 'ATT_ID' |
|
| 147 | - ), |
|
| 148 | - ); |
|
| 149 | - $this->_fields = array( |
|
| 150 | - 'Attendee_CPT' => array( |
|
| 151 | - 'ATT_ID' => $model_field_factory->createPrimaryKeyIntField( |
|
| 152 | - 'ID', |
|
| 153 | - esc_html__('Attendee ID', 'event_espresso') |
|
| 154 | - ), |
|
| 155 | - 'ATT_full_name' => $model_field_factory->createPlainTextField( |
|
| 156 | - 'post_title', |
|
| 157 | - esc_html__('Attendee Full Name', 'event_espresso'), |
|
| 158 | - false, |
|
| 159 | - esc_html__('Unknown', 'event_espresso') |
|
| 160 | - ), |
|
| 161 | - 'ATT_bio' => $model_field_factory->createPostContentField( |
|
| 162 | - 'post_content', |
|
| 163 | - esc_html__('Attendee Biography', 'event_espresso'), |
|
| 164 | - false, |
|
| 165 | - esc_html__('No Biography Provided', 'event_espresso') |
|
| 166 | - ), |
|
| 167 | - 'ATT_slug' => $model_field_factory->createSlugField( |
|
| 168 | - 'post_name', |
|
| 169 | - esc_html__('Attendee URL Slug', 'event_espresso') |
|
| 170 | - ), |
|
| 171 | - 'ATT_created' => $model_field_factory->createDatetimeField( |
|
| 172 | - 'post_date', |
|
| 173 | - esc_html__('Time Attendee Created', 'event_espresso') |
|
| 174 | - ), |
|
| 175 | - 'ATT_short_bio' => $model_field_factory->createSimpleHtmlField( |
|
| 176 | - 'post_excerpt', |
|
| 177 | - esc_html__('Attendee Short Biography', 'event_espresso'), |
|
| 178 | - true, |
|
| 179 | - esc_html__('No Biography Provided', 'event_espresso') |
|
| 180 | - ), |
|
| 181 | - 'ATT_modified' => $model_field_factory->createDatetimeField( |
|
| 182 | - 'post_modified', |
|
| 183 | - esc_html__('Time Attendee Last Modified', 'event_espresso') |
|
| 184 | - ), |
|
| 185 | - 'ATT_author' => $model_field_factory->createWpUserField( |
|
| 186 | - 'post_author', |
|
| 187 | - esc_html__('Creator ID of the first Event attended', 'event_espresso'), |
|
| 188 | - false |
|
| 189 | - ), |
|
| 190 | - 'ATT_parent' => $model_field_factory->createDbOnlyIntField( |
|
| 191 | - 'post_parent', |
|
| 192 | - esc_html__('Parent Attendee (unused)', 'event_espresso'), |
|
| 193 | - false, |
|
| 194 | - 0 |
|
| 195 | - ), |
|
| 196 | - 'post_type' => $model_field_factory->createWpPostTypeField('espresso_attendees'), |
|
| 197 | - 'status' => $model_field_factory->createWpPostStatusField( |
|
| 198 | - 'post_status', |
|
| 199 | - esc_html__('Attendee Status', 'event_espresso'), |
|
| 200 | - false, |
|
| 201 | - 'publish' |
|
| 202 | - ), |
|
| 203 | - 'password' => new EE_Password_Field( |
|
| 204 | - 'post_password', |
|
| 205 | - esc_html__('Password', 'event_espresso'), |
|
| 206 | - false, |
|
| 207 | - '', |
|
| 208 | - array( |
|
| 209 | - 'ATT_bio', |
|
| 210 | - 'ATT_short_bio', |
|
| 211 | - 'ATT_address', |
|
| 212 | - 'ATT_address2', |
|
| 213 | - 'ATT_city', |
|
| 214 | - 'STA_ID', |
|
| 215 | - 'CNT_ISO', |
|
| 216 | - 'ATT_zip', |
|
| 217 | - 'ATT_email', |
|
| 218 | - 'ATT_phone' |
|
| 219 | - ) |
|
| 220 | - ) |
|
| 221 | - ), |
|
| 222 | - 'Attendee_Meta' => array( |
|
| 223 | - 'ATTM_ID' => $model_field_factory->createDbOnlyIntField( |
|
| 224 | - 'ATTM_ID', |
|
| 225 | - esc_html__('Attendee Meta Row ID', 'event_espresso'), |
|
| 226 | - false |
|
| 227 | - ), |
|
| 228 | - 'ATT_ID_fk' => $model_field_factory->createDbOnlyIntField( |
|
| 229 | - 'ATT_ID', |
|
| 230 | - esc_html__('Foreign Key to Attendee in Post Table', 'event_espresso'), |
|
| 231 | - false |
|
| 232 | - ), |
|
| 233 | - 'ATT_fname' => $model_field_factory->createPlainTextField( |
|
| 234 | - 'ATT_fname', |
|
| 235 | - esc_html__('First Name', 'event_espresso') |
|
| 236 | - ), |
|
| 237 | - 'ATT_lname' => $model_field_factory->createPlainTextField( |
|
| 238 | - 'ATT_lname', |
|
| 239 | - esc_html__('Last Name', 'event_espresso') |
|
| 240 | - ), |
|
| 241 | - 'ATT_address' => $model_field_factory->createPlainTextField( |
|
| 242 | - 'ATT_address', |
|
| 243 | - esc_html__('Address Part 1', 'event_espresso') |
|
| 244 | - ), |
|
| 245 | - 'ATT_address2' => $model_field_factory->createPlainTextField( |
|
| 246 | - 'ATT_address2', |
|
| 247 | - esc_html__('Address Part 2', 'event_espresso') |
|
| 248 | - ), |
|
| 249 | - 'ATT_city' => $model_field_factory->createPlainTextField( |
|
| 250 | - 'ATT_city', |
|
| 251 | - esc_html__('City', 'event_espresso') |
|
| 252 | - ), |
|
| 253 | - 'STA_ID' => $model_field_factory->createForeignKeyIntField( |
|
| 254 | - 'STA_ID', |
|
| 255 | - esc_html__('State', 'event_espresso'), |
|
| 256 | - true, |
|
| 257 | - 0, |
|
| 258 | - 'State' |
|
| 259 | - ), |
|
| 260 | - 'CNT_ISO' => $model_field_factory->createForeignKeyStringField( |
|
| 261 | - 'CNT_ISO', |
|
| 262 | - esc_html__('Country', 'event_espresso'), |
|
| 263 | - true, |
|
| 264 | - '', |
|
| 265 | - 'Country' |
|
| 266 | - ), |
|
| 267 | - 'ATT_zip' => $model_field_factory->createPlainTextField( |
|
| 268 | - 'ATT_zip', |
|
| 269 | - esc_html__('ZIP/Postal Code', 'event_espresso') |
|
| 270 | - ), |
|
| 271 | - 'ATT_email' => $model_field_factory->createEmailField( |
|
| 272 | - 'ATT_email', |
|
| 273 | - esc_html__('Email Address', 'event_espresso') |
|
| 274 | - ), |
|
| 275 | - 'ATT_phone' => $model_field_factory->createPlainTextField( |
|
| 276 | - 'ATT_phone', |
|
| 277 | - esc_html__('Phone', 'event_espresso') |
|
| 278 | - ), |
|
| 279 | - ), |
|
| 280 | - ); |
|
| 281 | - $this->_model_relations = array( |
|
| 282 | - 'Registration' => new EE_Has_Many_Relation(), |
|
| 283 | - 'State' => new EE_Belongs_To_Relation(), |
|
| 284 | - 'Country' => new EE_Belongs_To_Relation(), |
|
| 285 | - 'Event' => new EE_HABTM_Relation('Registration', false), |
|
| 286 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
| 287 | - 'Message' => new EE_Has_Many_Any_Relation(false), |
|
| 288 | - // allow deletion of attendees even if they have messages in the queue for them. |
|
| 289 | - 'Term_Relationship' => new EE_Has_Many_Relation(), |
|
| 290 | - 'Term_Taxonomy' => new EE_HABTM_Relation('Term_Relationship'), |
|
| 291 | - ); |
|
| 292 | - $this->_caps_slug = 'contacts'; |
|
| 293 | - $this->model_chain_to_password = ''; |
|
| 294 | - parent::__construct($timezone); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Gets the name of the field on the attendee model corresponding to the system question string |
|
| 301 | - * which should be one of the keys from EEM_Attendee::_system_question_to_attendee_field_name |
|
| 302 | - * |
|
| 303 | - * @param string $system_question_string |
|
| 304 | - * @return string|null if not found |
|
| 305 | - */ |
|
| 306 | - public function get_attendee_field_for_system_question($system_question_string) |
|
| 307 | - { |
|
| 308 | - return isset($this->_system_question_to_attendee_field_name[ $system_question_string ]) |
|
| 309 | - ? $this->_system_question_to_attendee_field_name[ $system_question_string ] |
|
| 310 | - : null; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Gets mapping from esp_question.QST_system values to their corresponding attendee field names |
|
| 317 | - * |
|
| 318 | - * @return array |
|
| 319 | - */ |
|
| 320 | - public function system_question_to_attendee_field_mapping() |
|
| 321 | - { |
|
| 322 | - return $this->_system_question_to_attendee_field_name; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Gets all the attendees for a transaction (by using the esp_registration as a join table) |
|
| 329 | - * |
|
| 330 | - * @param EE_Transaction /int $transaction_id_or_obj EE_Transaction or its ID |
|
| 331 | - * @return EE_Attendee[]|EE_Base_Class[] |
|
| 332 | - * @throws EE_Error |
|
| 333 | - */ |
|
| 334 | - public function get_attendees_for_transaction($transaction_id_or_obj) |
|
| 335 | - { |
|
| 336 | - return $this->get_all( |
|
| 337 | - array( |
|
| 338 | - array( |
|
| 339 | - 'Registration.Transaction.TXN_ID' => $transaction_id_or_obj instanceof EE_Transaction |
|
| 340 | - ? $transaction_id_or_obj->ID() |
|
| 341 | - : $transaction_id_or_obj, |
|
| 342 | - ), |
|
| 343 | - ) |
|
| 344 | - ); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * retrieve a single attendee from db via their ID |
|
| 351 | - * |
|
| 352 | - * @param $ATT_ID |
|
| 353 | - * @return mixed array on success, FALSE on fail |
|
| 354 | - * @deprecated |
|
| 355 | - */ |
|
| 356 | - public function get_attendee_by_ID($ATT_ID = false) |
|
| 357 | - { |
|
| 358 | - // retrieve a particular EE_Attendee |
|
| 359 | - return $this->get_one_by_ID($ATT_ID); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * retrieve a single attendee from db via their ID |
|
| 366 | - * |
|
| 367 | - * @param array $where_cols_n_values |
|
| 368 | - * @return mixed array on success, FALSE on fail |
|
| 369 | - * @throws EE_Error |
|
| 370 | - */ |
|
| 371 | - public function get_attendee($where_cols_n_values = array()) |
|
| 372 | - { |
|
| 373 | - if (empty($where_cols_n_values)) { |
|
| 374 | - return false; |
|
| 375 | - } |
|
| 376 | - $attendee = $this->get_all(array($where_cols_n_values)); |
|
| 377 | - if (! empty($attendee)) { |
|
| 378 | - return array_shift($attendee); |
|
| 379 | - } |
|
| 380 | - return false; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Search for an existing Attendee record in the DB |
|
| 387 | - * |
|
| 388 | - * @param array $where_cols_n_values |
|
| 389 | - * @return bool|mixed |
|
| 390 | - * @throws EE_Error |
|
| 391 | - */ |
|
| 392 | - public function find_existing_attendee($where_cols_n_values = null) |
|
| 393 | - { |
|
| 394 | - // search by combo of first and last names plus the email address |
|
| 395 | - $attendee_data_keys = array( |
|
| 396 | - 'ATT_fname' => $this->_ATT_fname, |
|
| 397 | - 'ATT_lname' => $this->_ATT_lname, |
|
| 398 | - 'ATT_email' => $this->_ATT_email, |
|
| 399 | - ); |
|
| 400 | - // no search params means attendee object already exists. |
|
| 401 | - $where_cols_n_values = is_array($where_cols_n_values) && ! empty($where_cols_n_values) |
|
| 402 | - ? $where_cols_n_values |
|
| 403 | - : $attendee_data_keys; |
|
| 404 | - $valid_data = true; |
|
| 405 | - // check for required values |
|
| 406 | - $valid_data = isset($where_cols_n_values['ATT_fname']) && ! empty($where_cols_n_values['ATT_fname']) |
|
| 407 | - ? $valid_data |
|
| 408 | - : false; |
|
| 409 | - $valid_data = isset($where_cols_n_values['ATT_lname']) && ! empty($where_cols_n_values['ATT_lname']) |
|
| 410 | - ? $valid_data |
|
| 411 | - : false; |
|
| 412 | - $valid_data = isset($where_cols_n_values['ATT_email']) && ! empty($where_cols_n_values['ATT_email']) |
|
| 413 | - ? $valid_data |
|
| 414 | - : false; |
|
| 415 | - if ($valid_data) { |
|
| 416 | - $attendee = $this->get_attendee($where_cols_n_values); |
|
| 417 | - if ($attendee instanceof EE_Attendee) { |
|
| 418 | - return $attendee; |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - return false; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Takes an incoming array of EE_Registration ids |
|
| 428 | - * and sends back a list of corresponding non duplicate EE_Attendee objects. |
|
| 429 | - * |
|
| 430 | - * @since 4.3.0 |
|
| 431 | - * @param array $ids array of EE_Registration ids |
|
| 432 | - * @return EE_Attendee[]|EE_Base_Class[] |
|
| 433 | - * @throws EE_Error |
|
| 434 | - */ |
|
| 435 | - public function get_array_of_contacts_from_reg_ids($ids) |
|
| 436 | - { |
|
| 437 | - $ids = (array) $ids; |
|
| 438 | - $_where = array( |
|
| 439 | - 'Registration.REG_ID' => array('in', $ids), |
|
| 440 | - ); |
|
| 441 | - return $this->get_all(array($_where)); |
|
| 442 | - } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @deprecated |
|
| 53 | + */ |
|
| 54 | + const city_question_id = 6; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @deprecated |
|
| 59 | + */ |
|
| 60 | + const state_question_id = 7; |
|
| 61 | + |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @deprecated |
|
| 65 | + */ |
|
| 66 | + const country_question_id = 8; |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @deprecated |
|
| 71 | + */ |
|
| 72 | + const zip_question_id = 9; |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @deprecated |
|
| 77 | + */ |
|
| 78 | + const phone_question_id = 10; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * When looking for questions that correspond to attendee fields, |
|
| 82 | + * look for the question with this QST_system value. |
|
| 83 | + * These replace the old constants like EEM_Attendee::*_question_id |
|
| 84 | + */ |
|
| 85 | + const system_question_fname = 'fname'; |
|
| 86 | + |
|
| 87 | + const system_question_lname = 'lname'; |
|
| 88 | + |
|
| 89 | + const system_question_email = 'email'; |
|
| 90 | + |
|
| 91 | + const system_question_email_confirm = 'email_confirm'; |
|
| 92 | + |
|
| 93 | + const system_question_address = 'address'; |
|
| 94 | + |
|
| 95 | + const system_question_address2 = 'address2'; |
|
| 96 | + |
|
| 97 | + const system_question_city = 'city'; |
|
| 98 | + |
|
| 99 | + const system_question_state = 'state'; |
|
| 100 | + |
|
| 101 | + const system_question_country = 'country'; |
|
| 102 | + |
|
| 103 | + const system_question_zip = 'zip'; |
|
| 104 | + |
|
| 105 | + const system_question_phone = 'phone'; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Keys are all the EEM_Attendee::system_question_* constants, which are |
|
| 109 | + * also all the values of QST_system in the questions table, and values |
|
| 110 | + * are their corresponding Attendee field names |
|
| 111 | + * |
|
| 112 | + * @var array |
|
| 113 | + */ |
|
| 114 | + protected $_system_question_to_attendee_field_name = array( |
|
| 115 | + EEM_Attendee::system_question_fname => 'ATT_fname', |
|
| 116 | + EEM_Attendee::system_question_lname => 'ATT_lname', |
|
| 117 | + EEM_Attendee::system_question_email => 'ATT_email', |
|
| 118 | + EEM_Attendee::system_question_address => 'ATT_address', |
|
| 119 | + EEM_Attendee::system_question_address2 => 'ATT_address2', |
|
| 120 | + EEM_Attendee::system_question_city => 'ATT_city', |
|
| 121 | + EEM_Attendee::system_question_state => 'STA_ID', |
|
| 122 | + EEM_Attendee::system_question_country => 'CNT_ISO', |
|
| 123 | + EEM_Attendee::system_question_zip => 'ATT_zip', |
|
| 124 | + EEM_Attendee::system_question_phone => 'ATT_phone', |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * EEM_Attendee constructor. |
|
| 131 | + * |
|
| 132 | + * @param null $timezone |
|
| 133 | + * @param ModelFieldFactory $model_field_factory |
|
| 134 | + * @throws EE_Error |
|
| 135 | + * @throws InvalidArgumentException |
|
| 136 | + */ |
|
| 137 | + protected function __construct($timezone, ModelFieldFactory $model_field_factory) |
|
| 138 | + { |
|
| 139 | + $this->singular_item = esc_html__('Attendee', 'event_espresso'); |
|
| 140 | + $this->plural_item = esc_html__('Attendees', 'event_espresso'); |
|
| 141 | + $this->_tables = array( |
|
| 142 | + 'Attendee_CPT' => new EE_Primary_Table('posts', 'ID'), |
|
| 143 | + 'Attendee_Meta' => new EE_Secondary_Table( |
|
| 144 | + 'esp_attendee_meta', |
|
| 145 | + 'ATTM_ID', |
|
| 146 | + 'ATT_ID' |
|
| 147 | + ), |
|
| 148 | + ); |
|
| 149 | + $this->_fields = array( |
|
| 150 | + 'Attendee_CPT' => array( |
|
| 151 | + 'ATT_ID' => $model_field_factory->createPrimaryKeyIntField( |
|
| 152 | + 'ID', |
|
| 153 | + esc_html__('Attendee ID', 'event_espresso') |
|
| 154 | + ), |
|
| 155 | + 'ATT_full_name' => $model_field_factory->createPlainTextField( |
|
| 156 | + 'post_title', |
|
| 157 | + esc_html__('Attendee Full Name', 'event_espresso'), |
|
| 158 | + false, |
|
| 159 | + esc_html__('Unknown', 'event_espresso') |
|
| 160 | + ), |
|
| 161 | + 'ATT_bio' => $model_field_factory->createPostContentField( |
|
| 162 | + 'post_content', |
|
| 163 | + esc_html__('Attendee Biography', 'event_espresso'), |
|
| 164 | + false, |
|
| 165 | + esc_html__('No Biography Provided', 'event_espresso') |
|
| 166 | + ), |
|
| 167 | + 'ATT_slug' => $model_field_factory->createSlugField( |
|
| 168 | + 'post_name', |
|
| 169 | + esc_html__('Attendee URL Slug', 'event_espresso') |
|
| 170 | + ), |
|
| 171 | + 'ATT_created' => $model_field_factory->createDatetimeField( |
|
| 172 | + 'post_date', |
|
| 173 | + esc_html__('Time Attendee Created', 'event_espresso') |
|
| 174 | + ), |
|
| 175 | + 'ATT_short_bio' => $model_field_factory->createSimpleHtmlField( |
|
| 176 | + 'post_excerpt', |
|
| 177 | + esc_html__('Attendee Short Biography', 'event_espresso'), |
|
| 178 | + true, |
|
| 179 | + esc_html__('No Biography Provided', 'event_espresso') |
|
| 180 | + ), |
|
| 181 | + 'ATT_modified' => $model_field_factory->createDatetimeField( |
|
| 182 | + 'post_modified', |
|
| 183 | + esc_html__('Time Attendee Last Modified', 'event_espresso') |
|
| 184 | + ), |
|
| 185 | + 'ATT_author' => $model_field_factory->createWpUserField( |
|
| 186 | + 'post_author', |
|
| 187 | + esc_html__('Creator ID of the first Event attended', 'event_espresso'), |
|
| 188 | + false |
|
| 189 | + ), |
|
| 190 | + 'ATT_parent' => $model_field_factory->createDbOnlyIntField( |
|
| 191 | + 'post_parent', |
|
| 192 | + esc_html__('Parent Attendee (unused)', 'event_espresso'), |
|
| 193 | + false, |
|
| 194 | + 0 |
|
| 195 | + ), |
|
| 196 | + 'post_type' => $model_field_factory->createWpPostTypeField('espresso_attendees'), |
|
| 197 | + 'status' => $model_field_factory->createWpPostStatusField( |
|
| 198 | + 'post_status', |
|
| 199 | + esc_html__('Attendee Status', 'event_espresso'), |
|
| 200 | + false, |
|
| 201 | + 'publish' |
|
| 202 | + ), |
|
| 203 | + 'password' => new EE_Password_Field( |
|
| 204 | + 'post_password', |
|
| 205 | + esc_html__('Password', 'event_espresso'), |
|
| 206 | + false, |
|
| 207 | + '', |
|
| 208 | + array( |
|
| 209 | + 'ATT_bio', |
|
| 210 | + 'ATT_short_bio', |
|
| 211 | + 'ATT_address', |
|
| 212 | + 'ATT_address2', |
|
| 213 | + 'ATT_city', |
|
| 214 | + 'STA_ID', |
|
| 215 | + 'CNT_ISO', |
|
| 216 | + 'ATT_zip', |
|
| 217 | + 'ATT_email', |
|
| 218 | + 'ATT_phone' |
|
| 219 | + ) |
|
| 220 | + ) |
|
| 221 | + ), |
|
| 222 | + 'Attendee_Meta' => array( |
|
| 223 | + 'ATTM_ID' => $model_field_factory->createDbOnlyIntField( |
|
| 224 | + 'ATTM_ID', |
|
| 225 | + esc_html__('Attendee Meta Row ID', 'event_espresso'), |
|
| 226 | + false |
|
| 227 | + ), |
|
| 228 | + 'ATT_ID_fk' => $model_field_factory->createDbOnlyIntField( |
|
| 229 | + 'ATT_ID', |
|
| 230 | + esc_html__('Foreign Key to Attendee in Post Table', 'event_espresso'), |
|
| 231 | + false |
|
| 232 | + ), |
|
| 233 | + 'ATT_fname' => $model_field_factory->createPlainTextField( |
|
| 234 | + 'ATT_fname', |
|
| 235 | + esc_html__('First Name', 'event_espresso') |
|
| 236 | + ), |
|
| 237 | + 'ATT_lname' => $model_field_factory->createPlainTextField( |
|
| 238 | + 'ATT_lname', |
|
| 239 | + esc_html__('Last Name', 'event_espresso') |
|
| 240 | + ), |
|
| 241 | + 'ATT_address' => $model_field_factory->createPlainTextField( |
|
| 242 | + 'ATT_address', |
|
| 243 | + esc_html__('Address Part 1', 'event_espresso') |
|
| 244 | + ), |
|
| 245 | + 'ATT_address2' => $model_field_factory->createPlainTextField( |
|
| 246 | + 'ATT_address2', |
|
| 247 | + esc_html__('Address Part 2', 'event_espresso') |
|
| 248 | + ), |
|
| 249 | + 'ATT_city' => $model_field_factory->createPlainTextField( |
|
| 250 | + 'ATT_city', |
|
| 251 | + esc_html__('City', 'event_espresso') |
|
| 252 | + ), |
|
| 253 | + 'STA_ID' => $model_field_factory->createForeignKeyIntField( |
|
| 254 | + 'STA_ID', |
|
| 255 | + esc_html__('State', 'event_espresso'), |
|
| 256 | + true, |
|
| 257 | + 0, |
|
| 258 | + 'State' |
|
| 259 | + ), |
|
| 260 | + 'CNT_ISO' => $model_field_factory->createForeignKeyStringField( |
|
| 261 | + 'CNT_ISO', |
|
| 262 | + esc_html__('Country', 'event_espresso'), |
|
| 263 | + true, |
|
| 264 | + '', |
|
| 265 | + 'Country' |
|
| 266 | + ), |
|
| 267 | + 'ATT_zip' => $model_field_factory->createPlainTextField( |
|
| 268 | + 'ATT_zip', |
|
| 269 | + esc_html__('ZIP/Postal Code', 'event_espresso') |
|
| 270 | + ), |
|
| 271 | + 'ATT_email' => $model_field_factory->createEmailField( |
|
| 272 | + 'ATT_email', |
|
| 273 | + esc_html__('Email Address', 'event_espresso') |
|
| 274 | + ), |
|
| 275 | + 'ATT_phone' => $model_field_factory->createPlainTextField( |
|
| 276 | + 'ATT_phone', |
|
| 277 | + esc_html__('Phone', 'event_espresso') |
|
| 278 | + ), |
|
| 279 | + ), |
|
| 280 | + ); |
|
| 281 | + $this->_model_relations = array( |
|
| 282 | + 'Registration' => new EE_Has_Many_Relation(), |
|
| 283 | + 'State' => new EE_Belongs_To_Relation(), |
|
| 284 | + 'Country' => new EE_Belongs_To_Relation(), |
|
| 285 | + 'Event' => new EE_HABTM_Relation('Registration', false), |
|
| 286 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
| 287 | + 'Message' => new EE_Has_Many_Any_Relation(false), |
|
| 288 | + // allow deletion of attendees even if they have messages in the queue for them. |
|
| 289 | + 'Term_Relationship' => new EE_Has_Many_Relation(), |
|
| 290 | + 'Term_Taxonomy' => new EE_HABTM_Relation('Term_Relationship'), |
|
| 291 | + ); |
|
| 292 | + $this->_caps_slug = 'contacts'; |
|
| 293 | + $this->model_chain_to_password = ''; |
|
| 294 | + parent::__construct($timezone); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Gets the name of the field on the attendee model corresponding to the system question string |
|
| 301 | + * which should be one of the keys from EEM_Attendee::_system_question_to_attendee_field_name |
|
| 302 | + * |
|
| 303 | + * @param string $system_question_string |
|
| 304 | + * @return string|null if not found |
|
| 305 | + */ |
|
| 306 | + public function get_attendee_field_for_system_question($system_question_string) |
|
| 307 | + { |
|
| 308 | + return isset($this->_system_question_to_attendee_field_name[ $system_question_string ]) |
|
| 309 | + ? $this->_system_question_to_attendee_field_name[ $system_question_string ] |
|
| 310 | + : null; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Gets mapping from esp_question.QST_system values to their corresponding attendee field names |
|
| 317 | + * |
|
| 318 | + * @return array |
|
| 319 | + */ |
|
| 320 | + public function system_question_to_attendee_field_mapping() |
|
| 321 | + { |
|
| 322 | + return $this->_system_question_to_attendee_field_name; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Gets all the attendees for a transaction (by using the esp_registration as a join table) |
|
| 329 | + * |
|
| 330 | + * @param EE_Transaction /int $transaction_id_or_obj EE_Transaction or its ID |
|
| 331 | + * @return EE_Attendee[]|EE_Base_Class[] |
|
| 332 | + * @throws EE_Error |
|
| 333 | + */ |
|
| 334 | + public function get_attendees_for_transaction($transaction_id_or_obj) |
|
| 335 | + { |
|
| 336 | + return $this->get_all( |
|
| 337 | + array( |
|
| 338 | + array( |
|
| 339 | + 'Registration.Transaction.TXN_ID' => $transaction_id_or_obj instanceof EE_Transaction |
|
| 340 | + ? $transaction_id_or_obj->ID() |
|
| 341 | + : $transaction_id_or_obj, |
|
| 342 | + ), |
|
| 343 | + ) |
|
| 344 | + ); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * retrieve a single attendee from db via their ID |
|
| 351 | + * |
|
| 352 | + * @param $ATT_ID |
|
| 353 | + * @return mixed array on success, FALSE on fail |
|
| 354 | + * @deprecated |
|
| 355 | + */ |
|
| 356 | + public function get_attendee_by_ID($ATT_ID = false) |
|
| 357 | + { |
|
| 358 | + // retrieve a particular EE_Attendee |
|
| 359 | + return $this->get_one_by_ID($ATT_ID); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * retrieve a single attendee from db via their ID |
|
| 366 | + * |
|
| 367 | + * @param array $where_cols_n_values |
|
| 368 | + * @return mixed array on success, FALSE on fail |
|
| 369 | + * @throws EE_Error |
|
| 370 | + */ |
|
| 371 | + public function get_attendee($where_cols_n_values = array()) |
|
| 372 | + { |
|
| 373 | + if (empty($where_cols_n_values)) { |
|
| 374 | + return false; |
|
| 375 | + } |
|
| 376 | + $attendee = $this->get_all(array($where_cols_n_values)); |
|
| 377 | + if (! empty($attendee)) { |
|
| 378 | + return array_shift($attendee); |
|
| 379 | + } |
|
| 380 | + return false; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Search for an existing Attendee record in the DB |
|
| 387 | + * |
|
| 388 | + * @param array $where_cols_n_values |
|
| 389 | + * @return bool|mixed |
|
| 390 | + * @throws EE_Error |
|
| 391 | + */ |
|
| 392 | + public function find_existing_attendee($where_cols_n_values = null) |
|
| 393 | + { |
|
| 394 | + // search by combo of first and last names plus the email address |
|
| 395 | + $attendee_data_keys = array( |
|
| 396 | + 'ATT_fname' => $this->_ATT_fname, |
|
| 397 | + 'ATT_lname' => $this->_ATT_lname, |
|
| 398 | + 'ATT_email' => $this->_ATT_email, |
|
| 399 | + ); |
|
| 400 | + // no search params means attendee object already exists. |
|
| 401 | + $where_cols_n_values = is_array($where_cols_n_values) && ! empty($where_cols_n_values) |
|
| 402 | + ? $where_cols_n_values |
|
| 403 | + : $attendee_data_keys; |
|
| 404 | + $valid_data = true; |
|
| 405 | + // check for required values |
|
| 406 | + $valid_data = isset($where_cols_n_values['ATT_fname']) && ! empty($where_cols_n_values['ATT_fname']) |
|
| 407 | + ? $valid_data |
|
| 408 | + : false; |
|
| 409 | + $valid_data = isset($where_cols_n_values['ATT_lname']) && ! empty($where_cols_n_values['ATT_lname']) |
|
| 410 | + ? $valid_data |
|
| 411 | + : false; |
|
| 412 | + $valid_data = isset($where_cols_n_values['ATT_email']) && ! empty($where_cols_n_values['ATT_email']) |
|
| 413 | + ? $valid_data |
|
| 414 | + : false; |
|
| 415 | + if ($valid_data) { |
|
| 416 | + $attendee = $this->get_attendee($where_cols_n_values); |
|
| 417 | + if ($attendee instanceof EE_Attendee) { |
|
| 418 | + return $attendee; |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + return false; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Takes an incoming array of EE_Registration ids |
|
| 428 | + * and sends back a list of corresponding non duplicate EE_Attendee objects. |
|
| 429 | + * |
|
| 430 | + * @since 4.3.0 |
|
| 431 | + * @param array $ids array of EE_Registration ids |
|
| 432 | + * @return EE_Attendee[]|EE_Base_Class[] |
|
| 433 | + * @throws EE_Error |
|
| 434 | + */ |
|
| 435 | + public function get_array_of_contacts_from_reg_ids($ids) |
|
| 436 | + { |
|
| 437 | + $ids = (array) $ids; |
|
| 438 | + $_where = array( |
|
| 439 | + 'Registration.REG_ID' => array('in', $ids), |
|
| 440 | + ); |
|
| 441 | + return $this->get_all(array($_where)); |
|
| 442 | + } |
|
| 443 | 443 | } |
@@ -59,11 +59,11 @@ |
||
| 59 | 59 | ); |
| 60 | 60 | $this->_model_relations = array(); |
| 61 | 61 | foreach ($models_this_can_attach_to as $model) { |
| 62 | - $this->_model_relations[ $model ] = new EE_Belongs_To_Relation(); |
|
| 62 | + $this->_model_relations[$model] = new EE_Belongs_To_Relation(); |
|
| 63 | 63 | } |
| 64 | 64 | $this->_wp_core_model = true; |
| 65 | 65 | foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
| 66 | - $this->_cap_restriction_generators[ $cap_context ] = new EE_Restriction_Generator_Meta( |
|
| 66 | + $this->_cap_restriction_generators[$cap_context] = new EE_Restriction_Generator_Meta( |
|
| 67 | 67 | 'meta_key', |
| 68 | 68 | 'meta_value' |
| 69 | 69 | ); |
@@ -18,56 +18,56 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class EEM_Post_Meta extends EEM_Base |
| 20 | 20 | { |
| 21 | - // private instance of the EE_Post_Meta object |
|
| 22 | - protected static $_instance = null; |
|
| 21 | + // private instance of the EE_Post_Meta object |
|
| 22 | + protected static $_instance = null; |
|
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | |
| 26 | - protected function __construct($timezone = null) |
|
| 27 | - { |
|
| 28 | - $this->singular_item = esc_html__('Post Meta', 'event_espresso'); |
|
| 29 | - $this->plural_item = esc_html__('Post Metas', 'event_espresso'); |
|
| 30 | - $this->_tables = array( |
|
| 31 | - 'Post_Meta' => new EE_Primary_Table('postmeta', 'meta_id'), |
|
| 32 | - ); |
|
| 33 | - $models_this_can_attach_to = array_keys(EE_Registry::instance()->cpt_models()); |
|
| 34 | - $this->_fields = array( |
|
| 35 | - 'Post_Meta' => array( |
|
| 36 | - 'meta_id' => new EE_Primary_Key_Int_Field( |
|
| 37 | - 'meta_id', |
|
| 38 | - esc_html__("Meta ID", "event_espresso") |
|
| 39 | - ), |
|
| 40 | - 'post_id' => new EE_Foreign_Key_Int_Field( |
|
| 41 | - 'post_id', |
|
| 42 | - esc_html__("Primary Key of Post", "event_espresso"), |
|
| 43 | - false, |
|
| 44 | - 0, |
|
| 45 | - $models_this_can_attach_to |
|
| 46 | - ), |
|
| 47 | - 'meta_key' => new EE_Plain_Text_Field( |
|
| 48 | - 'meta_key', |
|
| 49 | - esc_html__("Meta Key", "event_espresso"), |
|
| 50 | - false, |
|
| 51 | - '' |
|
| 52 | - ), |
|
| 53 | - 'meta_value' => new EE_Maybe_Serialized_Text_Field( |
|
| 54 | - 'meta_value', |
|
| 55 | - esc_html__("Meta Value", "event_espresso"), |
|
| 56 | - true |
|
| 57 | - ), |
|
| 58 | - ), |
|
| 59 | - ); |
|
| 60 | - $this->_model_relations = array(); |
|
| 61 | - foreach ($models_this_can_attach_to as $model) { |
|
| 62 | - $this->_model_relations[ $model ] = new EE_Belongs_To_Relation(); |
|
| 63 | - } |
|
| 64 | - $this->_wp_core_model = true; |
|
| 65 | - foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 66 | - $this->_cap_restriction_generators[ $cap_context ] = new EE_Restriction_Generator_Meta( |
|
| 67 | - 'meta_key', |
|
| 68 | - 'meta_value' |
|
| 69 | - ); |
|
| 70 | - } |
|
| 71 | - parent::__construct($timezone); |
|
| 72 | - } |
|
| 26 | + protected function __construct($timezone = null) |
|
| 27 | + { |
|
| 28 | + $this->singular_item = esc_html__('Post Meta', 'event_espresso'); |
|
| 29 | + $this->plural_item = esc_html__('Post Metas', 'event_espresso'); |
|
| 30 | + $this->_tables = array( |
|
| 31 | + 'Post_Meta' => new EE_Primary_Table('postmeta', 'meta_id'), |
|
| 32 | + ); |
|
| 33 | + $models_this_can_attach_to = array_keys(EE_Registry::instance()->cpt_models()); |
|
| 34 | + $this->_fields = array( |
|
| 35 | + 'Post_Meta' => array( |
|
| 36 | + 'meta_id' => new EE_Primary_Key_Int_Field( |
|
| 37 | + 'meta_id', |
|
| 38 | + esc_html__("Meta ID", "event_espresso") |
|
| 39 | + ), |
|
| 40 | + 'post_id' => new EE_Foreign_Key_Int_Field( |
|
| 41 | + 'post_id', |
|
| 42 | + esc_html__("Primary Key of Post", "event_espresso"), |
|
| 43 | + false, |
|
| 44 | + 0, |
|
| 45 | + $models_this_can_attach_to |
|
| 46 | + ), |
|
| 47 | + 'meta_key' => new EE_Plain_Text_Field( |
|
| 48 | + 'meta_key', |
|
| 49 | + esc_html__("Meta Key", "event_espresso"), |
|
| 50 | + false, |
|
| 51 | + '' |
|
| 52 | + ), |
|
| 53 | + 'meta_value' => new EE_Maybe_Serialized_Text_Field( |
|
| 54 | + 'meta_value', |
|
| 55 | + esc_html__("Meta Value", "event_espresso"), |
|
| 56 | + true |
|
| 57 | + ), |
|
| 58 | + ), |
|
| 59 | + ); |
|
| 60 | + $this->_model_relations = array(); |
|
| 61 | + foreach ($models_this_can_attach_to as $model) { |
|
| 62 | + $this->_model_relations[ $model ] = new EE_Belongs_To_Relation(); |
|
| 63 | + } |
|
| 64 | + $this->_wp_core_model = true; |
|
| 65 | + foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 66 | + $this->_cap_restriction_generators[ $cap_context ] = new EE_Restriction_Generator_Meta( |
|
| 67 | + 'meta_key', |
|
| 68 | + 'meta_value' |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | + parent::__construct($timezone); |
|
| 72 | + } |
|
| 73 | 73 | } |
@@ -6,14 +6,14 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class EE_All_Caps_Text_Field extends EE_Text_Field_Base |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * makes it all upper case, and key-like |
|
| 11 | - * |
|
| 12 | - * @param string $value_inputted_for_field_on_model_object |
|
| 13 | - * @return string |
|
| 14 | - */ |
|
| 15 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 16 | - { |
|
| 17 | - return strtoupper(sanitize_key($value_inputted_for_field_on_model_object)); |
|
| 18 | - } |
|
| 9 | + /** |
|
| 10 | + * makes it all upper case, and key-like |
|
| 11 | + * |
|
| 12 | + * @param string $value_inputted_for_field_on_model_object |
|
| 13 | + * @return string |
|
| 14 | + */ |
|
| 15 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 16 | + { |
|
| 17 | + return strtoupper(sanitize_key($value_inputted_for_field_on_model_object)); |
|
| 18 | + } |
|
| 19 | 19 | } |
@@ -2,15 +2,15 @@ |
||
| 2 | 2 | |
| 3 | 3 | class EE_Slug_Field extends EE_Text_Field_Base |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * ensures string is usable in URLs |
|
| 7 | - * |
|
| 8 | - * @param string $value_inputted_for_field_on_model_object |
|
| 9 | - * @return string |
|
| 10 | - */ |
|
| 11 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 12 | - { |
|
| 13 | - // reminder: function prepares for use in URLs, not making human-readable. |
|
| 14 | - return sanitize_title($value_inputted_for_field_on_model_object); |
|
| 15 | - } |
|
| 5 | + /** |
|
| 6 | + * ensures string is usable in URLs |
|
| 7 | + * |
|
| 8 | + * @param string $value_inputted_for_field_on_model_object |
|
| 9 | + * @return string |
|
| 10 | + */ |
|
| 11 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 12 | + { |
|
| 13 | + // reminder: function prepares for use in URLs, not making human-readable. |
|
| 14 | + return sanitize_title($value_inputted_for_field_on_model_object); |
|
| 15 | + } |
|
| 16 | 16 | } |
@@ -3,27 +3,27 @@ |
||
| 3 | 3 | class EE_Primary_Key_Int_Field extends EE_Primary_Key_Field_Base |
| 4 | 4 | { |
| 5 | 5 | |
| 6 | - public function __construct($table_column, $nicename) |
|
| 7 | - { |
|
| 8 | - parent::__construct($table_column, $nicename, 0); |
|
| 9 | - $this->setSchemaType('integer'); |
|
| 10 | - } |
|
| 6 | + public function __construct($table_column, $nicename) |
|
| 7 | + { |
|
| 8 | + parent::__construct($table_column, $nicename, 0); |
|
| 9 | + $this->setSchemaType('integer'); |
|
| 10 | + } |
|
| 11 | 11 | |
| 12 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 13 | - { |
|
| 14 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
| 15 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
| 16 | - } |
|
| 17 | - return absint($value_inputted_for_field_on_model_object); |
|
| 18 | - } |
|
| 12 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 13 | + { |
|
| 14 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
| 15 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
| 16 | + } |
|
| 17 | + return absint($value_inputted_for_field_on_model_object); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
| 21 | - { |
|
| 22 | - return intval($value_found_in_db_for_model_object); |
|
| 23 | - } |
|
| 20 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
| 21 | + { |
|
| 22 | + return intval($value_found_in_db_for_model_object); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function is_auto_increment() |
|
| 26 | - { |
|
| 27 | - return true; |
|
| 28 | - } |
|
| 25 | + public function is_auto_increment() |
|
| 26 | + { |
|
| 27 | + return true; |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | /** |
| 128 | 128 | * allow for changing the defaults |
| 129 | 129 | */ |
| 130 | - $this->_nicename = apply_filters( |
|
| 130 | + $this->_nicename = apply_filters( |
|
| 131 | 131 | 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
| 132 | 132 | $this->_nicename, |
| 133 | 133 | $this |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | public function get_qualified_column() |
| 219 | 219 | { |
| 220 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
| 220 | + return $this->get_table_alias().".".$this->get_table_column(); |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | { |
| 313 | 313 | if ($this->is_nullable()) { |
| 314 | 314 | $this->_schema_type = (array) $this->_schema_type; |
| 315 | - if (! in_array('null', $this->_schema_type)) { |
|
| 315 | + if ( ! in_array('null', $this->_schema_type)) { |
|
| 316 | 316 | $this->_schema_type[] = 'null'; |
| 317 | 317 | }; |
| 318 | 318 | } |
@@ -369,10 +369,10 @@ discard block |
||
| 369 | 369 | switch ($property_key) { |
| 370 | 370 | case 'pretty': |
| 371 | 371 | case 'rendered': |
| 372 | - $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
| 372 | + $value_to_return[$property_key] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
| 373 | 373 | break; |
| 374 | 374 | default: |
| 375 | - $value_to_return[ $property_key ] = $default_value; |
|
| 375 | + $value_to_return[$property_key] = $default_value; |
|
| 376 | 376 | break; |
| 377 | 377 | } |
| 378 | 378 | } |
@@ -437,7 +437,7 @@ discard block |
||
| 437 | 437 | */ |
| 438 | 438 | protected function setSchemaReadOnly($readonly) |
| 439 | 439 | { |
| 440 | - if (! is_bool($readonly)) { |
|
| 440 | + if ( ! is_bool($readonly)) { |
|
| 441 | 441 | throw new InvalidArgumentException( |
| 442 | 442 | sprintf( |
| 443 | 443 | esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
@@ -584,7 +584,7 @@ discard block |
||
| 584 | 584 | */ |
| 585 | 585 | private function validateSchemaType($type) |
| 586 | 586 | { |
| 587 | - if (! (is_string($type) || is_array($type))) { |
|
| 587 | + if ( ! (is_string($type) || is_array($type))) { |
|
| 588 | 588 | throw new InvalidArgumentException( |
| 589 | 589 | sprintf( |
| 590 | 590 | esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
@@ -614,7 +614,7 @@ discard block |
||
| 614 | 614 | return; |
| 615 | 615 | } |
| 616 | 616 | |
| 617 | - if (! isset($allowable_types[ $type ])) { |
|
| 617 | + if ( ! isset($allowable_types[$type])) { |
|
| 618 | 618 | throw new InvalidArgumentException( |
| 619 | 619 | sprintf( |
| 620 | 620 | esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
@@ -633,7 +633,7 @@ discard block |
||
| 633 | 633 | */ |
| 634 | 634 | private function validateSchemaFormat($format) |
| 635 | 635 | { |
| 636 | - if (! is_string($format)) { |
|
| 636 | + if ( ! is_string($format)) { |
|
| 637 | 637 | throw new InvalidArgumentException( |
| 638 | 638 | sprintf( |
| 639 | 639 | esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
@@ -656,7 +656,7 @@ discard block |
||
| 656 | 656 | ) |
| 657 | 657 | ); |
| 658 | 658 | |
| 659 | - if (! isset($allowable_formats[ $format ])) { |
|
| 659 | + if ( ! isset($allowable_formats[$format])) { |
|
| 660 | 660 | throw new InvalidArgumentException( |
| 661 | 661 | sprintf( |
| 662 | 662 | esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
@@ -20,651 +20,650 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | abstract class EE_Model_Field_Base implements HasSchemaInterface |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * The alias for the table the column belongs to. |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $_table_alias; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The actual db column name for the table |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - protected $_table_column; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The authoritative name for the table column (used by client code to reference the field). |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - protected $_name; |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * A description for the field. |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $_nicename; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Whether the field is nullable or not |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $_nullable; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * What the default value for the field should be. |
|
| 59 | - * @var mixed |
|
| 60 | - */ |
|
| 61 | - protected $_default_value; |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Other configuration for the field |
|
| 66 | - * @var mixed |
|
| 67 | - */ |
|
| 68 | - protected $_other_config; |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The name of the model this field is instantiated for. |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - protected $_model_name; |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * This should be a json-schema valid data type for the field. |
|
| 80 | - * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - private $_schema_type = 'string'; |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * If the schema has a defined format then it should be defined via this property. |
|
| 88 | - * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
| 89 | - * @var string |
|
| 90 | - */ |
|
| 91 | - private $_schema_format = ''; |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
| 96 | - * settable by client code. |
|
| 97 | - * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
| 98 | - * @var bool |
|
| 99 | - */ |
|
| 100 | - private $_schema_readonly = false; |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $table_column |
|
| 105 | - * @param string $nicename |
|
| 106 | - * @param bool $nullable |
|
| 107 | - * @param null $default_value |
|
| 108 | - */ |
|
| 109 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 110 | - { |
|
| 111 | - $this->_table_column = $table_column; |
|
| 112 | - $this->_nicename = $nicename; |
|
| 113 | - $this->_nullable = $nullable; |
|
| 114 | - $this->_default_value = $default_value; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param $table_alias |
|
| 120 | - * @param $name |
|
| 121 | - * @param $model_name |
|
| 122 | - */ |
|
| 123 | - public function _construct_finalize($table_alias, $name, $model_name) |
|
| 124 | - { |
|
| 125 | - $this->_table_alias = $table_alias; |
|
| 126 | - $this->_name = $name; |
|
| 127 | - $this->_model_name = $model_name; |
|
| 128 | - /** |
|
| 129 | - * allow for changing the defaults |
|
| 130 | - */ |
|
| 131 | - $this->_nicename = apply_filters( |
|
| 132 | - 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
| 133 | - $this->_nicename, |
|
| 134 | - $this |
|
| 135 | - ); |
|
| 136 | - $this->_default_value = apply_filters( |
|
| 137 | - 'FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
| 138 | - $this->_default_value, |
|
| 139 | - $this |
|
| 140 | - ); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - public function get_table_alias() |
|
| 144 | - { |
|
| 145 | - return $this->_table_alias; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function get_table_column() |
|
| 149 | - { |
|
| 150 | - return $this->_table_column; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
| 155 | - * |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function get_model_name() |
|
| 159 | - { |
|
| 160 | - return $this->_model_name; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @throws \EE_Error |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - public function get_name() |
|
| 168 | - { |
|
| 169 | - if ($this->_name) { |
|
| 170 | - return $this->_name; |
|
| 171 | - } else { |
|
| 172 | - throw new EE_Error(sprintf(esc_html__( |
|
| 173 | - "Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
| 174 | - "event_espresso" |
|
| 175 | - ), get_class($this))); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - public function get_nicename() |
|
| 180 | - { |
|
| 181 | - return $this->_nicename; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - public function is_nullable() |
|
| 185 | - { |
|
| 186 | - return $this->_nullable; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * returns whether this field is an auto-increment field or not. If it is, then |
|
| 191 | - * on insertion it can be null. However, on updates it must be present. |
|
| 192 | - * |
|
| 193 | - * @return boolean |
|
| 194 | - */ |
|
| 195 | - public function is_auto_increment() |
|
| 196 | - { |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * The default value in the model object's value domain. See lengthy comment about |
|
| 202 | - * value domains at the top of EEM_Base |
|
| 203 | - * |
|
| 204 | - * @return mixed |
|
| 205 | - */ |
|
| 206 | - public function get_default_value() |
|
| 207 | - { |
|
| 208 | - return $this->_default_value; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Returns the table alias joined to the table column, however this isn't the right |
|
| 213 | - * table alias if the aliased table is being joined to. In that case, you can use |
|
| 214 | - * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
| 215 | - * in the current query |
|
| 216 | - * |
|
| 217 | - * @return string |
|
| 218 | - */ |
|
| 219 | - public function get_qualified_column() |
|
| 220 | - { |
|
| 221 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * When get() is called on a model object (eg EE_Event), before returning its value, |
|
| 226 | - * call this function on it, allowing us to customize the returned value based on |
|
| 227 | - * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
| 228 | - * we simply return it. |
|
| 229 | - * |
|
| 230 | - * @param mixed $value_of_field_on_model_object |
|
| 231 | - * @return mixed |
|
| 232 | - */ |
|
| 233 | - public function prepare_for_get($value_of_field_on_model_object) |
|
| 234 | - { |
|
| 235 | - return $value_of_field_on_model_object; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * When inserting or updating a field on a model object, run this function on each |
|
| 240 | - * value to prepare it for insertion into the db. Generally this converts |
|
| 241 | - * the validated input on the model object into the format used in the DB. |
|
| 242 | - * |
|
| 243 | - * @param mixed $value_of_field_on_model_object |
|
| 244 | - * @return mixed |
|
| 245 | - */ |
|
| 246 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
| 247 | - { |
|
| 248 | - return $value_of_field_on_model_object; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
| 253 | - * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
| 254 | - * By default, we do nothing. |
|
| 255 | - * |
|
| 256 | - * If the model field is going to perform any validation on the input, this is where it should be done |
|
| 257 | - * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
| 258 | - * so it's best to validate it right away). |
|
| 259 | - * |
|
| 260 | - * @param mixed $value_inputted_for_field_on_model_object |
|
| 261 | - * @return mixed |
|
| 262 | - */ |
|
| 263 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 264 | - { |
|
| 265 | - return $value_inputted_for_field_on_model_object; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * When instantiating a model object from DB results, this function is called before setting each field. |
|
| 271 | - * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
| 272 | - * is the one child classes will most often define. |
|
| 273 | - * |
|
| 274 | - * @param mixed $value_found_in_db_for_model_object |
|
| 275 | - * @return mixed |
|
| 276 | - */ |
|
| 277 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
| 278 | - { |
|
| 279 | - return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
| 284 | - * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
| 285 | - * 2013, at 3:23pm" instead of |
|
| 286 | - * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
| 287 | - * |
|
| 288 | - * @param mixed $value_on_field_to_be_outputted |
|
| 289 | - * @return mixed |
|
| 290 | - */ |
|
| 291 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
| 292 | - { |
|
| 293 | - return $value_on_field_to_be_outputted; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Returns whatever is set as the nicename for the object. |
|
| 299 | - * @return string |
|
| 300 | - */ |
|
| 301 | - public function getSchemaDescription() |
|
| 302 | - { |
|
| 303 | - return $this->get_nicename(); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Returns whatever is set as the $_schema_type property for the object. |
|
| 309 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
| 310 | - * @return string|array |
|
| 311 | - */ |
|
| 312 | - public function getSchemaType() |
|
| 313 | - { |
|
| 314 | - if ($this->is_nullable()) { |
|
| 315 | - $this->_schema_type = (array) $this->_schema_type; |
|
| 316 | - if (! in_array('null', $this->_schema_type)) { |
|
| 317 | - $this->_schema_type[] = 'null'; |
|
| 318 | - }; |
|
| 319 | - } |
|
| 320 | - return $this->_schema_type; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
| 326 | - * for this property. |
|
| 327 | - * @param string|array $type |
|
| 328 | - * @throws InvalidArgumentException |
|
| 329 | - */ |
|
| 330 | - protected function setSchemaType($type) |
|
| 331 | - { |
|
| 332 | - $this->validateSchemaType($type); |
|
| 333 | - $this->_schema_type = $type; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
| 339 | - * this method and return the properties for the schema. |
|
| 340 | - * |
|
| 341 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
| 342 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
| 343 | - * |
|
| 344 | - * @return array |
|
| 345 | - */ |
|
| 346 | - public function getSchemaProperties() |
|
| 347 | - { |
|
| 348 | - return array(); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
| 355 | - * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
| 356 | - * property keys and correctly prepare the default according to that expected property value. |
|
| 357 | - * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
| 358 | - * |
|
| 359 | - * @return mixed |
|
| 360 | - */ |
|
| 361 | - public function getSchemaDefault() |
|
| 362 | - { |
|
| 363 | - $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
| 364 | - $schema_properties = $this->getSchemaProperties(); |
|
| 365 | - |
|
| 366 | - // if this schema has properties than shape the default value to match the properties shape. |
|
| 367 | - if ($schema_properties) { |
|
| 368 | - $value_to_return = array(); |
|
| 369 | - foreach ($schema_properties as $property_key => $property_schema) { |
|
| 370 | - switch ($property_key) { |
|
| 371 | - case 'pretty': |
|
| 372 | - case 'rendered': |
|
| 373 | - $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
| 374 | - break; |
|
| 375 | - default: |
|
| 376 | - $value_to_return[ $property_key ] = $default_value; |
|
| 377 | - break; |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - $default_value = $value_to_return; |
|
| 381 | - } |
|
| 382 | - return $default_value; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * If a child class has enum values, they should override this method and provide a simple array |
|
| 390 | - * of the enum values. |
|
| 391 | - |
|
| 392 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
| 393 | - * are set on the instantiated object that could be filtered after construct. |
|
| 394 | - * |
|
| 395 | - * @return array |
|
| 396 | - */ |
|
| 397 | - public function getSchemaEnum() |
|
| 398 | - { |
|
| 399 | - return array(); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * This returns the value of the $_schema_format property on the object. |
|
| 405 | - * @return string |
|
| 406 | - */ |
|
| 407 | - public function getSchemaFormat() |
|
| 408 | - { |
|
| 409 | - return $this->_schema_format; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Sets the schema format property. |
|
| 415 | - * @throws InvalidArgumentException |
|
| 416 | - * @param string $format |
|
| 417 | - */ |
|
| 418 | - protected function setSchemaFormat($format) |
|
| 419 | - { |
|
| 420 | - $this->validateSchemaFormat($format); |
|
| 421 | - $this->_schema_format = $format; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * This returns the value of the $_schema_readonly property on the object. |
|
| 427 | - * @return bool |
|
| 428 | - */ |
|
| 429 | - public function getSchemaReadonly() |
|
| 430 | - { |
|
| 431 | - return $this->_schema_readonly; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * This sets the value for the $_schema_readonly property. |
|
| 437 | - * @param bool $readonly (only explicit boolean values are accepted) |
|
| 438 | - */ |
|
| 439 | - protected function setSchemaReadOnly($readonly) |
|
| 440 | - { |
|
| 441 | - if (! is_bool($readonly)) { |
|
| 442 | - throw new InvalidArgumentException( |
|
| 443 | - sprintf( |
|
| 444 | - esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
| 445 | - print_r($readonly, true) |
|
| 446 | - ) |
|
| 447 | - ); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - $this->_schema_readonly = $readonly; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
| 458 | - * @uses _get_wpdb_data_type() |
|
| 459 | - * |
|
| 460 | - * @return string |
|
| 461 | - */ |
|
| 462 | - public function get_wpdb_data_type() |
|
| 463 | - { |
|
| 464 | - return $this->_get_wpdb_data_type(); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
| 470 | - * @param string $type Included if a specific type is requested. |
|
| 471 | - * @uses get_schema_type() |
|
| 472 | - * @return string |
|
| 473 | - */ |
|
| 474 | - protected function _get_wpdb_data_type($type = '') |
|
| 475 | - { |
|
| 476 | - $type = empty($type) ? $this->getSchemaType() : $type; |
|
| 477 | - |
|
| 478 | - // if type is an array, then different parsing is required. |
|
| 479 | - if (is_array($type)) { |
|
| 480 | - return $this->_get_wpdb_data_type_for_type_array($type); |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - $wpdb_type = '%s'; |
|
| 484 | - switch ($type) { |
|
| 485 | - case 'number': |
|
| 486 | - $wpdb_type = '%f'; |
|
| 487 | - break; |
|
| 488 | - case 'integer': |
|
| 489 | - case 'boolean': |
|
| 490 | - $wpdb_type = '%d'; |
|
| 491 | - break; |
|
| 492 | - case 'object': |
|
| 493 | - $properties = $this->getSchemaProperties(); |
|
| 494 | - if (isset($properties['raw'], $properties['raw']['type'])) { |
|
| 495 | - $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
| 496 | - } |
|
| 497 | - break; // leave at default |
|
| 498 | - } |
|
| 499 | - return $wpdb_type; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - |
|
| 503 | - |
|
| 504 | - protected function _get_wpdb_data_type_for_type_array($type) |
|
| 505 | - { |
|
| 506 | - $type = (array) $type; |
|
| 507 | - // first let's flip because then we can do a faster key check |
|
| 508 | - $type = array_flip($type); |
|
| 509 | - |
|
| 510 | - // check for things that mean '%s' |
|
| 511 | - if (isset($type['string'], $type['object'], $type['array'])) { |
|
| 512 | - return '%s'; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - // if makes it past the above condition and there's float in the array |
|
| 516 | - // then the type is %f |
|
| 517 | - if (isset($type['number'])) { |
|
| 518 | - return '%f'; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - // if it makes it above the above conditions and there is an integer in the array |
|
| 522 | - // then the type is %d |
|
| 523 | - if (isset($type['integer'])) { |
|
| 524 | - return '%d'; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - // anything else is a string |
|
| 528 | - return '%s'; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * This returns elements used to represent this field in the json schema. |
|
| 534 | - * |
|
| 535 | - * @link http://json-schema.org/ |
|
| 536 | - * @return array |
|
| 537 | - */ |
|
| 538 | - public function getSchema() |
|
| 539 | - { |
|
| 540 | - $schema = array( |
|
| 541 | - 'description' => $this->getSchemaDescription(), |
|
| 542 | - 'type' => $this->getSchemaType(), |
|
| 543 | - 'readonly' => $this->getSchemaReadonly(), |
|
| 544 | - 'default' => $this->getSchemaDefault() |
|
| 545 | - ); |
|
| 546 | - |
|
| 547 | - // optional properties of the schema |
|
| 548 | - $enum = $this->getSchemaEnum(); |
|
| 549 | - $properties = $this->getSchemaProperties(); |
|
| 550 | - $format = $this->getSchemaFormat(); |
|
| 551 | - if ($enum) { |
|
| 552 | - $schema['enum'] = $enum; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - if ($properties) { |
|
| 556 | - $schema['properties'] = $properties; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - if ($format) { |
|
| 560 | - $schema['format'] = $format; |
|
| 561 | - } |
|
| 562 | - return $schema; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
| 567 | - * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
| 568 | - * want to see their value, then they shouldn't be db-only fields!) |
|
| 569 | - * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
| 570 | - * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
| 571 | - * By default, all fields aren't db-only. |
|
| 572 | - * |
|
| 573 | - * @return boolean |
|
| 574 | - */ |
|
| 575 | - public function is_db_only_field() |
|
| 576 | - { |
|
| 577 | - return false; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Validates the incoming string|array to ensure its an allowable type. |
|
| 583 | - * @throws InvalidArgumentException |
|
| 584 | - * @param string|array $type |
|
| 585 | - */ |
|
| 586 | - private function validateSchemaType($type) |
|
| 587 | - { |
|
| 588 | - if (! (is_string($type) || is_array($type))) { |
|
| 589 | - throw new InvalidArgumentException( |
|
| 590 | - sprintf( |
|
| 591 | - esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
| 592 | - print_r($type, true) |
|
| 593 | - ) |
|
| 594 | - ); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - // validate allowable types. |
|
| 598 | - // @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
| 599 | - $allowable_types = array_flip( |
|
| 600 | - array( |
|
| 601 | - 'string', |
|
| 602 | - 'number', |
|
| 603 | - 'null', |
|
| 604 | - 'object', |
|
| 605 | - 'array', |
|
| 606 | - 'boolean', |
|
| 607 | - 'integer' |
|
| 608 | - ) |
|
| 609 | - ); |
|
| 610 | - |
|
| 611 | - if (is_array($type)) { |
|
| 612 | - foreach ($type as $item_in_type) { |
|
| 613 | - $this->validateSchemaType($item_in_type); |
|
| 614 | - } |
|
| 615 | - return; |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - if (! isset($allowable_types[ $type ])) { |
|
| 619 | - throw new InvalidArgumentException( |
|
| 620 | - sprintf( |
|
| 621 | - esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
| 622 | - $type, |
|
| 623 | - implode(',', array_flip($allowable_types)) |
|
| 624 | - ) |
|
| 625 | - ); |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
| 632 | - * @throws InvalidArgumentException |
|
| 633 | - * @param $format |
|
| 634 | - */ |
|
| 635 | - private function validateSchemaFormat($format) |
|
| 636 | - { |
|
| 637 | - if (! is_string($format)) { |
|
| 638 | - throw new InvalidArgumentException( |
|
| 639 | - sprintf( |
|
| 640 | - esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
| 641 | - print_r($format, true) |
|
| 642 | - ) |
|
| 643 | - ); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - // validate allowable format values |
|
| 647 | - // @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
| 648 | - $allowable_formats = array_flip( |
|
| 649 | - array( |
|
| 650 | - 'date-time', |
|
| 651 | - 'email', |
|
| 652 | - 'hostname', |
|
| 653 | - 'ipv4', |
|
| 654 | - 'ipv6', |
|
| 655 | - 'uri', |
|
| 656 | - 'uriref' |
|
| 657 | - ) |
|
| 658 | - ); |
|
| 659 | - |
|
| 660 | - if (! isset($allowable_formats[ $format ])) { |
|
| 661 | - throw new InvalidArgumentException( |
|
| 662 | - sprintf( |
|
| 663 | - esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
| 664 | - $format, |
|
| 665 | - implode(',', array_flip($allowable_formats)) |
|
| 666 | - ) |
|
| 667 | - ); |
|
| 668 | - } |
|
| 669 | - } |
|
| 23 | + /** |
|
| 24 | + * The alias for the table the column belongs to. |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $_table_alias; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The actual db column name for the table |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + protected $_table_column; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The authoritative name for the table column (used by client code to reference the field). |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + protected $_name; |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * A description for the field. |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $_nicename; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Whether the field is nullable or not |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $_nullable; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * What the default value for the field should be. |
|
| 59 | + * @var mixed |
|
| 60 | + */ |
|
| 61 | + protected $_default_value; |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Other configuration for the field |
|
| 66 | + * @var mixed |
|
| 67 | + */ |
|
| 68 | + protected $_other_config; |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The name of the model this field is instantiated for. |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + protected $_model_name; |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * This should be a json-schema valid data type for the field. |
|
| 80 | + * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + private $_schema_type = 'string'; |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * If the schema has a defined format then it should be defined via this property. |
|
| 88 | + * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
| 89 | + * @var string |
|
| 90 | + */ |
|
| 91 | + private $_schema_format = ''; |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
| 96 | + * settable by client code. |
|
| 97 | + * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
| 98 | + * @var bool |
|
| 99 | + */ |
|
| 100 | + private $_schema_readonly = false; |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $table_column |
|
| 105 | + * @param string $nicename |
|
| 106 | + * @param bool $nullable |
|
| 107 | + * @param null $default_value |
|
| 108 | + */ |
|
| 109 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 110 | + { |
|
| 111 | + $this->_table_column = $table_column; |
|
| 112 | + $this->_nicename = $nicename; |
|
| 113 | + $this->_nullable = $nullable; |
|
| 114 | + $this->_default_value = $default_value; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param $table_alias |
|
| 120 | + * @param $name |
|
| 121 | + * @param $model_name |
|
| 122 | + */ |
|
| 123 | + public function _construct_finalize($table_alias, $name, $model_name) |
|
| 124 | + { |
|
| 125 | + $this->_table_alias = $table_alias; |
|
| 126 | + $this->_name = $name; |
|
| 127 | + $this->_model_name = $model_name; |
|
| 128 | + /** |
|
| 129 | + * allow for changing the defaults |
|
| 130 | + */ |
|
| 131 | + $this->_nicename = apply_filters( |
|
| 132 | + 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
| 133 | + $this->_nicename, |
|
| 134 | + $this |
|
| 135 | + ); |
|
| 136 | + $this->_default_value = apply_filters( |
|
| 137 | + 'FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
| 138 | + $this->_default_value, |
|
| 139 | + $this |
|
| 140 | + ); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + public function get_table_alias() |
|
| 144 | + { |
|
| 145 | + return $this->_table_alias; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function get_table_column() |
|
| 149 | + { |
|
| 150 | + return $this->_table_column; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
| 155 | + * |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function get_model_name() |
|
| 159 | + { |
|
| 160 | + return $this->_model_name; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @throws \EE_Error |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + public function get_name() |
|
| 168 | + { |
|
| 169 | + if ($this->_name) { |
|
| 170 | + return $this->_name; |
|
| 171 | + } else { |
|
| 172 | + throw new EE_Error(sprintf(esc_html__( |
|
| 173 | + "Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
| 174 | + "event_espresso" |
|
| 175 | + ), get_class($this))); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + public function get_nicename() |
|
| 180 | + { |
|
| 181 | + return $this->_nicename; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + public function is_nullable() |
|
| 185 | + { |
|
| 186 | + return $this->_nullable; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * returns whether this field is an auto-increment field or not. If it is, then |
|
| 191 | + * on insertion it can be null. However, on updates it must be present. |
|
| 192 | + * |
|
| 193 | + * @return boolean |
|
| 194 | + */ |
|
| 195 | + public function is_auto_increment() |
|
| 196 | + { |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * The default value in the model object's value domain. See lengthy comment about |
|
| 202 | + * value domains at the top of EEM_Base |
|
| 203 | + * |
|
| 204 | + * @return mixed |
|
| 205 | + */ |
|
| 206 | + public function get_default_value() |
|
| 207 | + { |
|
| 208 | + return $this->_default_value; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Returns the table alias joined to the table column, however this isn't the right |
|
| 213 | + * table alias if the aliased table is being joined to. In that case, you can use |
|
| 214 | + * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
| 215 | + * in the current query |
|
| 216 | + * |
|
| 217 | + * @return string |
|
| 218 | + */ |
|
| 219 | + public function get_qualified_column() |
|
| 220 | + { |
|
| 221 | + return $this->get_table_alias() . "." . $this->get_table_column(); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * When get() is called on a model object (eg EE_Event), before returning its value, |
|
| 226 | + * call this function on it, allowing us to customize the returned value based on |
|
| 227 | + * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
| 228 | + * we simply return it. |
|
| 229 | + * |
|
| 230 | + * @param mixed $value_of_field_on_model_object |
|
| 231 | + * @return mixed |
|
| 232 | + */ |
|
| 233 | + public function prepare_for_get($value_of_field_on_model_object) |
|
| 234 | + { |
|
| 235 | + return $value_of_field_on_model_object; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * When inserting or updating a field on a model object, run this function on each |
|
| 240 | + * value to prepare it for insertion into the db. Generally this converts |
|
| 241 | + * the validated input on the model object into the format used in the DB. |
|
| 242 | + * |
|
| 243 | + * @param mixed $value_of_field_on_model_object |
|
| 244 | + * @return mixed |
|
| 245 | + */ |
|
| 246 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
| 247 | + { |
|
| 248 | + return $value_of_field_on_model_object; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
| 253 | + * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
| 254 | + * By default, we do nothing. |
|
| 255 | + * |
|
| 256 | + * If the model field is going to perform any validation on the input, this is where it should be done |
|
| 257 | + * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
| 258 | + * so it's best to validate it right away). |
|
| 259 | + * |
|
| 260 | + * @param mixed $value_inputted_for_field_on_model_object |
|
| 261 | + * @return mixed |
|
| 262 | + */ |
|
| 263 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 264 | + { |
|
| 265 | + return $value_inputted_for_field_on_model_object; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * When instantiating a model object from DB results, this function is called before setting each field. |
|
| 271 | + * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
| 272 | + * is the one child classes will most often define. |
|
| 273 | + * |
|
| 274 | + * @param mixed $value_found_in_db_for_model_object |
|
| 275 | + * @return mixed |
|
| 276 | + */ |
|
| 277 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
| 278 | + { |
|
| 279 | + return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
| 284 | + * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
| 285 | + * 2013, at 3:23pm" instead of |
|
| 286 | + * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
| 287 | + * |
|
| 288 | + * @param mixed $value_on_field_to_be_outputted |
|
| 289 | + * @return mixed |
|
| 290 | + */ |
|
| 291 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
| 292 | + { |
|
| 293 | + return $value_on_field_to_be_outputted; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Returns whatever is set as the nicename for the object. |
|
| 299 | + * @return string |
|
| 300 | + */ |
|
| 301 | + public function getSchemaDescription() |
|
| 302 | + { |
|
| 303 | + return $this->get_nicename(); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Returns whatever is set as the $_schema_type property for the object. |
|
| 309 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
| 310 | + * @return string|array |
|
| 311 | + */ |
|
| 312 | + public function getSchemaType() |
|
| 313 | + { |
|
| 314 | + if ($this->is_nullable()) { |
|
| 315 | + $this->_schema_type = (array) $this->_schema_type; |
|
| 316 | + if (! in_array('null', $this->_schema_type)) { |
|
| 317 | + $this->_schema_type[] = 'null'; |
|
| 318 | + }; |
|
| 319 | + } |
|
| 320 | + return $this->_schema_type; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
| 326 | + * for this property. |
|
| 327 | + * @param string|array $type |
|
| 328 | + * @throws InvalidArgumentException |
|
| 329 | + */ |
|
| 330 | + protected function setSchemaType($type) |
|
| 331 | + { |
|
| 332 | + $this->validateSchemaType($type); |
|
| 333 | + $this->_schema_type = $type; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
| 339 | + * this method and return the properties for the schema. |
|
| 340 | + * |
|
| 341 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
| 342 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
| 343 | + * |
|
| 344 | + * @return array |
|
| 345 | + */ |
|
| 346 | + public function getSchemaProperties() |
|
| 347 | + { |
|
| 348 | + return array(); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
| 355 | + * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
| 356 | + * property keys and correctly prepare the default according to that expected property value. |
|
| 357 | + * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
| 358 | + * |
|
| 359 | + * @return mixed |
|
| 360 | + */ |
|
| 361 | + public function getSchemaDefault() |
|
| 362 | + { |
|
| 363 | + $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
| 364 | + $schema_properties = $this->getSchemaProperties(); |
|
| 365 | + |
|
| 366 | + // if this schema has properties than shape the default value to match the properties shape. |
|
| 367 | + if ($schema_properties) { |
|
| 368 | + $value_to_return = array(); |
|
| 369 | + foreach ($schema_properties as $property_key => $property_schema) { |
|
| 370 | + switch ($property_key) { |
|
| 371 | + case 'pretty': |
|
| 372 | + case 'rendered': |
|
| 373 | + $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
| 374 | + break; |
|
| 375 | + default: |
|
| 376 | + $value_to_return[ $property_key ] = $default_value; |
|
| 377 | + break; |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + $default_value = $value_to_return; |
|
| 381 | + } |
|
| 382 | + return $default_value; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * If a child class has enum values, they should override this method and provide a simple array |
|
| 390 | + * of the enum values. |
|
| 391 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
| 392 | + * are set on the instantiated object that could be filtered after construct. |
|
| 393 | + * |
|
| 394 | + * @return array |
|
| 395 | + */ |
|
| 396 | + public function getSchemaEnum() |
|
| 397 | + { |
|
| 398 | + return array(); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * This returns the value of the $_schema_format property on the object. |
|
| 404 | + * @return string |
|
| 405 | + */ |
|
| 406 | + public function getSchemaFormat() |
|
| 407 | + { |
|
| 408 | + return $this->_schema_format; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Sets the schema format property. |
|
| 414 | + * @throws InvalidArgumentException |
|
| 415 | + * @param string $format |
|
| 416 | + */ |
|
| 417 | + protected function setSchemaFormat($format) |
|
| 418 | + { |
|
| 419 | + $this->validateSchemaFormat($format); |
|
| 420 | + $this->_schema_format = $format; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * This returns the value of the $_schema_readonly property on the object. |
|
| 426 | + * @return bool |
|
| 427 | + */ |
|
| 428 | + public function getSchemaReadonly() |
|
| 429 | + { |
|
| 430 | + return $this->_schema_readonly; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * This sets the value for the $_schema_readonly property. |
|
| 436 | + * @param bool $readonly (only explicit boolean values are accepted) |
|
| 437 | + */ |
|
| 438 | + protected function setSchemaReadOnly($readonly) |
|
| 439 | + { |
|
| 440 | + if (! is_bool($readonly)) { |
|
| 441 | + throw new InvalidArgumentException( |
|
| 442 | + sprintf( |
|
| 443 | + esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
| 444 | + print_r($readonly, true) |
|
| 445 | + ) |
|
| 446 | + ); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $this->_schema_readonly = $readonly; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
| 457 | + * @uses _get_wpdb_data_type() |
|
| 458 | + * |
|
| 459 | + * @return string |
|
| 460 | + */ |
|
| 461 | + public function get_wpdb_data_type() |
|
| 462 | + { |
|
| 463 | + return $this->_get_wpdb_data_type(); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
| 469 | + * @param string $type Included if a specific type is requested. |
|
| 470 | + * @uses get_schema_type() |
|
| 471 | + * @return string |
|
| 472 | + */ |
|
| 473 | + protected function _get_wpdb_data_type($type = '') |
|
| 474 | + { |
|
| 475 | + $type = empty($type) ? $this->getSchemaType() : $type; |
|
| 476 | + |
|
| 477 | + // if type is an array, then different parsing is required. |
|
| 478 | + if (is_array($type)) { |
|
| 479 | + return $this->_get_wpdb_data_type_for_type_array($type); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + $wpdb_type = '%s'; |
|
| 483 | + switch ($type) { |
|
| 484 | + case 'number': |
|
| 485 | + $wpdb_type = '%f'; |
|
| 486 | + break; |
|
| 487 | + case 'integer': |
|
| 488 | + case 'boolean': |
|
| 489 | + $wpdb_type = '%d'; |
|
| 490 | + break; |
|
| 491 | + case 'object': |
|
| 492 | + $properties = $this->getSchemaProperties(); |
|
| 493 | + if (isset($properties['raw'], $properties['raw']['type'])) { |
|
| 494 | + $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
| 495 | + } |
|
| 496 | + break; // leave at default |
|
| 497 | + } |
|
| 498 | + return $wpdb_type; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + |
|
| 502 | + |
|
| 503 | + protected function _get_wpdb_data_type_for_type_array($type) |
|
| 504 | + { |
|
| 505 | + $type = (array) $type; |
|
| 506 | + // first let's flip because then we can do a faster key check |
|
| 507 | + $type = array_flip($type); |
|
| 508 | + |
|
| 509 | + // check for things that mean '%s' |
|
| 510 | + if (isset($type['string'], $type['object'], $type['array'])) { |
|
| 511 | + return '%s'; |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + // if makes it past the above condition and there's float in the array |
|
| 515 | + // then the type is %f |
|
| 516 | + if (isset($type['number'])) { |
|
| 517 | + return '%f'; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + // if it makes it above the above conditions and there is an integer in the array |
|
| 521 | + // then the type is %d |
|
| 522 | + if (isset($type['integer'])) { |
|
| 523 | + return '%d'; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + // anything else is a string |
|
| 527 | + return '%s'; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * This returns elements used to represent this field in the json schema. |
|
| 533 | + * |
|
| 534 | + * @link http://json-schema.org/ |
|
| 535 | + * @return array |
|
| 536 | + */ |
|
| 537 | + public function getSchema() |
|
| 538 | + { |
|
| 539 | + $schema = array( |
|
| 540 | + 'description' => $this->getSchemaDescription(), |
|
| 541 | + 'type' => $this->getSchemaType(), |
|
| 542 | + 'readonly' => $this->getSchemaReadonly(), |
|
| 543 | + 'default' => $this->getSchemaDefault() |
|
| 544 | + ); |
|
| 545 | + |
|
| 546 | + // optional properties of the schema |
|
| 547 | + $enum = $this->getSchemaEnum(); |
|
| 548 | + $properties = $this->getSchemaProperties(); |
|
| 549 | + $format = $this->getSchemaFormat(); |
|
| 550 | + if ($enum) { |
|
| 551 | + $schema['enum'] = $enum; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + if ($properties) { |
|
| 555 | + $schema['properties'] = $properties; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + if ($format) { |
|
| 559 | + $schema['format'] = $format; |
|
| 560 | + } |
|
| 561 | + return $schema; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
| 566 | + * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
| 567 | + * want to see their value, then they shouldn't be db-only fields!) |
|
| 568 | + * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
| 569 | + * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
| 570 | + * By default, all fields aren't db-only. |
|
| 571 | + * |
|
| 572 | + * @return boolean |
|
| 573 | + */ |
|
| 574 | + public function is_db_only_field() |
|
| 575 | + { |
|
| 576 | + return false; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Validates the incoming string|array to ensure its an allowable type. |
|
| 582 | + * @throws InvalidArgumentException |
|
| 583 | + * @param string|array $type |
|
| 584 | + */ |
|
| 585 | + private function validateSchemaType($type) |
|
| 586 | + { |
|
| 587 | + if (! (is_string($type) || is_array($type))) { |
|
| 588 | + throw new InvalidArgumentException( |
|
| 589 | + sprintf( |
|
| 590 | + esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
| 591 | + print_r($type, true) |
|
| 592 | + ) |
|
| 593 | + ); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + // validate allowable types. |
|
| 597 | + // @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
| 598 | + $allowable_types = array_flip( |
|
| 599 | + array( |
|
| 600 | + 'string', |
|
| 601 | + 'number', |
|
| 602 | + 'null', |
|
| 603 | + 'object', |
|
| 604 | + 'array', |
|
| 605 | + 'boolean', |
|
| 606 | + 'integer' |
|
| 607 | + ) |
|
| 608 | + ); |
|
| 609 | + |
|
| 610 | + if (is_array($type)) { |
|
| 611 | + foreach ($type as $item_in_type) { |
|
| 612 | + $this->validateSchemaType($item_in_type); |
|
| 613 | + } |
|
| 614 | + return; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + if (! isset($allowable_types[ $type ])) { |
|
| 618 | + throw new InvalidArgumentException( |
|
| 619 | + sprintf( |
|
| 620 | + esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
| 621 | + $type, |
|
| 622 | + implode(',', array_flip($allowable_types)) |
|
| 623 | + ) |
|
| 624 | + ); |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
| 631 | + * @throws InvalidArgumentException |
|
| 632 | + * @param $format |
|
| 633 | + */ |
|
| 634 | + private function validateSchemaFormat($format) |
|
| 635 | + { |
|
| 636 | + if (! is_string($format)) { |
|
| 637 | + throw new InvalidArgumentException( |
|
| 638 | + sprintf( |
|
| 639 | + esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
| 640 | + print_r($format, true) |
|
| 641 | + ) |
|
| 642 | + ); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + // validate allowable format values |
|
| 646 | + // @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
| 647 | + $allowable_formats = array_flip( |
|
| 648 | + array( |
|
| 649 | + 'date-time', |
|
| 650 | + 'email', |
|
| 651 | + 'hostname', |
|
| 652 | + 'ipv4', |
|
| 653 | + 'ipv6', |
|
| 654 | + 'uri', |
|
| 655 | + 'uriref' |
|
| 656 | + ) |
|
| 657 | + ); |
|
| 658 | + |
|
| 659 | + if (! isset($allowable_formats[ $format ])) { |
|
| 660 | + throw new InvalidArgumentException( |
|
| 661 | + sprintf( |
|
| 662 | + esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
| 663 | + $format, |
|
| 664 | + implode(',', array_flip($allowable_formats)) |
|
| 665 | + ) |
|
| 666 | + ); |
|
| 667 | + } |
|
| 668 | + } |
|
| 670 | 669 | } |