Completed
Branch dependabot/npm_and_yarn/wordpr... (0475b3)
by
unknown
14:52 queued 12:42
created
core/libraries/rest_api/calculations/Registration.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -96,7 +96,7 @@
 block discarded – undo
96 96
                     $status_pretty = 'NEVER';
97 97
                     break;
98 98
             }
99
-            $checkin_stati[ $datetime_id ] = $status_pretty;
99
+            $checkin_stati[$datetime_id] = $status_pretty;
100 100
         }
101 101
         return $checkin_stati;
102 102
     }
Please login to merge, or discard this patch.
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -24,109 +24,109 @@
 block discarded – undo
24 24
  */
25 25
 class Registration extends RegistrationCalculationBase
26 26
 {
27
-    /**
28
-     * @var EEM_Registration
29
-     */
30
-    protected $registration_model;
27
+	/**
28
+	 * @var EEM_Registration
29
+	 */
30
+	protected $registration_model;
31 31
 
32
-    /**
33
-     * Registration constructor.
34
-     * @param EEM_Registration $registration_model
35
-     */
36
-    public function __construct(EEM_Registration $registration_model)
37
-    {
38
-        $this->registration_model = $registration_model;
39
-    }
32
+	/**
33
+	 * Registration constructor.
34
+	 * @param EEM_Registration $registration_model
35
+	 */
36
+	public function __construct(EEM_Registration $registration_model)
37
+	{
38
+		$this->registration_model = $registration_model;
39
+	}
40 40
 
41
-    /**
42
-     * Calculates the checkin status for each datetime this registration has access to
43
-     *
44
-     * @param array            $wpdb_row
45
-     * @param WP_REST_Request $request
46
-     * @param RegistrationControllerBase $controller
47
-     * @return array
48
-     * @throws EE_Error
49
-     * @throws InvalidDataTypeException
50
-     * @throws InvalidInterfaceException
51
-     * @throws InvalidArgumentException
52
-     */
53
-    public function datetimeCheckinStati($wpdb_row, $request, $controller)
54
-    {
55
-        if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) {
56
-            $reg = $this->registration_model->get_one_by_ID($wpdb_row['Registration.REG_ID']);
57
-        } else {
58
-            $reg = null;
59
-        }
60
-        if (
61
-            ! $reg instanceof EE_Registration
62
-        ) {
63
-            throw new EE_Error(
64
-                sprintf(
65
-                    esc_html__(
66
-                    // @codingStandardsIgnoreStart
67
-                        'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found',
68
-                        // @codingStandardsIgnoreEnd
69
-                        'event_espresso'
70
-                    ),
71
-                    $wpdb_row['Registration.REG_ID'],
72
-                    print_r($wpdb_row, true)
73
-                )
74
-            );
75
-        }
76
-        $datetime_ids = EEM_Datetime::instance()->get_col(
77
-            [
78
-                [
79
-                    'Ticket.TKT_ID' => $reg->ticket_ID(),
80
-                ],
81
-                'default_where_conditions' => EEM_Base::default_where_conditions_minimum_all,
82
-            ]
83
-        );
84
-        $checkin_stati = array();
85
-        foreach ($datetime_ids as $datetime_id) {
86
-            $status = $reg->check_in_status_for_datetime($datetime_id);
87
-            switch ($status) {
88
-                case EE_Checkin::status_checked_out:
89
-                    $status_pretty = 'OUT';
90
-                    break;
91
-                case EE_Checkin::status_checked_in:
92
-                    $status_pretty = 'IN';
93
-                    break;
94
-                case EE_Checkin::status_checked_never:
95
-                default:
96
-                    $status_pretty = 'NEVER';
97
-                    break;
98
-            }
99
-            $checkin_stati[ $datetime_id ] = $status_pretty;
100
-        }
101
-        return $checkin_stati;
102
-    }
41
+	/**
42
+	 * Calculates the checkin status for each datetime this registration has access to
43
+	 *
44
+	 * @param array            $wpdb_row
45
+	 * @param WP_REST_Request $request
46
+	 * @param RegistrationControllerBase $controller
47
+	 * @return array
48
+	 * @throws EE_Error
49
+	 * @throws InvalidDataTypeException
50
+	 * @throws InvalidInterfaceException
51
+	 * @throws InvalidArgumentException
52
+	 */
53
+	public function datetimeCheckinStati($wpdb_row, $request, $controller)
54
+	{
55
+		if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) {
56
+			$reg = $this->registration_model->get_one_by_ID($wpdb_row['Registration.REG_ID']);
57
+		} else {
58
+			$reg = null;
59
+		}
60
+		if (
61
+			! $reg instanceof EE_Registration
62
+		) {
63
+			throw new EE_Error(
64
+				sprintf(
65
+					esc_html__(
66
+					// @codingStandardsIgnoreStart
67
+						'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found',
68
+						// @codingStandardsIgnoreEnd
69
+						'event_espresso'
70
+					),
71
+					$wpdb_row['Registration.REG_ID'],
72
+					print_r($wpdb_row, true)
73
+				)
74
+			);
75
+		}
76
+		$datetime_ids = EEM_Datetime::instance()->get_col(
77
+			[
78
+				[
79
+					'Ticket.TKT_ID' => $reg->ticket_ID(),
80
+				],
81
+				'default_where_conditions' => EEM_Base::default_where_conditions_minimum_all,
82
+			]
83
+		);
84
+		$checkin_stati = array();
85
+		foreach ($datetime_ids as $datetime_id) {
86
+			$status = $reg->check_in_status_for_datetime($datetime_id);
87
+			switch ($status) {
88
+				case EE_Checkin::status_checked_out:
89
+					$status_pretty = 'OUT';
90
+					break;
91
+				case EE_Checkin::status_checked_in:
92
+					$status_pretty = 'IN';
93
+					break;
94
+				case EE_Checkin::status_checked_never:
95
+				default:
96
+					$status_pretty = 'NEVER';
97
+					break;
98
+			}
99
+			$checkin_stati[ $datetime_id ] = $status_pretty;
100
+		}
101
+		return $checkin_stati;
102
+	}
103 103
 
104 104
 
105
-    /**
106
-     * Provides an array for all the calculations possible that outlines a json schema for those calculations.
107
-     * Array is indexed by calculation (snake case) and value is the schema for that calculation.
108
-     *
109
-     * @since 4.9.68.p
110
-     * @return array
111
-     */
112
-    public function schemaForCalculations()
113
-    {
114
-        return array(
115
-            'datetime_checkin_stati' => array(
116
-                'description' => esc_html__(
117
-                    'Returns the checkin status for each datetime this registration has access to.',
118
-                    'event_espresso'
119
-                ),
120
-                'type' => 'object',
121
-                'properties' => array(),
122
-                'additionalProperties' => array(
123
-                    'description' => esc_html__(
124
-                        'Keys are date-time ids and values are the check-in status',
125
-                        'event_espresso'
126
-                    ),
127
-                    'type' => 'string'
128
-                ),
129
-            ),
130
-        );
131
-    }
105
+	/**
106
+	 * Provides an array for all the calculations possible that outlines a json schema for those calculations.
107
+	 * Array is indexed by calculation (snake case) and value is the schema for that calculation.
108
+	 *
109
+	 * @since 4.9.68.p
110
+	 * @return array
111
+	 */
112
+	public function schemaForCalculations()
113
+	{
114
+		return array(
115
+			'datetime_checkin_stati' => array(
116
+				'description' => esc_html__(
117
+					'Returns the checkin status for each datetime this registration has access to.',
118
+					'event_espresso'
119
+				),
120
+				'type' => 'object',
121
+				'properties' => array(),
122
+				'additionalProperties' => array(
123
+					'description' => esc_html__(
124
+						'Keys are date-time ids and values are the check-in status',
125
+						'event_espresso'
126
+					),
127
+					'type' => 'string'
128
+				),
129
+			),
130
+		);
131
+	}
132 132
 }
Please login to merge, or discard this patch.
core/libraries/rest_api/CalculatedModelFields.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
      */
58 58
     public function mapping($refresh = false)
59 59
     {
60
-        if (! $this->mapping || $refresh) {
60
+        if ( ! $this->mapping || $refresh) {
61 61
             $this->mapping = $this->generateNewMapping();
62 62
         }
63 63
         return $this->mapping;
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
         foreach ($models_with_calculated_fields as $model_name) {
82 82
             $calculator = $this->factory->createFromModel($model_name);
83 83
             foreach (array_keys(call_user_func(array($calculator, 'schemaForCalculations'))) as $field_name) {
84
-                $mapping[ $model_name ][ $field_name ] = get_class($calculator);
84
+                $mapping[$model_name][$field_name] = get_class($calculator);
85 85
             }
86 86
         }
87 87
         return apply_filters(
@@ -108,8 +108,8 @@  discard block
 block discarded – undo
108 108
             foreach ($map_for_model as $calculation_index => $calculations_class) {
109 109
                 $calculator = $this->factory->createFromClassname($calculations_class);
110 110
                 $schema = call_user_func(array($calculator, 'schemaForCalculation'), $calculation_index);
111
-                if (! empty($schema)) {
112
-                    $schema_map[ $map_model ][ $calculation_index ] = $schema;
111
+                if ( ! empty($schema)) {
112
+                    $schema_map[$map_model][$calculation_index] = $schema;
113 113
                 }
114 114
             }
115 115
         }
@@ -126,8 +126,8 @@  discard block
 block discarded – undo
126 126
     public function retrieveCalculatedFieldsForModel(EEM_Base $model)
127 127
     {
128 128
         $mapping = $this->mapping();
129
-        if (isset($mapping[ $model->get_this_model_name() ])) {
130
-            return array_keys($mapping[ $model->get_this_model_name() ]);
129
+        if (isset($mapping[$model->get_this_model_name()])) {
130
+            return array_keys($mapping[$model->get_this_model_name()]);
131 131
         }
132 132
         return array();
133 133
     }
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
      */
141 141
     public function getJsonSchemaForModel(EEM_Base $model)
142 142
     {
143
-        if (! $this->mapping_schema) {
143
+        if ( ! $this->mapping_schema) {
144 144
             $this->mapping_schema = $this->generateNewMappingSchema();
145 145
         }
146 146
         return array(
@@ -149,8 +149,8 @@  discard block
 block discarded – undo
149 149
                 'event_espresso'
150 150
             ),
151 151
             'type' => 'object',
152
-            'properties' => isset($this->mapping_schema[ $model->get_this_model_name() ])
153
-                ? $this->mapping_schema[ $model->get_this_model_name() ]
152
+            'properties' => isset($this->mapping_schema[$model->get_this_model_name()])
153
+                ? $this->mapping_schema[$model->get_this_model_name()]
154 154
                 : array(),
155 155
             'additionalProperties' => false,
156 156
             'readonly' => true,
@@ -179,10 +179,10 @@  discard block
 block discarded – undo
179 179
     ) {
180 180
         $mapping = $this->mapping();
181 181
         if (
182
-            isset($mapping[ $model->get_this_model_name() ])
183
-            && isset($mapping[ $model->get_this_model_name() ][ $field_name ])
182
+            isset($mapping[$model->get_this_model_name()])
183
+            && isset($mapping[$model->get_this_model_name()][$field_name])
184 184
         ) {
185
-            $classname = $mapping[ $model->get_this_model_name() ][ $field_name ];
185
+            $classname = $mapping[$model->get_this_model_name()][$field_name];
186 186
             $calculator = $this->factory->createFromClassname($classname);
187 187
             $class_method_name = EEH_Inflector::camelize_all_but_first($field_name);
188 188
             return call_user_func(array($calculator, $class_method_name), $wpdb_row, $rest_request, $controller);
Please login to merge, or discard this patch.
Indentation   +174 added lines, -174 removed lines patch added patch discarded remove patch
@@ -22,178 +22,178 @@
 block discarded – undo
22 22
 class CalculatedModelFields
23 23
 {
24 24
 
25
-    /**
26
-     * @var array
27
-     */
28
-    protected $mapping;
29
-
30
-    /**
31
-     * @var array
32
-     */
33
-    protected $mapping_schema;
34
-
35
-    /**
36
-     * @var CalculatedModelFieldsFactory
37
-     */
38
-    private $factory;
39
-
40
-    /**
41
-     * CalculatedModelFields constructor.
42
-     * @param CalculatedModelFieldsFactory $factory
43
-     */
44
-    public function __construct(CalculatedModelFieldsFactory $factory)
45
-    {
46
-        $this->factory = $factory;
47
-    }
48
-    /**
49
-     * @param bool $refresh
50
-     * @return array top-level-keys are model names (eg "Event")
51
-     * next-level are the calculated field names AND method names on classes
52
-     * which perform calculations, values are the fully qualified classnames which do the calculations
53
-     * These callbacks should accept as arguments:
54
-     * the wpdb row results,
55
-     * the WP_Request object,
56
-     * the controller object
57
-     */
58
-    public function mapping($refresh = false)
59
-    {
60
-        if (! $this->mapping || $refresh) {
61
-            $this->mapping = $this->generateNewMapping();
62
-        }
63
-        return $this->mapping;
64
-    }
65
-
66
-
67
-    /**
68
-     * Generates a new mapping between model calculated fields and their callbacks
69
-     *
70
-     * @return array
71
-     */
72
-    protected function generateNewMapping()
73
-    {
74
-        $mapping = array();
75
-        $models_with_calculated_fields = array(
76
-            'Attendee',
77
-            'Datetime',
78
-            'Event',
79
-            'Registration'
80
-        );
81
-        foreach ($models_with_calculated_fields as $model_name) {
82
-            $calculator = $this->factory->createFromModel($model_name);
83
-            foreach (array_keys(call_user_func(array($calculator, 'schemaForCalculations'))) as $field_name) {
84
-                $mapping[ $model_name ][ $field_name ] = get_class($calculator);
85
-            }
86
-        }
87
-        return apply_filters(
88
-            'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping',
89
-            $mapping
90
-        );
91
-    }
92
-
93
-
94
-    /**
95
-     * Generates the schema for each calculation index in the calculation map.
96
-     *
97
-     * @return array
98
-     * @throws UnexpectedEntityException
99
-     */
100
-    protected function generateNewMappingSchema()
101
-    {
102
-        $schema_map = array();
103
-        foreach ($this->mapping() as $map_model => $map_for_model) {
104
-            /**
105
-             * @var string $calculation_index
106
-             * @var string $calculations_class
107
-             */
108
-            foreach ($map_for_model as $calculation_index => $calculations_class) {
109
-                $calculator = $this->factory->createFromClassname($calculations_class);
110
-                $schema = call_user_func(array($calculator, 'schemaForCalculation'), $calculation_index);
111
-                if (! empty($schema)) {
112
-                    $schema_map[ $map_model ][ $calculation_index ] = $schema;
113
-                }
114
-            }
115
-        }
116
-        return $schema_map;
117
-    }
118
-
119
-
120
-    /**
121
-     * Gets the known calculated fields for model
122
-     *
123
-     * @param EEM_Base $model
124
-     * @return array allowable values for this field
125
-     */
126
-    public function retrieveCalculatedFieldsForModel(EEM_Base $model)
127
-    {
128
-        $mapping = $this->mapping();
129
-        if (isset($mapping[ $model->get_this_model_name() ])) {
130
-            return array_keys($mapping[ $model->get_this_model_name() ]);
131
-        }
132
-        return array();
133
-    }
134
-
135
-
136
-    /**
137
-     * Returns the JsonSchema for the calculated fields on the given model.
138
-     * @param EEM_Base $model
139
-     * @return array
140
-     */
141
-    public function getJsonSchemaForModel(EEM_Base $model)
142
-    {
143
-        if (! $this->mapping_schema) {
144
-            $this->mapping_schema = $this->generateNewMappingSchema();
145
-        }
146
-        return array(
147
-            'description' => esc_html__(
148
-                'Available calculated fields for this model.  Fields are only present in the response if explicitly requested',
149
-                'event_espresso'
150
-            ),
151
-            'type' => 'object',
152
-            'properties' => isset($this->mapping_schema[ $model->get_this_model_name() ])
153
-                ? $this->mapping_schema[ $model->get_this_model_name() ]
154
-                : array(),
155
-            'additionalProperties' => false,
156
-            'readonly' => true,
157
-        );
158
-    }
159
-
160
-
161
-    /**
162
-     * Retrieves the value for this calculation
163
-     *
164
-     * @param EEM_Base $model
165
-     * @param string $field_name
166
-     * @param array $wpdb_row
167
-     * @param $rest_request
168
-     * @param BaseController $controller
169
-     * @return mixed|null
170
-     * @throws RestException
171
-     * @throws UnexpectedEntityException
172
-     */
173
-    public function retrieveCalculatedFieldValue(
174
-        EEM_Base $model,
175
-        $field_name,
176
-        $wpdb_row,
177
-        $rest_request,
178
-        Base $controller
179
-    ) {
180
-        $mapping = $this->mapping();
181
-        if (
182
-            isset($mapping[ $model->get_this_model_name() ])
183
-            && isset($mapping[ $model->get_this_model_name() ][ $field_name ])
184
-        ) {
185
-            $classname = $mapping[ $model->get_this_model_name() ][ $field_name ];
186
-            $calculator = $this->factory->createFromClassname($classname);
187
-            $class_method_name = EEH_Inflector::camelize_all_but_first($field_name);
188
-            return call_user_func(array($calculator, $class_method_name), $wpdb_row, $rest_request, $controller);
189
-        }
190
-        throw new RestException(
191
-            'calculated_field_does_not_exist',
192
-            sprintf(
193
-                esc_html__('There is no calculated field %1$s on resource %2$s', 'event_espresso'),
194
-                $field_name,
195
-                $model->get_this_model_name()
196
-            )
197
-        );
198
-    }
25
+	/**
26
+	 * @var array
27
+	 */
28
+	protected $mapping;
29
+
30
+	/**
31
+	 * @var array
32
+	 */
33
+	protected $mapping_schema;
34
+
35
+	/**
36
+	 * @var CalculatedModelFieldsFactory
37
+	 */
38
+	private $factory;
39
+
40
+	/**
41
+	 * CalculatedModelFields constructor.
42
+	 * @param CalculatedModelFieldsFactory $factory
43
+	 */
44
+	public function __construct(CalculatedModelFieldsFactory $factory)
45
+	{
46
+		$this->factory = $factory;
47
+	}
48
+	/**
49
+	 * @param bool $refresh
50
+	 * @return array top-level-keys are model names (eg "Event")
51
+	 * next-level are the calculated field names AND method names on classes
52
+	 * which perform calculations, values are the fully qualified classnames which do the calculations
53
+	 * These callbacks should accept as arguments:
54
+	 * the wpdb row results,
55
+	 * the WP_Request object,
56
+	 * the controller object
57
+	 */
58
+	public function mapping($refresh = false)
59
+	{
60
+		if (! $this->mapping || $refresh) {
61
+			$this->mapping = $this->generateNewMapping();
62
+		}
63
+		return $this->mapping;
64
+	}
65
+
66
+
67
+	/**
68
+	 * Generates a new mapping between model calculated fields and their callbacks
69
+	 *
70
+	 * @return array
71
+	 */
72
+	protected function generateNewMapping()
73
+	{
74
+		$mapping = array();
75
+		$models_with_calculated_fields = array(
76
+			'Attendee',
77
+			'Datetime',
78
+			'Event',
79
+			'Registration'
80
+		);
81
+		foreach ($models_with_calculated_fields as $model_name) {
82
+			$calculator = $this->factory->createFromModel($model_name);
83
+			foreach (array_keys(call_user_func(array($calculator, 'schemaForCalculations'))) as $field_name) {
84
+				$mapping[ $model_name ][ $field_name ] = get_class($calculator);
85
+			}
86
+		}
87
+		return apply_filters(
88
+			'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping',
89
+			$mapping
90
+		);
91
+	}
92
+
93
+
94
+	/**
95
+	 * Generates the schema for each calculation index in the calculation map.
96
+	 *
97
+	 * @return array
98
+	 * @throws UnexpectedEntityException
99
+	 */
100
+	protected function generateNewMappingSchema()
101
+	{
102
+		$schema_map = array();
103
+		foreach ($this->mapping() as $map_model => $map_for_model) {
104
+			/**
105
+			 * @var string $calculation_index
106
+			 * @var string $calculations_class
107
+			 */
108
+			foreach ($map_for_model as $calculation_index => $calculations_class) {
109
+				$calculator = $this->factory->createFromClassname($calculations_class);
110
+				$schema = call_user_func(array($calculator, 'schemaForCalculation'), $calculation_index);
111
+				if (! empty($schema)) {
112
+					$schema_map[ $map_model ][ $calculation_index ] = $schema;
113
+				}
114
+			}
115
+		}
116
+		return $schema_map;
117
+	}
118
+
119
+
120
+	/**
121
+	 * Gets the known calculated fields for model
122
+	 *
123
+	 * @param EEM_Base $model
124
+	 * @return array allowable values for this field
125
+	 */
126
+	public function retrieveCalculatedFieldsForModel(EEM_Base $model)
127
+	{
128
+		$mapping = $this->mapping();
129
+		if (isset($mapping[ $model->get_this_model_name() ])) {
130
+			return array_keys($mapping[ $model->get_this_model_name() ]);
131
+		}
132
+		return array();
133
+	}
134
+
135
+
136
+	/**
137
+	 * Returns the JsonSchema for the calculated fields on the given model.
138
+	 * @param EEM_Base $model
139
+	 * @return array
140
+	 */
141
+	public function getJsonSchemaForModel(EEM_Base $model)
142
+	{
143
+		if (! $this->mapping_schema) {
144
+			$this->mapping_schema = $this->generateNewMappingSchema();
145
+		}
146
+		return array(
147
+			'description' => esc_html__(
148
+				'Available calculated fields for this model.  Fields are only present in the response if explicitly requested',
149
+				'event_espresso'
150
+			),
151
+			'type' => 'object',
152
+			'properties' => isset($this->mapping_schema[ $model->get_this_model_name() ])
153
+				? $this->mapping_schema[ $model->get_this_model_name() ]
154
+				: array(),
155
+			'additionalProperties' => false,
156
+			'readonly' => true,
157
+		);
158
+	}
159
+
160
+
161
+	/**
162
+	 * Retrieves the value for this calculation
163
+	 *
164
+	 * @param EEM_Base $model
165
+	 * @param string $field_name
166
+	 * @param array $wpdb_row
167
+	 * @param $rest_request
168
+	 * @param BaseController $controller
169
+	 * @return mixed|null
170
+	 * @throws RestException
171
+	 * @throws UnexpectedEntityException
172
+	 */
173
+	public function retrieveCalculatedFieldValue(
174
+		EEM_Base $model,
175
+		$field_name,
176
+		$wpdb_row,
177
+		$rest_request,
178
+		Base $controller
179
+	) {
180
+		$mapping = $this->mapping();
181
+		if (
182
+			isset($mapping[ $model->get_this_model_name() ])
183
+			&& isset($mapping[ $model->get_this_model_name() ][ $field_name ])
184
+		) {
185
+			$classname = $mapping[ $model->get_this_model_name() ][ $field_name ];
186
+			$calculator = $this->factory->createFromClassname($classname);
187
+			$class_method_name = EEH_Inflector::camelize_all_but_first($field_name);
188
+			return call_user_func(array($calculator, $class_method_name), $wpdb_row, $rest_request, $controller);
189
+		}
190
+		throw new RestException(
191
+			'calculated_field_does_not_exist',
192
+			sprintf(
193
+				esc_html__('There is no calculated field %1$s on resource %2$s', 'event_espresso'),
194
+				$field_name,
195
+				$model->get_this_model_name()
196
+			)
197
+		);
198
+	}
199 199
 }
Please login to merge, or discard this patch.
core/libraries/rest_api/ModelVersionInfo.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -114,10 +114,10 @@  discard block
 block discarded – undo
114 114
         foreach ($this->resource_changes as $version => $model_classnames) {
115 115
             foreach ($model_classnames as $model_classname => $extra_fields) {
116 116
                 foreach ($extra_fields as $fieldname => $field_data) {
117
-                    $this->resource_changes[ $model_classname ][ $fieldname ]['name'] = $fieldname;
117
+                    $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname;
118 118
                     foreach ($defaults as $attribute => $default_value) {
119
-                        if (! isset($this->resource_changes[ $model_classname ][ $fieldname ][ $attribute ])) {
120
-                            $this->resource_changes[ $model_classname ][ $fieldname ][ $attribute ] = $default_value;
119
+                        if ( ! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) {
120
+                            $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value;
121 121
                         }
122 122
                     }
123 123
                 }
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
             $model_changes = array();
140 140
             foreach ($this->modelChanges() as $version => $models_changed_in_version) {
141 141
                 if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) {
142
-                    $model_changes[ $version ] = $models_changed_in_version;
142
+                    $model_changes[$version] = $models_changed_in_version;
143 143
                 }
144 144
             }
145 145
             $this->cached_model_changes_between_requested_version_and_current = $model_changes;
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
             $resource_changes = array();
162 162
             foreach ($this->resourceChanges() as $version => $model_classnames) {
163 163
                 if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) {
164
-                    $resource_changes[ $version ] = $model_classnames;
164
+                    $resource_changes[$version] = $model_classnames;
165 165
                 }
166 166
             }
167 167
             $this->cached_resource_changes_between_requested_version_and_current = $resource_changes;
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
             foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) {
213 213
                 foreach ($models_changed as $model_name => $new_indicator_or_fields_added) {
214 214
                     if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) {
215
-                        unset($all_models_in_current_version[ $model_name ]);
215
+                        unset($all_models_in_current_version[$model_name]);
216 216
                     }
217 217
                 }
218 218
             }
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
     public function isModelNameInThisVersion($model_name)
238 238
     {
239 239
         $model_names = $this->modelsForRequestedVersion();
240
-        if (isset($model_names[ $model_name ])) {
240
+        if (isset($model_names[$model_name])) {
241 241
             return true;
242 242
         } else {
243 243
             return false;
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
      */
281 281
     public function fieldsOnModelInThisVersion($model)
282 282
     {
283
-        if (! isset($this->cached_fields_on_models[ $model->get_this_model_name() ])) {
283
+        if ( ! isset($this->cached_fields_on_models[$model->get_this_model_name()])) {
284 284
             // get all model changes between the requested version and current core version
285 285
             $changes = $this->modelChangesBetweenRequestedVersionAndCurrent();
286 286
             // fetch all fields currently on this model
@@ -288,12 +288,12 @@  discard block
 block discarded – undo
288 288
             // remove all fields that have been added since
289 289
             foreach ($changes as $version => $changes_in_version) {
290 290
                 if (
291
-                    isset($changes_in_version[ $model->get_this_model_name() ])
292
-                    && $changes_in_version[ $model->get_this_model_name() ] !== ModelVersionInfo::MODEL_ADDED
291
+                    isset($changes_in_version[$model->get_this_model_name()])
292
+                    && $changes_in_version[$model->get_this_model_name()] !== ModelVersionInfo::MODEL_ADDED
293 293
                 ) {
294 294
                     $current_fields = array_diff_key(
295 295
                         $current_fields,
296
-                        array_flip($changes_in_version[ $model->get_this_model_name() ])
296
+                        array_flip($changes_in_version[$model->get_this_model_name()])
297 297
                     );
298 298
                 }
299 299
             }
@@ -448,7 +448,7 @@  discard block
 block discarded – undo
448 448
         $relations = array();
449 449
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
450 450
             if ($this->isModelNameInThisVersion($relation_name)) {
451
-                $relations[ $relation_name ] = $relation_obj;
451
+                $relations[$relation_name] = $relation_obj;
452 452
             }
453 453
         }
454 454
         // filter the results, but use the old filter name
Please login to merge, or discard this patch.
Indentation   +437 added lines, -437 removed lines patch added patch discarded remove patch
@@ -21,441 +21,441 @@
 block discarded – undo
21 21
 class ModelVersionInfo
22 22
 {
23 23
 
24
-    /**
25
-     * Constant used in the $_model_changes array to indicate that a model
26
-     * was completely new in this version
27
-     */
28
-    const MODEL_ADDED = 'model_added_in_this_version';
29
-
30
-    /**
31
-     * Top-level keys are versions (major and minor version numbers, eg "4.6")
32
-     * next-level keys are model names (eg "Event") that underwent some change in that version
33
-     * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version),
34
-     * or it's an array where the values are model field names,
35
-     * or API resource properties (ie, non-model fields that appear in REST API results)
36
-     * If a version is missing then we don't know anything about what changes it introduced from the previous version
37
-     *
38
-     * @var array
39
-     */
40
-    protected $model_changes = array();
41
-
42
-    /**
43
-     * top-level keys are version numbers,
44
-     * next-level keys are model CLASSNAMES (even parent classnames),
45
-     * and next-level keys are extra resource properties to attach to those models' resources,
46
-     * and next-level key-value pairs, where the keys are:
47
-     * 'raw', 'type', 'nullable', 'table_alias', 'table_column',  'always_available'
48
-     *
49
-     * @var array
50
-     */
51
-    protected $resource_changes = array();
52
-
53
-    /**
54
-     * @var string indicating what version of the API was requested
55
-     * (eg although core might be at version 4.8.11, they may have sent a request
56
-     * for 4.6)
57
-     */
58
-    protected $requested_version = null;
59
-
60
-    /**
61
-     * Keys are model names, values are their classnames.
62
-     * We cache this so we only need to calculate this once per request
63
-     *
64
-     * @var array
65
-     */
66
-    protected $cached_models_for_requested_version = null;
67
-
68
-    /**
69
-     * @var array
70
-     */
71
-    protected $cached_model_changes_between_requested_version_and_current = null;
72
-
73
-    /**
74
-     * @var array
75
-     */
76
-    protected $cached_resource_changes_between_requested_version_and_current = null;
77
-
78
-    /**
79
-     * 2d array where top-level keys are model names, 2nd-level keys are field names
80
-     * and values are the actual field objects
81
-     *
82
-     * @var array
83
-     */
84
-    protected $cached_fields_on_models = array();
85
-
86
-
87
-    /**
88
-     * Model_Version_Info constructor.
89
-     *
90
-     * @param string $requested_version
91
-     */
92
-    public function __construct($requested_version)
93
-    {
94
-        $this->requested_version = (string) $requested_version;
95
-        $this->model_changes = array(
96
-            '4.8.29' => array(
97
-                // first version where the REST API is in EE core, so no need
98
-                // to specify how its different from the previous
99
-            ),
100
-        );
101
-        // setup data for "extra" fields added onto resources which don't actually exist on models
102
-        $this->resource_changes = apply_filters(
103
-            'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models',
104
-            array()
105
-        );
106
-        $defaults = array(
107
-            'raw'              => false,
108
-            'type'             => 'N/A',
109
-            'nullable'         => true,
110
-            'table_alias'      => 'N/A',
111
-            'table_column'     => 'N/A',
112
-            'always_available' => true,
113
-        );
114
-        foreach ($this->resource_changes as $version => $model_classnames) {
115
-            foreach ($model_classnames as $model_classname => $extra_fields) {
116
-                foreach ($extra_fields as $fieldname => $field_data) {
117
-                    $this->resource_changes[ $model_classname ][ $fieldname ]['name'] = $fieldname;
118
-                    foreach ($defaults as $attribute => $default_value) {
119
-                        if (! isset($this->resource_changes[ $model_classname ][ $fieldname ][ $attribute ])) {
120
-                            $this->resource_changes[ $model_classname ][ $fieldname ][ $attribute ] = $default_value;
121
-                        }
122
-                    }
123
-                }
124
-            }
125
-        }
126
-    }
127
-
128
-
129
-    /**
130
-     * Returns a slice of Model_Version_Info::model_changes()'s array
131
-     * indicating exactly what changes happened between the current core version,
132
-     * and the version requested
133
-     *
134
-     * @return array
135
-     */
136
-    public function modelChangesBetweenRequestedVersionAndCurrent()
137
-    {
138
-        if ($this->cached_model_changes_between_requested_version_and_current === null) {
139
-            $model_changes = array();
140
-            foreach ($this->modelChanges() as $version => $models_changed_in_version) {
141
-                if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) {
142
-                    $model_changes[ $version ] = $models_changed_in_version;
143
-                }
144
-            }
145
-            $this->cached_model_changes_between_requested_version_and_current = $model_changes;
146
-        }
147
-        return $this->cached_model_changes_between_requested_version_and_current;
148
-    }
149
-
150
-
151
-    /**
152
-     * Returns a slice of Model_Version_Info::model_changes()'s array
153
-     * indicating exactly what changes happened between the current core version,
154
-     * and the version requested
155
-     *
156
-     * @return array
157
-     */
158
-    public function resourceChangesBetweenRequestedVersionAndCurrent()
159
-    {
160
-        if ($this->cached_resource_changes_between_requested_version_and_current === null) {
161
-            $resource_changes = array();
162
-            foreach ($this->resourceChanges() as $version => $model_classnames) {
163
-                if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) {
164
-                    $resource_changes[ $version ] = $model_classnames;
165
-                }
166
-            }
167
-            $this->cached_resource_changes_between_requested_version_and_current = $resource_changes;
168
-        }
169
-        return $this->cached_resource_changes_between_requested_version_and_current;
170
-    }
171
-
172
-
173
-    /**
174
-     * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7'
175
-     *
176
-     * @return string like '4.6'
177
-     */
178
-    public function requestedVersion()
179
-    {
180
-        return $this->requested_version;
181
-    }
182
-
183
-
184
-    /**
185
-     * Returns an array describing how the models have changed in each version of core
186
-     * that supports the API (starting at 4.6)
187
-     * Top-level keys are versions (major and minor version numbers, eg "4.6")
188
-     * next-level keys are model names (eg "Event") that underwent some change in that version
189
-     * and the value is either NULL (indicating the model is completely NEW in this version),
190
-     * or it's an array where fields are value names.
191
-     * If a version is missing then we don't know anything about what changes it introduced from the previous version
192
-     *
193
-     * @return array
194
-     */
195
-    public function modelChanges()
196
-    {
197
-        return $this->model_changes;
198
-    }
199
-
200
-
201
-    /**
202
-     * Takes into account the requested version, and the current version, and
203
-     * what changed between the two, and tries to return.
204
-     * Analogous to EE_Registry::instance()->non_abstract_db_models
205
-     *
206
-     * @return array keys are model names, values are their classname
207
-     */
208
-    public function modelsForRequestedVersion()
209
-    {
210
-        if ($this->cached_models_for_requested_version === null) {
211
-            $all_models_in_current_version = EE_Registry::instance()->non_abstract_db_models;
212
-            foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) {
213
-                foreach ($models_changed as $model_name => $new_indicator_or_fields_added) {
214
-                    if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) {
215
-                        unset($all_models_in_current_version[ $model_name ]);
216
-                    }
217
-                }
218
-            }
219
-            $this->cached_models_for_requested_version = apply_filters(
220
-                'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version',
221
-                $all_models_in_current_version,
222
-                $this
223
-            );
224
-        }
225
-        return $this->cached_models_for_requested_version;
226
-    }
227
-
228
-
229
-    /**
230
-     * Determines if this is a valid model name in the requested version.
231
-     * Similar to EE_Registry::instance()->is_model_name(), but takes the requested
232
-     * version's models into account
233
-     *
234
-     * @param string $model_name eg 'Event'
235
-     * @return boolean
236
-     */
237
-    public function isModelNameInThisVersion($model_name)
238
-    {
239
-        $model_names = $this->modelsForRequestedVersion();
240
-        if (isset($model_names[ $model_name ])) {
241
-            return true;
242
-        } else {
243
-            return false;
244
-        }
245
-    }
246
-
247
-
248
-    /**
249
-     * Wrapper for EE_Registry::instance()->load_model(), but takes the requested
250
-     * version's models into account
251
-     *
252
-     * @param string $model_name
253
-     * @return \EEM_Base
254
-     * @throws \EE_Error
255
-     */
256
-    public function loadModel($model_name)
257
-    {
258
-        if ($this->isModelNameInThisVersion($model_name)) {
259
-            return EE_Registry::instance()->load_model($model_name);
260
-        } else {
261
-            throw new \EE_Error(
262
-                sprintf(
263
-                    esc_html__(
264
-                        'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso',
265
-                        'event_espresso'
266
-                    ),
267
-                    $model_name,
268
-                    $this->requestedVersion()
269
-                )
270
-            );
271
-        }
272
-    }
273
-
274
-
275
-    /**
276
-     * Gets all the fields that should exist on this model right now
277
-     *
278
-     * @param \EEM_Base $model
279
-     * @return array|\EE_Model_Field_Base[]
280
-     */
281
-    public function fieldsOnModelInThisVersion($model)
282
-    {
283
-        if (! isset($this->cached_fields_on_models[ $model->get_this_model_name() ])) {
284
-            // get all model changes between the requested version and current core version
285
-            $changes = $this->modelChangesBetweenRequestedVersionAndCurrent();
286
-            // fetch all fields currently on this model
287
-            $current_fields = $model->field_settings();
288
-            // remove all fields that have been added since
289
-            foreach ($changes as $version => $changes_in_version) {
290
-                if (
291
-                    isset($changes_in_version[ $model->get_this_model_name() ])
292
-                    && $changes_in_version[ $model->get_this_model_name() ] !== ModelVersionInfo::MODEL_ADDED
293
-                ) {
294
-                    $current_fields = array_diff_key(
295
-                        $current_fields,
296
-                        array_flip($changes_in_version[ $model->get_this_model_name() ])
297
-                    );
298
-                }
299
-            }
300
-            $this->cached_fields_on_models = $current_fields;
301
-        }
302
-        return $this->cached_fields_on_models;
303
-    }
304
-
305
-
306
-    /**
307
-     * Determines if $object is of one of the classes of $classes. Similar to
308
-     * in_array(), except this checks if $object is a subclass of the classnames provided
309
-     * in $classnames
310
-     *
311
-     * @param object $object
312
-     * @param array  $classnames
313
-     * @return boolean
314
-     */
315
-    public function isSubclassOfOne($object, $classnames)
316
-    {
317
-        foreach ($classnames as $classname) {
318
-            if (is_a($object, $classname)) {
319
-                return true;
320
-            }
321
-        }
322
-        return false;
323
-    }
324
-
325
-
326
-    /**
327
-     * Returns the list of model field classes that that the API basically ignores
328
-     *
329
-     * @return array
330
-     */
331
-    public function fieldsIgnored()
332
-    {
333
-        return apply_filters(
334
-            'FHEE__Controller_Model_Read_fields_ignored',
335
-            array()
336
-        );
337
-    }
338
-
339
-
340
-    /**
341
-     * If this field one that should be ignored by the API?
342
-     *
343
-     * @param EE_Model_Field_Base
344
-     * @return boolean
345
-     */
346
-    public function fieldIsIgnored($field_obj)
347
-    {
348
-        return $this->isSubclassOfOne($field_obj, $this->fieldsIgnored());
349
-    }
350
-
351
-
352
-    /**
353
-     * Returns the list of model field classes that have a "raw" and non-raw formats.
354
-     * Normally the "raw" versions are only accessible to those who can edit them.
355
-     *
356
-     * @return array an array of EE_Model_Field_Base child classnames
357
-     */
358
-    public function fieldsThatHaveRenderedFormat()
359
-    {
360
-        return apply_filters(
361
-            'FHEE__Controller_Model_Read__fields_raw',
362
-            array('EE_Post_Content_Field', 'EE_Full_HTML_Field')
363
-        );
364
-    }
365
-
366
-
367
-    /**
368
-     * If this field one that has a raw format
369
-     *
370
-     * @param EE_Model_Field_Base
371
-     * @return boolean
372
-     */
373
-    public function fieldHasRenderedFormat($field_obj)
374
-    {
375
-        return $this->isSubclassOfOne($field_obj, $this->fieldsThatHaveRenderedFormat());
376
-    }
377
-
378
-
379
-    /**
380
-     * Returns the list of model field classes that have a "_pretty" and non-pretty versions.
381
-     * The pretty version of the field is NOT query-able or editable, but requires no extra permissions
382
-     * to view
383
-     *
384
-     * @return array an array of EE_Model_Field_Base child classnames
385
-     */
386
-    public function fieldsThatHavePrettyFormat()
387
-    {
388
-        return apply_filters(
389
-            'FHEE__Controller_Model_Read__fields_pretty',
390
-            array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field')
391
-        );
392
-    }
393
-
394
-
395
-    /**
396
-     * If this field one that has a pretty equivalent
397
-     *
398
-     * @param EE_Model_Field_Base
399
-     * @return boolean
400
-     */
401
-    public function fieldHasPrettyFormat($field_obj)
402
-    {
403
-        return $this->isSubclassOfOne($field_obj, $this->fieldsThatHavePrettyFormat());
404
-    }
405
-
406
-
407
-    /**
408
-     * Returns an array describing what extra API resource properties have been added through the versions
409
-     *
410
-     * @return array @see $this->_extra_resource_properties_for_models
411
-     */
412
-    public function resourceChanges()
413
-    {
414
-        return $this->resource_changes;
415
-    }
416
-
417
-
418
-    /**
419
-     * Returns an array where keys are extra resource properties in this version of the API,
420
-     * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes
421
-     *
422
-     * @param \EEM_Base $model
423
-     * @return array
424
-     */
425
-    public function extraResourcePropertiesForModel($model)
426
-    {
427
-        $extra_properties = array();
428
-        foreach ($this->resourceChangesBetweenRequestedVersionAndCurrent() as $version => $model_classnames) {
429
-            foreach ($model_classnames as $model_classname => $properties_added_in_this_version) {
430
-                if (is_subclass_of($model, $model_classname)) {
431
-                    $extra_properties = array_merge($extra_properties, $properties_added_in_this_version);
432
-                }
433
-            }
434
-        }
435
-        return $extra_properties;
436
-    }
437
-
438
-
439
-    /**
440
-     * Gets all the related models for the specified model. It's good to use this
441
-     * in case this model didn't exist for this version or something
442
-     *
443
-     * @param \EEM_Base $model
444
-     * @return \EE_Model_Relation_Base[]
445
-     */
446
-    public function relationSettings(\EEM_Base $model)
447
-    {
448
-        $relations = array();
449
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
450
-            if ($this->isModelNameInThisVersion($relation_name)) {
451
-                $relations[ $relation_name ] = $relation_obj;
452
-            }
453
-        }
454
-        // filter the results, but use the old filter name
455
-        return apply_filters(
456
-            'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include',
457
-            $relations,
458
-            $model
459
-        );
460
-    }
24
+	/**
25
+	 * Constant used in the $_model_changes array to indicate that a model
26
+	 * was completely new in this version
27
+	 */
28
+	const MODEL_ADDED = 'model_added_in_this_version';
29
+
30
+	/**
31
+	 * Top-level keys are versions (major and minor version numbers, eg "4.6")
32
+	 * next-level keys are model names (eg "Event") that underwent some change in that version
33
+	 * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version),
34
+	 * or it's an array where the values are model field names,
35
+	 * or API resource properties (ie, non-model fields that appear in REST API results)
36
+	 * If a version is missing then we don't know anything about what changes it introduced from the previous version
37
+	 *
38
+	 * @var array
39
+	 */
40
+	protected $model_changes = array();
41
+
42
+	/**
43
+	 * top-level keys are version numbers,
44
+	 * next-level keys are model CLASSNAMES (even parent classnames),
45
+	 * and next-level keys are extra resource properties to attach to those models' resources,
46
+	 * and next-level key-value pairs, where the keys are:
47
+	 * 'raw', 'type', 'nullable', 'table_alias', 'table_column',  'always_available'
48
+	 *
49
+	 * @var array
50
+	 */
51
+	protected $resource_changes = array();
52
+
53
+	/**
54
+	 * @var string indicating what version of the API was requested
55
+	 * (eg although core might be at version 4.8.11, they may have sent a request
56
+	 * for 4.6)
57
+	 */
58
+	protected $requested_version = null;
59
+
60
+	/**
61
+	 * Keys are model names, values are their classnames.
62
+	 * We cache this so we only need to calculate this once per request
63
+	 *
64
+	 * @var array
65
+	 */
66
+	protected $cached_models_for_requested_version = null;
67
+
68
+	/**
69
+	 * @var array
70
+	 */
71
+	protected $cached_model_changes_between_requested_version_and_current = null;
72
+
73
+	/**
74
+	 * @var array
75
+	 */
76
+	protected $cached_resource_changes_between_requested_version_and_current = null;
77
+
78
+	/**
79
+	 * 2d array where top-level keys are model names, 2nd-level keys are field names
80
+	 * and values are the actual field objects
81
+	 *
82
+	 * @var array
83
+	 */
84
+	protected $cached_fields_on_models = array();
85
+
86
+
87
+	/**
88
+	 * Model_Version_Info constructor.
89
+	 *
90
+	 * @param string $requested_version
91
+	 */
92
+	public function __construct($requested_version)
93
+	{
94
+		$this->requested_version = (string) $requested_version;
95
+		$this->model_changes = array(
96
+			'4.8.29' => array(
97
+				// first version where the REST API is in EE core, so no need
98
+				// to specify how its different from the previous
99
+			),
100
+		);
101
+		// setup data for "extra" fields added onto resources which don't actually exist on models
102
+		$this->resource_changes = apply_filters(
103
+			'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models',
104
+			array()
105
+		);
106
+		$defaults = array(
107
+			'raw'              => false,
108
+			'type'             => 'N/A',
109
+			'nullable'         => true,
110
+			'table_alias'      => 'N/A',
111
+			'table_column'     => 'N/A',
112
+			'always_available' => true,
113
+		);
114
+		foreach ($this->resource_changes as $version => $model_classnames) {
115
+			foreach ($model_classnames as $model_classname => $extra_fields) {
116
+				foreach ($extra_fields as $fieldname => $field_data) {
117
+					$this->resource_changes[ $model_classname ][ $fieldname ]['name'] = $fieldname;
118
+					foreach ($defaults as $attribute => $default_value) {
119
+						if (! isset($this->resource_changes[ $model_classname ][ $fieldname ][ $attribute ])) {
120
+							$this->resource_changes[ $model_classname ][ $fieldname ][ $attribute ] = $default_value;
121
+						}
122
+					}
123
+				}
124
+			}
125
+		}
126
+	}
127
+
128
+
129
+	/**
130
+	 * Returns a slice of Model_Version_Info::model_changes()'s array
131
+	 * indicating exactly what changes happened between the current core version,
132
+	 * and the version requested
133
+	 *
134
+	 * @return array
135
+	 */
136
+	public function modelChangesBetweenRequestedVersionAndCurrent()
137
+	{
138
+		if ($this->cached_model_changes_between_requested_version_and_current === null) {
139
+			$model_changes = array();
140
+			foreach ($this->modelChanges() as $version => $models_changed_in_version) {
141
+				if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) {
142
+					$model_changes[ $version ] = $models_changed_in_version;
143
+				}
144
+			}
145
+			$this->cached_model_changes_between_requested_version_and_current = $model_changes;
146
+		}
147
+		return $this->cached_model_changes_between_requested_version_and_current;
148
+	}
149
+
150
+
151
+	/**
152
+	 * Returns a slice of Model_Version_Info::model_changes()'s array
153
+	 * indicating exactly what changes happened between the current core version,
154
+	 * and the version requested
155
+	 *
156
+	 * @return array
157
+	 */
158
+	public function resourceChangesBetweenRequestedVersionAndCurrent()
159
+	{
160
+		if ($this->cached_resource_changes_between_requested_version_and_current === null) {
161
+			$resource_changes = array();
162
+			foreach ($this->resourceChanges() as $version => $model_classnames) {
163
+				if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) {
164
+					$resource_changes[ $version ] = $model_classnames;
165
+				}
166
+			}
167
+			$this->cached_resource_changes_between_requested_version_and_current = $resource_changes;
168
+		}
169
+		return $this->cached_resource_changes_between_requested_version_and_current;
170
+	}
171
+
172
+
173
+	/**
174
+	 * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7'
175
+	 *
176
+	 * @return string like '4.6'
177
+	 */
178
+	public function requestedVersion()
179
+	{
180
+		return $this->requested_version;
181
+	}
182
+
183
+
184
+	/**
185
+	 * Returns an array describing how the models have changed in each version of core
186
+	 * that supports the API (starting at 4.6)
187
+	 * Top-level keys are versions (major and minor version numbers, eg "4.6")
188
+	 * next-level keys are model names (eg "Event") that underwent some change in that version
189
+	 * and the value is either NULL (indicating the model is completely NEW in this version),
190
+	 * or it's an array where fields are value names.
191
+	 * If a version is missing then we don't know anything about what changes it introduced from the previous version
192
+	 *
193
+	 * @return array
194
+	 */
195
+	public function modelChanges()
196
+	{
197
+		return $this->model_changes;
198
+	}
199
+
200
+
201
+	/**
202
+	 * Takes into account the requested version, and the current version, and
203
+	 * what changed between the two, and tries to return.
204
+	 * Analogous to EE_Registry::instance()->non_abstract_db_models
205
+	 *
206
+	 * @return array keys are model names, values are their classname
207
+	 */
208
+	public function modelsForRequestedVersion()
209
+	{
210
+		if ($this->cached_models_for_requested_version === null) {
211
+			$all_models_in_current_version = EE_Registry::instance()->non_abstract_db_models;
212
+			foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) {
213
+				foreach ($models_changed as $model_name => $new_indicator_or_fields_added) {
214
+					if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) {
215
+						unset($all_models_in_current_version[ $model_name ]);
216
+					}
217
+				}
218
+			}
219
+			$this->cached_models_for_requested_version = apply_filters(
220
+				'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version',
221
+				$all_models_in_current_version,
222
+				$this
223
+			);
224
+		}
225
+		return $this->cached_models_for_requested_version;
226
+	}
227
+
228
+
229
+	/**
230
+	 * Determines if this is a valid model name in the requested version.
231
+	 * Similar to EE_Registry::instance()->is_model_name(), but takes the requested
232
+	 * version's models into account
233
+	 *
234
+	 * @param string $model_name eg 'Event'
235
+	 * @return boolean
236
+	 */
237
+	public function isModelNameInThisVersion($model_name)
238
+	{
239
+		$model_names = $this->modelsForRequestedVersion();
240
+		if (isset($model_names[ $model_name ])) {
241
+			return true;
242
+		} else {
243
+			return false;
244
+		}
245
+	}
246
+
247
+
248
+	/**
249
+	 * Wrapper for EE_Registry::instance()->load_model(), but takes the requested
250
+	 * version's models into account
251
+	 *
252
+	 * @param string $model_name
253
+	 * @return \EEM_Base
254
+	 * @throws \EE_Error
255
+	 */
256
+	public function loadModel($model_name)
257
+	{
258
+		if ($this->isModelNameInThisVersion($model_name)) {
259
+			return EE_Registry::instance()->load_model($model_name);
260
+		} else {
261
+			throw new \EE_Error(
262
+				sprintf(
263
+					esc_html__(
264
+						'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso',
265
+						'event_espresso'
266
+					),
267
+					$model_name,
268
+					$this->requestedVersion()
269
+				)
270
+			);
271
+		}
272
+	}
273
+
274
+
275
+	/**
276
+	 * Gets all the fields that should exist on this model right now
277
+	 *
278
+	 * @param \EEM_Base $model
279
+	 * @return array|\EE_Model_Field_Base[]
280
+	 */
281
+	public function fieldsOnModelInThisVersion($model)
282
+	{
283
+		if (! isset($this->cached_fields_on_models[ $model->get_this_model_name() ])) {
284
+			// get all model changes between the requested version and current core version
285
+			$changes = $this->modelChangesBetweenRequestedVersionAndCurrent();
286
+			// fetch all fields currently on this model
287
+			$current_fields = $model->field_settings();
288
+			// remove all fields that have been added since
289
+			foreach ($changes as $version => $changes_in_version) {
290
+				if (
291
+					isset($changes_in_version[ $model->get_this_model_name() ])
292
+					&& $changes_in_version[ $model->get_this_model_name() ] !== ModelVersionInfo::MODEL_ADDED
293
+				) {
294
+					$current_fields = array_diff_key(
295
+						$current_fields,
296
+						array_flip($changes_in_version[ $model->get_this_model_name() ])
297
+					);
298
+				}
299
+			}
300
+			$this->cached_fields_on_models = $current_fields;
301
+		}
302
+		return $this->cached_fields_on_models;
303
+	}
304
+
305
+
306
+	/**
307
+	 * Determines if $object is of one of the classes of $classes. Similar to
308
+	 * in_array(), except this checks if $object is a subclass of the classnames provided
309
+	 * in $classnames
310
+	 *
311
+	 * @param object $object
312
+	 * @param array  $classnames
313
+	 * @return boolean
314
+	 */
315
+	public function isSubclassOfOne($object, $classnames)
316
+	{
317
+		foreach ($classnames as $classname) {
318
+			if (is_a($object, $classname)) {
319
+				return true;
320
+			}
321
+		}
322
+		return false;
323
+	}
324
+
325
+
326
+	/**
327
+	 * Returns the list of model field classes that that the API basically ignores
328
+	 *
329
+	 * @return array
330
+	 */
331
+	public function fieldsIgnored()
332
+	{
333
+		return apply_filters(
334
+			'FHEE__Controller_Model_Read_fields_ignored',
335
+			array()
336
+		);
337
+	}
338
+
339
+
340
+	/**
341
+	 * If this field one that should be ignored by the API?
342
+	 *
343
+	 * @param EE_Model_Field_Base
344
+	 * @return boolean
345
+	 */
346
+	public function fieldIsIgnored($field_obj)
347
+	{
348
+		return $this->isSubclassOfOne($field_obj, $this->fieldsIgnored());
349
+	}
350
+
351
+
352
+	/**
353
+	 * Returns the list of model field classes that have a "raw" and non-raw formats.
354
+	 * Normally the "raw" versions are only accessible to those who can edit them.
355
+	 *
356
+	 * @return array an array of EE_Model_Field_Base child classnames
357
+	 */
358
+	public function fieldsThatHaveRenderedFormat()
359
+	{
360
+		return apply_filters(
361
+			'FHEE__Controller_Model_Read__fields_raw',
362
+			array('EE_Post_Content_Field', 'EE_Full_HTML_Field')
363
+		);
364
+	}
365
+
366
+
367
+	/**
368
+	 * If this field one that has a raw format
369
+	 *
370
+	 * @param EE_Model_Field_Base
371
+	 * @return boolean
372
+	 */
373
+	public function fieldHasRenderedFormat($field_obj)
374
+	{
375
+		return $this->isSubclassOfOne($field_obj, $this->fieldsThatHaveRenderedFormat());
376
+	}
377
+
378
+
379
+	/**
380
+	 * Returns the list of model field classes that have a "_pretty" and non-pretty versions.
381
+	 * The pretty version of the field is NOT query-able or editable, but requires no extra permissions
382
+	 * to view
383
+	 *
384
+	 * @return array an array of EE_Model_Field_Base child classnames
385
+	 */
386
+	public function fieldsThatHavePrettyFormat()
387
+	{
388
+		return apply_filters(
389
+			'FHEE__Controller_Model_Read__fields_pretty',
390
+			array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field')
391
+		);
392
+	}
393
+
394
+
395
+	/**
396
+	 * If this field one that has a pretty equivalent
397
+	 *
398
+	 * @param EE_Model_Field_Base
399
+	 * @return boolean
400
+	 */
401
+	public function fieldHasPrettyFormat($field_obj)
402
+	{
403
+		return $this->isSubclassOfOne($field_obj, $this->fieldsThatHavePrettyFormat());
404
+	}
405
+
406
+
407
+	/**
408
+	 * Returns an array describing what extra API resource properties have been added through the versions
409
+	 *
410
+	 * @return array @see $this->_extra_resource_properties_for_models
411
+	 */
412
+	public function resourceChanges()
413
+	{
414
+		return $this->resource_changes;
415
+	}
416
+
417
+
418
+	/**
419
+	 * Returns an array where keys are extra resource properties in this version of the API,
420
+	 * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes
421
+	 *
422
+	 * @param \EEM_Base $model
423
+	 * @return array
424
+	 */
425
+	public function extraResourcePropertiesForModel($model)
426
+	{
427
+		$extra_properties = array();
428
+		foreach ($this->resourceChangesBetweenRequestedVersionAndCurrent() as $version => $model_classnames) {
429
+			foreach ($model_classnames as $model_classname => $properties_added_in_this_version) {
430
+				if (is_subclass_of($model, $model_classname)) {
431
+					$extra_properties = array_merge($extra_properties, $properties_added_in_this_version);
432
+				}
433
+			}
434
+		}
435
+		return $extra_properties;
436
+	}
437
+
438
+
439
+	/**
440
+	 * Gets all the related models for the specified model. It's good to use this
441
+	 * in case this model didn't exist for this version or something
442
+	 *
443
+	 * @param \EEM_Base $model
444
+	 * @return \EE_Model_Relation_Base[]
445
+	 */
446
+	public function relationSettings(\EEM_Base $model)
447
+	{
448
+		$relations = array();
449
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
450
+			if ($this->isModelNameInThisVersion($relation_name)) {
451
+				$relations[ $relation_name ] = $relation_obj;
452
+			}
453
+		}
454
+		// filter the results, but use the old filter name
455
+		return apply_filters(
456
+			'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include',
457
+			$relations,
458
+			$model
459
+		);
460
+	}
461 461
 }
Please login to merge, or discard this patch.
core/libraries/messages/EE_Messages_Base.lib.php 2 patches
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -196,11 +196,11 @@  discard block
 block discarded – undo
196 196
             ! empty($messenger)
197 197
             && $Message_Resource_Manager->is_message_type_active_for_messenger($messenger, $this->name)
198 198
         ) {
199
-            $settings_to_use = $active_messengers[ $messenger ]['settings'][ $messenger . '-message_types' ];
199
+            $settings_to_use = $active_messengers[$messenger]['settings'][$messenger.'-message_types'];
200 200
         }
201 201
 
202
-        $this->_existing_admin_settings = isset($settings_to_use[ $this->name ]['settings'])
203
-            ? $settings_to_use[ $this->name ]['settings']
202
+        $this->_existing_admin_settings = isset($settings_to_use[$this->name]['settings'])
203
+            ? $settings_to_use[$this->name]['settings']
204 204
             : null;
205 205
     }
206 206
 
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
     public function get_valid_shortcodes()
241 241
     {
242 242
         $valid_shortcodes = apply_filters(
243
-            'FHEE__' . get_class($this) . '__get_valid_shortcodes',
243
+            'FHEE__'.get_class($this).'__get_valid_shortcodes',
244 244
             $this->_valid_shortcodes,
245 245
             $this
246 246
         );
@@ -283,19 +283,19 @@  discard block
 block discarded – undo
283 283
     protected function _get_admin_page_content($page, $action, $extra, $actives)
284 284
     {
285 285
         // we can also further refine the context by action (if present).
286
-        if (!empty($action)) {
287
-            $page = $page . '_' . $action;
286
+        if ( ! empty($action)) {
287
+            $page = $page.'_'.$action;
288 288
         }
289 289
 
290
-        if (!isset($this->admin_registered_pages[ $page ])) {
290
+        if ( ! isset($this->admin_registered_pages[$page])) {
291 291
             // todo: a place to throw an exception?
292 292
             // We need to indicate there is no registered page so this function is not being called correctly.
293 293
             return false;
294 294
         }
295 295
         // k made it here so let's call the method
296 296
         $content = call_user_func_array(
297
-            array( $this, '_get_admin_content_' . $page ),
298
-            array( $actives, $extra )
297
+            array($this, '_get_admin_content_'.$page),
298
+            array($actives, $extra)
299 299
         );
300 300
         if ($content === false) {
301 301
             // todo this needs to be an exception once we've got exceptions in place.
Please login to merge, or discard this patch.
Indentation   +265 added lines, -265 removed lines patch added patch discarded remove patch
@@ -14,44 +14,44 @@  discard block
 block discarded – undo
14 14
 abstract class EE_Messages_Base extends EE_Base
15 15
 {
16 16
 
17
-    /** DETAILS PROPERTIES **/
18
-    /**
19
-     * The following are used to hold details on the type for reference (i.e. on admin screens)
20
-     * and also used by the EE_message_type object to figure out where to get template data.
21
-     */
22
-    public $name;
23
-    public $description;
24
-    protected $_messages_item_type; // messenger OR message_type?
25
-
26
-
27
-    /**
28
-     * This is an array describing the ui facing labels
29
-     * that will be used whenever the messenger is referenced in the ui
30
-     *
31
-     * array(
32
-     *  'singular' => esc_html__('something'),
33
-     *  'plural' => esc_html__('somethings')
34
-     * )
35
-     *
36
-     * @var array
37
-     */
38
-    public $label;
39
-
40
-
41
-
42
-
43
-    /**
44
-     * This property when set will hold the slugs of all EE admin pages that we will need to retrieve fields for
45
-     * (and used to determine which callback method to call from the child class)
46
-     *
47
-     * structure should be
48
-     * array(
49
-     * 'page_action' => true
50
-     * )
51
-     *
17
+	/** DETAILS PROPERTIES **/
18
+	/**
19
+	 * The following are used to hold details on the type for reference (i.e. on admin screens)
20
+	 * and also used by the EE_message_type object to figure out where to get template data.
21
+	 */
22
+	public $name;
23
+	public $description;
24
+	protected $_messages_item_type; // messenger OR message_type?
25
+
26
+
27
+	/**
28
+	 * This is an array describing the ui facing labels
29
+	 * that will be used whenever the messenger is referenced in the ui
30
+	 *
31
+	 * array(
32
+	 *  'singular' => esc_html__('something'),
33
+	 *  'plural' => esc_html__('somethings')
34
+	 * )
35
+	 *
36
+	 * @var array
37
+	 */
38
+	public $label;
39
+
40
+
41
+
42
+
43
+	/**
44
+	 * This property when set will hold the slugs of all EE admin pages that we will need to retrieve fields for
45
+	 * (and used to determine which callback method to call from the child class)
46
+	 *
47
+	 * structure should be
48
+	 * array(
49
+	 * 'page_action' => true
50
+	 * )
51
+	 *
52 52
 *@var array
53
-     */
54
-    public $admin_registered_pages = array();
53
+	 */
54
+	public $admin_registered_pages = array();
55 55
 
56 56
 
57 57
 
@@ -60,265 +60,265 @@  discard block
 block discarded – undo
60 60
 
61 61
 
62 62
 
63
-    /**
64
-     * this property holds any specific fields for holding any settings related to a messenger (if any needed)
65
-     * @var array
66
-     */
67
-    protected $_admin_settings_fields = array();
63
+	/**
64
+	 * this property holds any specific fields for holding any settings related to a messenger (if any needed)
65
+	 * @var array
66
+	 */
67
+	protected $_admin_settings_fields = array();
68 68
 
69 69
 
70 70
 
71 71
 
72 72
 
73
-    /**
74
-     * this property will hold any existing settings that may have been set in the admin.
75
-     * @var array
76
-     */
77
-    protected $_existing_admin_settings = array();
73
+	/**
74
+	 * this property will hold any existing settings that may have been set in the admin.
75
+	 * @var array
76
+	 */
77
+	protected $_existing_admin_settings = array();
78 78
 
79 79
 
80 80
 
81 81
 
82 82
 
83
-    /**
84
-     * this property will hold an array of valid shortcodes for this message type and messengers.
85
-     * #For Message Types:
86
-     * This is an array of strings that correspond to defined EE_Shortcode libraries and per context.
87
-     * For example:
88
-     * array( 'admin' => array('transaction', 'event', 'attendee') )
89
-     * corresponds to 'EE_Transaction_Shortcodes.lib.php, EE_Event_Shortcodes.lib.php, EE_Attendee_Shortcodes.lib.php'
90
-     * for the admin context;
91
-     *
92
-     *
93
-     * #For Messengers:
94
-     * For example:
95
-     * array('subject' => array('transaction', 'event', 'attendee'))
96
-     * corresponds to 'EE_Transaction_Shortcodes.lib.php, EE_Event_Shortcodes.lib.php, EE_Attendee_Shortcodes.lib.php'
97
-     * for the 'subject' field;
98
-     * NOTE:  by default, with messengers, if the valid shortcodes for a field is left blank,
99
-     * that field will inherit whatever are set as valid shortcodes by message_type.
100
-     * This is so messenger can set specific valid codes for fields and leave other
101
-     * valid shortcodes up to the message type matched with the messenger.
102
-     *
103
-     * @access protected
104
-     * @var array
105
-     */
106
-    protected $_valid_shortcodes = array();
83
+	/**
84
+	 * this property will hold an array of valid shortcodes for this message type and messengers.
85
+	 * #For Message Types:
86
+	 * This is an array of strings that correspond to defined EE_Shortcode libraries and per context.
87
+	 * For example:
88
+	 * array( 'admin' => array('transaction', 'event', 'attendee') )
89
+	 * corresponds to 'EE_Transaction_Shortcodes.lib.php, EE_Event_Shortcodes.lib.php, EE_Attendee_Shortcodes.lib.php'
90
+	 * for the admin context;
91
+	 *
92
+	 *
93
+	 * #For Messengers:
94
+	 * For example:
95
+	 * array('subject' => array('transaction', 'event', 'attendee'))
96
+	 * corresponds to 'EE_Transaction_Shortcodes.lib.php, EE_Event_Shortcodes.lib.php, EE_Attendee_Shortcodes.lib.php'
97
+	 * for the 'subject' field;
98
+	 * NOTE:  by default, with messengers, if the valid shortcodes for a field is left blank,
99
+	 * that field will inherit whatever are set as valid shortcodes by message_type.
100
+	 * This is so messenger can set specific valid codes for fields and leave other
101
+	 * valid shortcodes up to the message type matched with the messenger.
102
+	 *
103
+	 * @access protected
104
+	 * @var array
105
+	 */
106
+	protected $_valid_shortcodes = array();
107 107
 
108 108
 
109 109
 
110 110
 
111 111
 
112
-    public function __construct()
113
-    {
114
-        $this->_set_admin_settings_fields();
115
-        $this->_set_valid_shortcodes();
116
-        $this->_set_admin_pages();
117
-    }
112
+	public function __construct()
113
+	{
114
+		$this->_set_admin_settings_fields();
115
+		$this->_set_valid_shortcodes();
116
+		$this->_set_admin_pages();
117
+	}
118 118
 
119 119
 
120 120
 
121 121
 
122 122
 
123
-    /**
124
-     * sets the _admin_settings_fields property which needs to be defined by child classes.
125
-     * You will want to set the _admin_settings_fields properties as a multi-dimensional array with the following format
126
-     * array(
127
-     *      {field_name - also used for setting index} => array(
128
-     *          'field_type' => {type of field: 'text', 'textarea', 'checkbox'},
129
-     *          'value_type' => {type of value: 'string', 'int', 'array', 'bool'},
130
-     *          'required' => {bool, required or not},
131
-     *          'validation' => {bool, true if we want validation, false if not},
132
-     *          'format' => {%d, or %s},
133
-     *          'label' => {label for the field, make sure it's localized},
134
-     *          'default' => {default value for the setting}
135
-     *      ),
136
-     * );
137
-     *
138
-     * @abstract
139
-     * @access protected
140
-     * @return void
141
-     */
142
-    abstract protected function _set_admin_settings_fields();
123
+	/**
124
+	 * sets the _admin_settings_fields property which needs to be defined by child classes.
125
+	 * You will want to set the _admin_settings_fields properties as a multi-dimensional array with the following format
126
+	 * array(
127
+	 *      {field_name - also used for setting index} => array(
128
+	 *          'field_type' => {type of field: 'text', 'textarea', 'checkbox'},
129
+	 *          'value_type' => {type of value: 'string', 'int', 'array', 'bool'},
130
+	 *          'required' => {bool, required or not},
131
+	 *          'validation' => {bool, true if we want validation, false if not},
132
+	 *          'format' => {%d, or %s},
133
+	 *          'label' => {label for the field, make sure it's localized},
134
+	 *          'default' => {default value for the setting}
135
+	 *      ),
136
+	 * );
137
+	 *
138
+	 * @abstract
139
+	 * @access protected
140
+	 * @return void
141
+	 */
142
+	abstract protected function _set_admin_settings_fields();
143 143
 
144 144
 
145 145
 
146 146
 
147 147
 
148
-    /**
149
-     * sets any properties on whether a message type or messenger interface shows up on a ee administration page.
150
-     * Child classes have to define this method but don't necessarily have to set the flags
151
-     * as they will be set to false by default.
152
-     *
153
-     * Child classes use this method to set the `_admin_registered_page` property.
154
-     * That property is to indicate what EE admin pages we have a corresponding callback for in the child class
155
-     * so Message Type/messenger fields/content is included on that admin page.
156
-     *
157
-     * @abstract
158
-     * @access protected
159
-     * @return void
160
-     */
161
-    abstract protected function _set_admin_pages();
148
+	/**
149
+	 * sets any properties on whether a message type or messenger interface shows up on a ee administration page.
150
+	 * Child classes have to define this method but don't necessarily have to set the flags
151
+	 * as they will be set to false by default.
152
+	 *
153
+	 * Child classes use this method to set the `_admin_registered_page` property.
154
+	 * That property is to indicate what EE admin pages we have a corresponding callback for in the child class
155
+	 * so Message Type/messenger fields/content is included on that admin page.
156
+	 *
157
+	 * @abstract
158
+	 * @access protected
159
+	 * @return void
160
+	 */
161
+	abstract protected function _set_admin_pages();
162 162
 
163 163
 
164 164
 
165 165
 
166 166
 
167
-    /**
168
-     * Child classes must declare the $_valid_shortcodes property using this method.
169
-     * See comments for $_valid_shortcodes property for details on what it is used for.
170
-     *
171
-     * @access protected
172
-     * @return void
173
-     */
174
-    abstract protected function _set_valid_shortcodes();
167
+	/**
168
+	 * Child classes must declare the $_valid_shortcodes property using this method.
169
+	 * See comments for $_valid_shortcodes property for details on what it is used for.
170
+	 *
171
+	 * @access protected
172
+	 * @return void
173
+	 */
174
+	abstract protected function _set_valid_shortcodes();
175 175
 
176 176
 
177 177
 
178
-    /**
179
-     * sets the _existing_admin_settings property can be overridden by child classes.
180
-     * We do this so we only do database calls if needed.
181
-     *
182
-     * @access protected
183
-     * @param string $messenger
184
-     */
185
-    protected function _set_existing_admin_settings($messenger = '')
186
-    {
187
-        /** @var EE_Message_Resource_Manager $Message_Resource_Manager */
188
-        $Message_Resource_Manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
189
-        $active_messengers = $Message_Resource_Manager->get_active_messengers_option();
190
-        $settings_to_use = $active_messengers;
191
-
192
-        /**
193
-         * This determines what will be used for the getting the settings.
194
-         */
195
-        if (
196
-            ! empty($messenger)
197
-            && $Message_Resource_Manager->is_message_type_active_for_messenger($messenger, $this->name)
198
-        ) {
199
-            $settings_to_use = $active_messengers[ $messenger ]['settings'][ $messenger . '-message_types' ];
200
-        }
201
-
202
-        $this->_existing_admin_settings = isset($settings_to_use[ $this->name ]['settings'])
203
-            ? $settings_to_use[ $this->name ]['settings']
204
-            : null;
205
-    }
206
-
207
-
208
-
209
-
210
-
211
-
212
-    /**
213
-     * get_existing_admin_settings
214
-     * (if needed) sets and returns the _existing_admin_settings property.
215
-     *
216
-     * @access public
217
-     * @param string $messenger
218
-     * @return array          settings
219
-     */
220
-    public function get_existing_admin_settings($messenger = '')
221
-    {
222
-        // if admin_settings property empty lets try setting it.
223
-        if (method_exists($this, '_set_existing_admin_settings') && empty($this->_existing_admin_settings)) {
224
-            $this->_set_existing_admin_settings($messenger);
225
-        }
226
-        return property_exists($this, '_existing_admin_settings')
227
-            ? $this->_existing_admin_settings
228
-            : null;
229
-    }
230
-
231
-
232
-
233
-
234
-
235
-
236
-    /**
237
-     * This returns the array of valid shortcodes for a message type or messenger as set by the child in the $_valid_shortcode property.
238
-     * @return array   an array of valid shortcodes.
239
-     */
240
-    public function get_valid_shortcodes()
241
-    {
242
-        $valid_shortcodes = apply_filters(
243
-            'FHEE__' . get_class($this) . '__get_valid_shortcodes',
244
-            $this->_valid_shortcodes,
245
-            $this
246
-        );
247
-        // The below filter applies to ALL messengers and message types so use with care!
248
-        $valid_shortcodes = apply_filters('FHEE__EE_Messages_Base__get_valid_shortcodes', $valid_shortcodes, $this);
249
-        return $valid_shortcodes;
250
-    }
251
-
252
-
253
-
254
-
255
-    /**
256
-     * getter that returns the protected admin_settings_fields property
257
-     *
258
-     * @access public
259
-     * @return array admin settings fields
260
-     */
261
-    public function get_admin_settings_fields()
262
-    {
263
-        return $this->_admin_settings_fields;
264
-    }
265
-
266
-
267
-
268
-
269
-
270
-    /**
271
-     * this public method accepts a page slug (for an EE_admin page)
272
-     * and will return the response from the child class callback function
273
-     * if that page is registered via the `_admin_registered_page` property set by the child class.
274
-     *
275
-     * @param string $page the slug of the EE admin page
276
-     * @param array $actives an array of active message type (or messenger) objects.
277
-     * @param string $action the page action (to allow for more specific handling - i.e. edit vs. add pages)
278
-     * @param array $extra This is just an extra argument that can be used
279
-     *                     to pass additional data for setting up page content.
280
-     * @access protected
281
-     * @return string $content for page.
282
-     */
283
-    protected function _get_admin_page_content($page, $action, $extra, $actives)
284
-    {
285
-        // we can also further refine the context by action (if present).
286
-        if (!empty($action)) {
287
-            $page = $page . '_' . $action;
288
-        }
289
-
290
-        if (!isset($this->admin_registered_pages[ $page ])) {
291
-            // todo: a place to throw an exception?
292
-            // We need to indicate there is no registered page so this function is not being called correctly.
293
-            return false;
294
-        }
295
-        // k made it here so let's call the method
296
-        $content = call_user_func_array(
297
-            array( $this, '_get_admin_content_' . $page ),
298
-            array( $actives, $extra )
299
-        );
300
-        if ($content === false) {
301
-            // todo this needs to be an exception once we've got exceptions in place.
302
-            return false;
303
-        }
304
-        return $content;
305
-    }
306
-
307
-
308
-    /**
309
-     * Allows a message type to specifically exclude template fields for the provided messenger.
310
-     * Filtered so this can be programmatically altered as well.
311
-     * @param string $messenger_name name of messenger
312
-     * @return array
313
-     */
314
-    public function excludedFieldsForMessenger($messenger_name)
315
-    {
316
-        return apply_filters(
317
-            'FHEE__EE_Messages_Base__excludedFieldForMessenger',
318
-            array(),
319
-            $messenger_name,
320
-            $this->name,
321
-            $this
322
-        );
323
-    }
178
+	/**
179
+	 * sets the _existing_admin_settings property can be overridden by child classes.
180
+	 * We do this so we only do database calls if needed.
181
+	 *
182
+	 * @access protected
183
+	 * @param string $messenger
184
+	 */
185
+	protected function _set_existing_admin_settings($messenger = '')
186
+	{
187
+		/** @var EE_Message_Resource_Manager $Message_Resource_Manager */
188
+		$Message_Resource_Manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
189
+		$active_messengers = $Message_Resource_Manager->get_active_messengers_option();
190
+		$settings_to_use = $active_messengers;
191
+
192
+		/**
193
+		 * This determines what will be used for the getting the settings.
194
+		 */
195
+		if (
196
+			! empty($messenger)
197
+			&& $Message_Resource_Manager->is_message_type_active_for_messenger($messenger, $this->name)
198
+		) {
199
+			$settings_to_use = $active_messengers[ $messenger ]['settings'][ $messenger . '-message_types' ];
200
+		}
201
+
202
+		$this->_existing_admin_settings = isset($settings_to_use[ $this->name ]['settings'])
203
+			? $settings_to_use[ $this->name ]['settings']
204
+			: null;
205
+	}
206
+
207
+
208
+
209
+
210
+
211
+
212
+	/**
213
+	 * get_existing_admin_settings
214
+	 * (if needed) sets and returns the _existing_admin_settings property.
215
+	 *
216
+	 * @access public
217
+	 * @param string $messenger
218
+	 * @return array          settings
219
+	 */
220
+	public function get_existing_admin_settings($messenger = '')
221
+	{
222
+		// if admin_settings property empty lets try setting it.
223
+		if (method_exists($this, '_set_existing_admin_settings') && empty($this->_existing_admin_settings)) {
224
+			$this->_set_existing_admin_settings($messenger);
225
+		}
226
+		return property_exists($this, '_existing_admin_settings')
227
+			? $this->_existing_admin_settings
228
+			: null;
229
+	}
230
+
231
+
232
+
233
+
234
+
235
+
236
+	/**
237
+	 * This returns the array of valid shortcodes for a message type or messenger as set by the child in the $_valid_shortcode property.
238
+	 * @return array   an array of valid shortcodes.
239
+	 */
240
+	public function get_valid_shortcodes()
241
+	{
242
+		$valid_shortcodes = apply_filters(
243
+			'FHEE__' . get_class($this) . '__get_valid_shortcodes',
244
+			$this->_valid_shortcodes,
245
+			$this
246
+		);
247
+		// The below filter applies to ALL messengers and message types so use with care!
248
+		$valid_shortcodes = apply_filters('FHEE__EE_Messages_Base__get_valid_shortcodes', $valid_shortcodes, $this);
249
+		return $valid_shortcodes;
250
+	}
251
+
252
+
253
+
254
+
255
+	/**
256
+	 * getter that returns the protected admin_settings_fields property
257
+	 *
258
+	 * @access public
259
+	 * @return array admin settings fields
260
+	 */
261
+	public function get_admin_settings_fields()
262
+	{
263
+		return $this->_admin_settings_fields;
264
+	}
265
+
266
+
267
+
268
+
269
+
270
+	/**
271
+	 * this public method accepts a page slug (for an EE_admin page)
272
+	 * and will return the response from the child class callback function
273
+	 * if that page is registered via the `_admin_registered_page` property set by the child class.
274
+	 *
275
+	 * @param string $page the slug of the EE admin page
276
+	 * @param array $actives an array of active message type (or messenger) objects.
277
+	 * @param string $action the page action (to allow for more specific handling - i.e. edit vs. add pages)
278
+	 * @param array $extra This is just an extra argument that can be used
279
+	 *                     to pass additional data for setting up page content.
280
+	 * @access protected
281
+	 * @return string $content for page.
282
+	 */
283
+	protected function _get_admin_page_content($page, $action, $extra, $actives)
284
+	{
285
+		// we can also further refine the context by action (if present).
286
+		if (!empty($action)) {
287
+			$page = $page . '_' . $action;
288
+		}
289
+
290
+		if (!isset($this->admin_registered_pages[ $page ])) {
291
+			// todo: a place to throw an exception?
292
+			// We need to indicate there is no registered page so this function is not being called correctly.
293
+			return false;
294
+		}
295
+		// k made it here so let's call the method
296
+		$content = call_user_func_array(
297
+			array( $this, '_get_admin_content_' . $page ),
298
+			array( $actives, $extra )
299
+		);
300
+		if ($content === false) {
301
+			// todo this needs to be an exception once we've got exceptions in place.
302
+			return false;
303
+		}
304
+		return $content;
305
+	}
306
+
307
+
308
+	/**
309
+	 * Allows a message type to specifically exclude template fields for the provided messenger.
310
+	 * Filtered so this can be programmatically altered as well.
311
+	 * @param string $messenger_name name of messenger
312
+	 * @return array
313
+	 */
314
+	public function excludedFieldsForMessenger($messenger_name)
315
+	{
316
+		return apply_filters(
317
+			'FHEE__EE_Messages_Base__excludedFieldForMessenger',
318
+			array(),
319
+			$messenger_name,
320
+			$this->name,
321
+			$this
322
+		);
323
+	}
324 324
 }
Please login to merge, or discard this patch.
core/libraries/messages/EE_Messages_Scheduler.lib.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
     public function __construct()
27 27
     {
28 28
         // register tasks (and make sure only registered once).
29
-        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
29
+        if ( ! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30 30
             add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31 31
         }
32 32
 
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
         EE_Registry::instance()->load_helper('DTT_Helper');
78 78
         $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
79 79
         $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
80
-        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
80
+        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array(EEH_DTT_Helper::tomorrow(), 'daily');
81 81
         return $tasks;
82 82
     }
83 83
 
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
                 true
97 97
             )
98 98
         ) {
99
-            $request_url  = add_query_arg(
99
+            $request_url = add_query_arg(
100 100
                 array_merge(
101 101
                     array('ee' => 'msg_cron_trigger'),
102 102
                     EE_Messages_Scheduler::get_request_params($task)
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
                 'sslverify'   => false,
110 110
                 'redirection' => 10,
111 111
             );
112
-            $response     = wp_remote_get($request_url, $request_args);
112
+            $response = wp_remote_get($request_url, $request_args);
113 113
             if (is_wp_error($response)) {
114 114
                 trigger_error($response->get_error_message());
115 115
             }
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
     public static function get_request_params($task)
130 130
     {
131 131
         // transient is used for flood control on msg_cron_trigger requests
132
-        $transient_key = 'ee_trans_' . uniqid($task);
132
+        $transient_key = 'ee_trans_'.uniqid($task);
133 133
         set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
134 134
         return array(
135 135
             'type' => $task,
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
             'AHEE__EE_Messages_Scheduler__sending'    => 'ee_message_cron',
203 203
         );
204 204
         foreach ($message_crons_to_check as $hook_name => $frequency) {
205
-            if (! wp_next_scheduled($hook_name)) {
205
+            if ( ! wp_next_scheduled($hook_name)) {
206 206
                 wp_schedule_event(time(), $frequency, $hook_name);
207 207
             }
208 208
         }
Please login to merge, or discard this patch.
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -14,225 +14,225 @@
 block discarded – undo
14 14
 class EE_Messages_Scheduler extends EE_Base
15 15
 {
16 16
 
17
-    /**
18
-     * Number of seconds between batch sends/generates on the cron job.
19
-     * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
20
-     * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
21
-     *
22
-     * @type int
23
-     */
24
-    const message_cron_schedule = 300;
25
-
26
-    /**
27
-     * Constructor
28
-     */
29
-    public function __construct()
30
-    {
31
-        // register tasks (and make sure only registered once).
32
-        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
33
-            add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
34
-        }
35
-
36
-        // register callbacks for scheduled events (but make sure they are set only once).
37
-        if (
38
-            ! has_action(
39
-                'AHEE__EE_Messages_Scheduler__generation',
40
-                array('EE_Messages_Scheduler', 'batch_generation')
41
-            )
42
-        ) {
43
-            add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
44
-            add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
45
-            add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
46
-        }
47
-
48
-        // add custom schedules
49
-        add_filter('cron_schedules', array($this, 'custom_schedules'));
50
-    }
51
-
52
-
53
-    /**
54
-     * Add custom schedules for wp_cron
55
-     *
56
-     * @param $schedules
57
-     */
58
-    public function custom_schedules($schedules)
59
-    {
60
-        $schedules['ee_message_cron'] = array(
61
-            'interval' => self::message_cron_schedule,
62
-            'display'  => esc_html__(
63
-                'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
64
-                'event_espresso'
65
-            ),
66
-        );
67
-        return $schedules;
68
-    }
69
-
70
-
71
-    /**
72
-     * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
73
-     * remove.
74
-     *
75
-     * @param array $tasks already existing scheduled tasks
76
-     * @return array
77
-     * @throws EE_Error
78
-     * @throws ReflectionException
79
-     */
80
-    public function register_scheduled_tasks($tasks)
81
-    {
82
-        EE_Registry::instance()->load_helper('DTT_Helper');
83
-        $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
84
-        $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
85
-        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
86
-        return $tasks;
87
-    }
88
-
89
-
90
-    /**
91
-     * This initiates a non-blocking separate request to execute on a scheduled task.
92
-     * Note: The EED_Messages module has the handlers for these requests.
93
-     *
94
-     * @param string $task The task the request is being generated for.
95
-     */
96
-    public static function initiate_scheduled_non_blocking_request($task)
97
-    {
98
-        if (
99
-            apply_filters(
100
-                'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
101
-                true
102
-            )
103
-        ) {
104
-            $request_url  = add_query_arg(
105
-                array_merge(
106
-                    array('ee' => 'msg_cron_trigger'),
107
-                    EE_Messages_Scheduler::get_request_params($task)
108
-                ),
109
-                site_url()
110
-            );
111
-            $request_args = array(
112
-                'timeout'     => 300,
113
-                'blocking'    => (defined('DOING_CRON') && DOING_CRON)
114
-                                 || (defined('DOING_AJAX') && DOING_AJAX),
115
-                'sslverify'   => false,
116
-                'redirection' => 10,
117
-            );
118
-            $response     = wp_remote_get($request_url, $request_args);
119
-            if (is_wp_error($response)) {
120
-                trigger_error($response->get_error_message());
121
-            }
122
-        } else {
123
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
124
-        }
125
-    }
126
-
127
-
128
-    /**
129
-     * This returns
130
-     * the request params used for a scheduled message task request.
131
-     *
132
-     * @param string $task The task the request is for.
133
-     * @return array
134
-     */
135
-    public static function get_request_params($task)
136
-    {
137
-        // transient is used for flood control on msg_cron_trigger requests
138
-        $transient_key = 'ee_trans_' . uniqid($task);
139
-        set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
140
-        return array(
141
-            'type' => $task,
142
-            'key'  => $transient_key,
143
-        );
144
-    }
145
-
146
-
147
-    /**
148
-     * This is used to execute an immediate call to the run_cron task performed by EED_Messages
149
-     *
150
-     * @param string $task The task the request is being generated for.
151
-     */
152
-    public static function initiate_immediate_request_on_cron($task)
153
-    {
154
-        /** @var RequestInterface $request */
155
-        $request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
156
-        $request_args = EE_Messages_Scheduler::get_request_params($task);
157
-        // set those request args in the request so it gets picked up
158
-        foreach ($request_args as $request_key => $request_value) {
159
-            $request->setRequestParam($request_key, $request_value);
160
-        }
161
-        EED_Messages::instance()->run_cron();
162
-    }
163
-
164
-
165
-    /**
166
-     * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
167
-     */
168
-    public static function batch_generation()
169
-    {
170
-        /**
171
-         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
172
-         */
173
-        if (
174
-            ! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
175
-            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
176
-        ) {
177
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
178
-        }
179
-    }
180
-
181
-
182
-    /**
183
-     * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
184
-     */
185
-    public static function batch_sending()
186
-    {
187
-        /**
188
-         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
189
-         */
190
-        if (
191
-            ! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
192
-            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
193
-        ) {
194
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
195
-        }
196
-    }
197
-
198
-
199
-    /**
200
-     * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
201
-     * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete
202
-     * permanently from the database messages that have a MSG_modified date older than 30 days.
203
-     *
204
-     * @throws EE_Error
205
-     * @throws EE_Error
206
-     */
207
-    public static function cleanup()
208
-    {
209
-        // First, confirm that the generation and sending EE_Messages_Scheduler crons are
210
-        // set and reschedule them if they are not.
211
-        $message_crons_to_check = array(
212
-            'AHEE__EE_Messages_Scheduler__generation' => 'ee_message_cron',
213
-            'AHEE__EE_Messages_Scheduler__sending'    => 'ee_message_cron',
214
-        );
215
-        foreach ($message_crons_to_check as $hook_name => $frequency) {
216
-            if (! wp_next_scheduled($hook_name)) {
217
-                wp_schedule_event(time(), $frequency, $hook_name);
218
-            }
219
-        }
220
-
221
-        // check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
222
-        // until the next scheduled event.
223
-        if (
224
-            ! EE_Registry::instance()->CFG->messages->delete_threshold
225
-            || ! EE_Maintenance_Mode::instance()->models_can_query()
226
-        ) {
227
-            return;
228
-        }
229
-
230
-        /**
231
-         * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
232
-         * of deleting messages.
233
-         */
234
-        if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
235
-            EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
236
-        }
237
-    }
17
+	/**
18
+	 * Number of seconds between batch sends/generates on the cron job.
19
+	 * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
20
+	 * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
21
+	 *
22
+	 * @type int
23
+	 */
24
+	const message_cron_schedule = 300;
25
+
26
+	/**
27
+	 * Constructor
28
+	 */
29
+	public function __construct()
30
+	{
31
+		// register tasks (and make sure only registered once).
32
+		if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
33
+			add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
34
+		}
35
+
36
+		// register callbacks for scheduled events (but make sure they are set only once).
37
+		if (
38
+			! has_action(
39
+				'AHEE__EE_Messages_Scheduler__generation',
40
+				array('EE_Messages_Scheduler', 'batch_generation')
41
+			)
42
+		) {
43
+			add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
44
+			add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
45
+			add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
46
+		}
47
+
48
+		// add custom schedules
49
+		add_filter('cron_schedules', array($this, 'custom_schedules'));
50
+	}
51
+
52
+
53
+	/**
54
+	 * Add custom schedules for wp_cron
55
+	 *
56
+	 * @param $schedules
57
+	 */
58
+	public function custom_schedules($schedules)
59
+	{
60
+		$schedules['ee_message_cron'] = array(
61
+			'interval' => self::message_cron_schedule,
62
+			'display'  => esc_html__(
63
+				'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
64
+				'event_espresso'
65
+			),
66
+		);
67
+		return $schedules;
68
+	}
69
+
70
+
71
+	/**
72
+	 * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
73
+	 * remove.
74
+	 *
75
+	 * @param array $tasks already existing scheduled tasks
76
+	 * @return array
77
+	 * @throws EE_Error
78
+	 * @throws ReflectionException
79
+	 */
80
+	public function register_scheduled_tasks($tasks)
81
+	{
82
+		EE_Registry::instance()->load_helper('DTT_Helper');
83
+		$tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
84
+		$tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
85
+		$tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
86
+		return $tasks;
87
+	}
88
+
89
+
90
+	/**
91
+	 * This initiates a non-blocking separate request to execute on a scheduled task.
92
+	 * Note: The EED_Messages module has the handlers for these requests.
93
+	 *
94
+	 * @param string $task The task the request is being generated for.
95
+	 */
96
+	public static function initiate_scheduled_non_blocking_request($task)
97
+	{
98
+		if (
99
+			apply_filters(
100
+				'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
101
+				true
102
+			)
103
+		) {
104
+			$request_url  = add_query_arg(
105
+				array_merge(
106
+					array('ee' => 'msg_cron_trigger'),
107
+					EE_Messages_Scheduler::get_request_params($task)
108
+				),
109
+				site_url()
110
+			);
111
+			$request_args = array(
112
+				'timeout'     => 300,
113
+				'blocking'    => (defined('DOING_CRON') && DOING_CRON)
114
+								 || (defined('DOING_AJAX') && DOING_AJAX),
115
+				'sslverify'   => false,
116
+				'redirection' => 10,
117
+			);
118
+			$response     = wp_remote_get($request_url, $request_args);
119
+			if (is_wp_error($response)) {
120
+				trigger_error($response->get_error_message());
121
+			}
122
+		} else {
123
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
124
+		}
125
+	}
126
+
127
+
128
+	/**
129
+	 * This returns
130
+	 * the request params used for a scheduled message task request.
131
+	 *
132
+	 * @param string $task The task the request is for.
133
+	 * @return array
134
+	 */
135
+	public static function get_request_params($task)
136
+	{
137
+		// transient is used for flood control on msg_cron_trigger requests
138
+		$transient_key = 'ee_trans_' . uniqid($task);
139
+		set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
140
+		return array(
141
+			'type' => $task,
142
+			'key'  => $transient_key,
143
+		);
144
+	}
145
+
146
+
147
+	/**
148
+	 * This is used to execute an immediate call to the run_cron task performed by EED_Messages
149
+	 *
150
+	 * @param string $task The task the request is being generated for.
151
+	 */
152
+	public static function initiate_immediate_request_on_cron($task)
153
+	{
154
+		/** @var RequestInterface $request */
155
+		$request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
156
+		$request_args = EE_Messages_Scheduler::get_request_params($task);
157
+		// set those request args in the request so it gets picked up
158
+		foreach ($request_args as $request_key => $request_value) {
159
+			$request->setRequestParam($request_key, $request_value);
160
+		}
161
+		EED_Messages::instance()->run_cron();
162
+	}
163
+
164
+
165
+	/**
166
+	 * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
167
+	 */
168
+	public static function batch_generation()
169
+	{
170
+		/**
171
+		 * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
172
+		 */
173
+		if (
174
+			! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
175
+			|| ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
176
+		) {
177
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
178
+		}
179
+	}
180
+
181
+
182
+	/**
183
+	 * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
184
+	 */
185
+	public static function batch_sending()
186
+	{
187
+		/**
188
+		 * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
189
+		 */
190
+		if (
191
+			! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
192
+			|| ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
193
+		) {
194
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
195
+		}
196
+	}
197
+
198
+
199
+	/**
200
+	 * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
201
+	 * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete
202
+	 * permanently from the database messages that have a MSG_modified date older than 30 days.
203
+	 *
204
+	 * @throws EE_Error
205
+	 * @throws EE_Error
206
+	 */
207
+	public static function cleanup()
208
+	{
209
+		// First, confirm that the generation and sending EE_Messages_Scheduler crons are
210
+		// set and reschedule them if they are not.
211
+		$message_crons_to_check = array(
212
+			'AHEE__EE_Messages_Scheduler__generation' => 'ee_message_cron',
213
+			'AHEE__EE_Messages_Scheduler__sending'    => 'ee_message_cron',
214
+		);
215
+		foreach ($message_crons_to_check as $hook_name => $frequency) {
216
+			if (! wp_next_scheduled($hook_name)) {
217
+				wp_schedule_event(time(), $frequency, $hook_name);
218
+			}
219
+		}
220
+
221
+		// check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
222
+		// until the next scheduled event.
223
+		if (
224
+			! EE_Registry::instance()->CFG->messages->delete_threshold
225
+			|| ! EE_Maintenance_Mode::instance()->models_can_query()
226
+		) {
227
+			return;
228
+		}
229
+
230
+		/**
231
+		 * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
232
+		 * of deleting messages.
233
+		 */
234
+		if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
235
+			EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
236
+		}
237
+	}
238 238
 }
Please login to merge, or discard this patch.
core/libraries/messages/data_class/EE_Messages_incoming_data.core.php 2 patches
Indentation   +518 added lines, -518 removed lines patch added patch discarded remove patch
@@ -18,522 +18,522 @@
 block discarded – undo
18 18
 {
19 19
 
20 20
 
21
-    /**
22
-     * user id for logged in user when data collected
23
-     *
24
-     * @var string $user_id
25
-     */
26
-    public $user_id;
27
-
28
-    /**
29
-     * IP Address of browser used
30
-     *
31
-     * @var string $ip_address
32
-     */
33
-    public $ip_address;
34
-
35
-    /**
36
-     * browser
37
-     *
38
-     * @var string $user_agent
39
-     */
40
-    public $user_agent;
41
-
42
-    /**
43
-     * Unix timestamp
44
-     *
45
-     * @var string $init_access
46
-     */
47
-    public $init_access;
48
-
49
-    /**
50
-     * Unix timestamp
51
-     *
52
-     * @var string $last_access
53
-     */
54
-    public $last_access;
55
-
56
-    /**
57
-     * The registrations details from the cart
58
-     *
59
-     * @var array $reg_info
60
-     */
61
-    public $reg_info;
62
-
63
-    /**
64
-     * Some data handlers can set what reg status all the registrations are filtered by.
65
-     * The status should match a EEM_Registration status constant.
66
-     *
67
-     * @var string $filtered_reg_status
68
-     */
69
-    public $filtered_reg_status;
70
-
71
-    /**
72
-     * will hold an array of events assembled from $reg_info
73
-     *
74
-     * @var EE_Event[] $events
75
-     */
76
-    public $events;
77
-
78
-    /**
79
-     * holds an array of datetimes assembled from the incoming data.
80
-     *
81
-     * @var EE_Datetime[] $datetimes
82
-     */
83
-    public $datetimes;
84
-
85
-    /**
86
-     * holds an array of tickets assembled from the incoming data.
87
-     *
88
-     * @var EE_Ticket[] $tickets
89
-     */
90
-    public $tickets;
91
-
92
-    /**
93
-     * holds an array with a key of parent line item and values are an array of children of that line item.
94
-     *
95
-     * @since 4.5.0
96
-     * @var EE_Line_Item[] $line_items_with_children
97
-     */
98
-    public $line_items_with_children;
99
-
100
-    /**
101
-     * will hold an array of attendees assembled from the $reg_info
102
-     *
103
-     * @var EE_Attendee[] $attendees
104
-     */
105
-    public $attendees;
106
-
107
-    /**
108
-     * will hold an array of cached registration objects and info assembled from reg_info
109
-     *
110
-     * @var array $registrations
111
-     */
112
-    public $registrations;
113
-
114
-    /**
115
-     * will hold an array of answers assembled from the $reg_info
116
-     *
117
-     * @var EE_Answer[] $answers
118
-     */
119
-    public $answers;
120
-
121
-    /**
122
-     * will hold an array of questions assembled from the $reg_info (indexed by Answer ID);
123
-     *
124
-     * @var EE_Question[] $questions
125
-     */
126
-    public $questions;
127
-
128
-    /**
129
-     * Will hold billing data assembled from $billing_info (if present)
130
-     *
131
-     * @var mixed (array|null) $billing
132
-     */
133
-    public $billing;
134
-
135
-    /**
136
-     * The total amount of tax for the transaction
137
-     *
138
-     * @var float $taxes
139
-     */
140
-    public $taxes;
141
-
142
-    /**
143
-     * Holds the line items related to taxes
144
-     *
145
-     * @since 4.5.0
146
-     * @var EE_Line_Item[] $tax_line_items
147
-     */
148
-    public $tax_line_items;
149
-
150
-    /**
151
-     * Hold the line items which aren't taxes and don't relate
152
-     * to tickets. So: promotions and miscellaneous charges
153
-     *
154
-     * @since 4.5
155
-     * @var EE_Line_Item[] $additional_line_items
156
-     */
157
-    public $additional_line_items;
158
-
159
-    /**
160
-     * Holds the grand total EE_Line_Item
161
-     *
162
-     * @var EE_Line_Item $grand_total_line_item
163
-     */
164
-    public $grand_total_line_item;
165
-
166
-    /**
167
-     * holds the grand total price object
168
-     * currently not used.
169
-     *
170
-     * @var null $grand_total_price_object
171
-     */
172
-    public $grand_total_price_object;
173
-
174
-    /**
175
-     * total number of tickets
176
-     *
177
-     * @var int $total_ticket_count
178
-     */
179
-    public $total_ticket_count;
180
-
181
-    /**
182
-     * Will hold the final transaction object (EE_Transaction)
183
-     *
184
-     * @var EE_Transaction $txn
185
-     */
186
-    public $txn;
187
-
188
-    /**
189
-     * Holds the payments related to a transaction
190
-     *
191
-     * @since 4.5.0
192
-     * @var EE_Payment[] $payments
193
-     */
194
-    public $payments;
195
-
196
-    /**
197
-     * Holds the first related payment related for a transaction
198
-     *
199
-     * @since 4.5.0
200
-     * @var EE_Payment $payment
201
-     */
202
-    public $payment;
203
-
204
-    /**
205
-     * Will hold the label for the txn status
206
-     *
207
-     * @var string $txn_status
208
-     */
209
-    public $txn_status;
210
-
211
-    /**
212
-     * Will hold the final registration object (EE_Registration)
213
-     *
214
-     * @var EE_Registration[] $reg_objs
215
-     */
216
-    public $reg_objs;
217
-
218
-    /**
219
-     * Will hold an array of primary attendee data (if present)
220
-     *
221
-     * @var array $primary_attendee_data
222
-     */
223
-    public $primary_attendee_data;
224
-
225
-    /**
226
-     * This is just an internal object used for passing around the incoming data.
227
-     *
228
-     * @var mixed $_data
229
-     */
230
-    protected $_data;
231
-
232
-    /**
233
-     * This is just an internal object used for passing around the incoming data.
234
-     *
235
-     * @var mixed $incoming_data
236
-     */
237
-    public $incoming_data;
238
-
239
-    /**
240
-     * hold objects that might be created
241
-     *
242
-     * @type EE_Registration $reg_obj
243
-     */
244
-    public $reg_obj;
245
-
246
-
247
-    /**
248
-     * constructor
249
-     *
250
-     * @param mixed $data incoming data object|array.  Suggested that child classes use type hinting for expected
251
-     *                    data object.  But here parent will be generic because we don't know what's coming in.
252
-     */
253
-    public function __construct($data)
254
-    {
255
-        $this->_data = $data;
256
-        $this->_setup_data();
257
-    }
258
-
259
-
260
-    /**
261
-     * Every child class has to setup the data object !
262
-     *
263
-     * @return void
264
-     */
265
-    abstract protected function _setup_data();
266
-
267
-
268
-    /**
269
-     * Returns database safe representation of the data later used to when instantiating this object.
270
-     *
271
-     * @param mixed $data The incoming data to be prepped.
272
-     * @return mixed   The prepped data for db
273
-     */
274
-    public static function convert_data_for_persistent_storage($data)
275
-    {
276
-        return $data;
277
-    }
278
-
279
-
280
-    /**
281
-     * Data that has been stored in persistent storage that was prepped by _convert_data_for_persistent_storage
282
-     * can be sent into this method and converted back into the format used for instantiating with this data handler.
283
-     *
284
-     * @param $data
285
-     * @return mixed
286
-     */
287
-    public static function convert_data_from_persistent_storage($data)
288
-    {
289
-        return $data;
290
-    }
291
-
292
-
293
-    /**
294
-     * only purpose is to return the data
295
-     *
296
-     * @access public
297
-     * @return mixed the formatted data object!
298
-     */
299
-    public function data()
300
-    {
301
-        return $this->_data;
302
-    }
303
-
304
-
305
-    /**
306
-     * This helper method can be used by any incoming data handlers to setup the data correctly.  All that is required
307
-     * is that $this->reg_objs be set.
308
-     *
309
-     * @throws EE_Error
310
-     * @throws InvalidDataTypeException
311
-     * @throws InvalidInterfaceException
312
-     * @throws InvalidArgumentException
313
-     * @throws EntityNotFoundException
314
-     */
315
-    protected function _assemble_data()
316
-    {
317
-        // verify that reg_objs is set
318
-        if (
319
-            ! is_array($this->reg_objs)
320
-            && ! reset($this->reg_objs) instanceof EE_Registration
321
-        ) {
322
-            throw new EE_Error(
323
-                esc_html__(
324
-                    'In order to assemble the data correctly, the "reg_objs" property must be an array of EE_Registration objects',
325
-                    'event_espresso'
326
-                )
327
-            );
328
-        }
329
-
330
-        // get all attendee and events associated with the registrations in this transaction
331
-        $events             = $event_setup = $evtcache = $tickets = $datetimes = array();
332
-        $answers            = $questions = $attendees = $line_items = $registrations = array();
333
-        $total_ticket_count = 0;
334
-
335
-        if (! empty($this->reg_objs)) {
336
-            $event_attendee_count = array();
337
-            /** @var EE_Registration $reg */
338
-            foreach ($this->reg_objs as $reg) {
339
-                if (
340
-                    $this->_skip_registration_for_processing($reg)
341
-                ) {
342
-                    continue;
343
-                }
344
-
345
-                $evt_id = $reg->event_ID();
346
-                /** @type EE_Ticket $ticket */
347
-                $ticket          = $reg->get_first_related('Ticket');
348
-                $attendee = $reg->attendee();
349
-                $event = $reg->event();
350
-                // if none of the following entities are available, then we can't setup other data reliably,
351
-                // so let's just skip.
352
-                if (
353
-                    ! $ticket instanceof EE_Ticket
354
-                    || ! $attendee instanceof EE_Attendee
355
-                    || ! $event instanceof EE_Event
356
-                ) {
357
-                    continue;
358
-                }
359
-                $relateddatetime = $ticket->datetimes();
360
-                $total_ticket_count++;
361
-                $tickets[ $ticket->ID() ]['ticket']                        = $ticket;
362
-                $tickets[ $ticket->ID() ]['count']                         = is_array($tickets[ $ticket->ID() ])
363
-                                                                           && isset($tickets[ $ticket->ID() ]['count'])
364
-                    ? $tickets[ $ticket->ID() ]['count'] + 1
365
-                    : 1;
366
-                $tickets[ $ticket->ID() ]['att_objs'][ $attendee->ID() ] = $attendee;
367
-                $tickets[ $ticket->ID() ]['dtt_objs']                      = $relateddatetime;
368
-                $tickets[ $ticket->ID() ]['reg_objs'][ $reg->ID() ]          = $reg;
369
-                $tickets[ $ticket->ID() ]['EE_Event']                      = $event;
370
-                $evtcache[ $evt_id ]                                       = $event;
371
-                $eventsetup[ $evt_id ]['reg_objs'][ $reg->ID() ]             = $reg;
372
-                $eventsetup[ $evt_id ]['tkt_objs'][ $ticket->ID() ]          = $ticket;
373
-                $eventsetup[ $evt_id ]['att_objs'][ $attendee->ID() ]    = $attendee;
374
-                $event_attendee_count[ $evt_id ]                           = isset($event_attendee_count[ $evt_id ])
375
-                    ? $event_attendee_count[ $evt_id ] + 1
376
-                    : 0;
377
-                $attendees[ $reg->attendee_ID() ]['line_ref'][]            = $evt_id;
378
-                $attendees[ $reg->attendee_ID() ]['att_obj']               = $attendee;
379
-                $attendees[ $reg->attendee_ID() ]['reg_objs'][ $reg->ID() ]  = $reg;
380
-                // $attendees[ $reg->attendee_ID() ]['registration_id'] = $reg->ID();
381
-                $attendees[ $reg->attendee_ID() ]['attendee_email']          = $attendee->email();
382
-                $attendees[ $reg->attendee_ID() ]['tkt_objs'][ $ticket->ID() ] = $ticket;
383
-                $attendees[ $reg->attendee_ID() ]['evt_objs'][ $evt_id ]       = $event;
384
-
385
-                // registrations
386
-                $registrations[ $reg->ID() ]['tkt_obj'] = $ticket;
387
-                $registrations[ $reg->ID() ]['evt_obj'] = $event;
388
-                $registrations[ $reg->ID() ]['reg_obj'] = $reg;
389
-                $registrations[ $reg->ID() ]['att_obj'] = $attendee;
390
-
391
-                // set up answer objects
392
-                $rel_ans = $reg->get_many_related('Answer');
393
-                foreach ($rel_ans as $ansid => $answer) {
394
-                    if (! isset($questions[ $ansid ])) {
395
-                        $questions[ $ansid ] = $answer->get_first_related('Question');
396
-                    }
397
-                    $answers[ $ansid ]                               = $answer;
398
-                    $registrations[ $reg->ID() ]['ans_objs'][ $ansid ] = $answer;
399
-                }
400
-                /**
401
-                 * @var int $dtt_id
402
-                 * @var EE_Datetime $datetime
403
-                 */
404
-                foreach ($relateddatetime as $dtt_id => $datetime) {
405
-                    $eventsetup[ $evt_id ]['dtt_objs'][ $dtt_id ]       = $datetime;
406
-                    $registrations[ $reg->ID() ]['dtt_objs'][ $dtt_id ] = $datetime;
407
-
408
-                    if (isset($datetimes[ $dtt_id ])) {
409
-                        continue; // already have this info in the datetimes array.
410
-                    }
411
-
412
-                    $datetimes[ $dtt_id ]['tkt_objs'][]           = $ticket;
413
-                    $datetimes[ $dtt_id ]['datetime']             = $datetime;
414
-                    $datetimes[ $dtt_id ]['evt_objs'][ $evt_id ]    = $event;
415
-                    $datetimes[ $dtt_id ]['reg_objs'][ $reg->ID() ] = $reg;
416
-                }
417
-            }
418
-
419
-            // let's loop through the unique event=>reg items and setup data on them
420
-
421
-            if (! empty($eventsetup)) {
422
-                foreach ($eventsetup as $evt_id => $items) {
423
-                    $ticket_line_items_for_event = array();
424
-                    if ($this->txn instanceof EE_Transaction) {
425
-                        $ticket_line_items_for_event = EEM_Line_Item::instance()->get_all(
426
-                            array(
427
-                                array(
428
-                                    'Ticket.Datetime.EVT_ID' => $evt_id,
429
-                                    'TXN_ID'                 => $this->txn->ID(),
430
-                                ),
431
-                                'default_where_conditions' => 'none',
432
-                            )
433
-                        );
434
-                    }
435
-                    $events[ $evt_id ] = array(
436
-                        'ID'              => $evt_id,
437
-                        'event'           => $evtcache[ $evt_id ],
438
-                        'name'            => $evtcache[ $evt_id ] instanceof EE_Event ? $evtcache[ $evt_id ]->name() : '',
439
-                        'total_attendees' => $event_attendee_count[ $evt_id ],
440
-                        'reg_objs'        => $items['reg_objs'],
441
-                        'tkt_objs'        => $items['tkt_objs'],
442
-                        'att_objs'        => $items['att_objs'],
443
-                        'dtt_objs'        => isset($items['dtt_objs']) ? $items['dtt_objs'] : array(),
444
-                        'line_items'      => $ticket_line_items_for_event,
445
-                    );
446
-
447
-                    // make sure the tickets have the line items setup for them.
448
-                    foreach ($ticket_line_items_for_event as $line_id => $line_item) {
449
-                        // only add the ticket line items if we already have this ticket in the $tickets array.
450
-                        if ($line_item instanceof EE_Line_Item && isset($tickets[ $line_item->ticket()->ID() ])) {
451
-                            $tickets[ $line_item->ticket()->ID() ]['line_item']      = $line_item;
452
-                            $tickets[ $line_item->ticket()->ID() ]['sub_line_items'] = $line_item->children();
453
-                            $line_items[ $line_item->ID() ]['children']              = $line_item->children();
454
-                            $line_items[ $line_item->ID() ]['EE_Ticket']             = $line_item->ticket();
455
-                        }
456
-                    }
457
-                }
458
-            }
459
-
460
-            $this->grand_total_line_item = $this->txn instanceof EE_Transaction
461
-                ? $this->txn->total_line_item()
462
-                : null;
463
-        }
464
-
465
-        // lets set the attendees and events properties
466
-        $this->attendees                = $attendees;
467
-        $this->events                   = $events;
468
-        $this->tickets                  = $tickets;
469
-        $this->line_items_with_children = $line_items;
470
-        $this->datetimes                = $datetimes;
471
-        $this->questions                = $questions;
472
-        $this->answers                  = $answers;
473
-        $this->total_ticket_count       = $total_ticket_count;
474
-        $this->registrations            = $registrations;
475
-
476
-        if ($this->txn instanceof EE_Transaction) {
477
-            $this->tax_line_items        = $this->txn->tax_items();
478
-            $this->additional_line_items = $this->txn->non_ticket_line_items();
479
-            $this->payments              = $this->txn->payments();
480
-
481
-            // setup primary registration if we have a single transaction object to work with
482
-
483
-            // let's get just the primary_attendee_data!  First we get the primary registration object.
484
-            $primary_reg = $this->txn->primary_registration();
485
-            // verify
486
-            if ($primary_reg instanceof EE_Registration) {
487
-                // get attendee object
488
-                if ($primary_reg->attendee() instanceof EE_Attendee) {
489
-                    // now we can setup the primary_attendee_data array
490
-                    $this->primary_attendee_data = array(
491
-                        'registration_id' => $primary_reg->ID(),
492
-                        'att_obj'         => $primary_reg->attendee(),
493
-                        'reg_obj'         => $primary_reg,
494
-                        'primary_att_obj' => $primary_reg->attendee(),
495
-                        'primary_reg_obj' => $primary_reg,
496
-                    );
497
-                } else {
498
-                    EE_Error::add_error(
499
-                        esc_html__(
500
-                            'Incoming data does not have a valid Attendee object for the primary registrant.',
501
-                            'event_espresso'
502
-                        ),
503
-                        __FILE__,
504
-                        __FUNCTION__,
505
-                        __LINE__
506
-                    );
507
-                }
508
-            } else {
509
-                EE_Error::add_error(
510
-                    esc_html__(
511
-                        'Incoming data does not have a valid Registration object for the primary registrant.',
512
-                        'event_espresso'
513
-                    ),
514
-                    __FILE__,
515
-                    __FUNCTION__,
516
-                    __LINE__
517
-                );
518
-            }
519
-        }
520
-    }
521
-
522
-    /**
523
-     * This simply considers whether the given registration should be processed or not based on comparison with the
524
-     * filtered_reg_status property.
525
-     *
526
-     * @param EE_Registration $registration
527
-     * @return bool  returning true means we DO want to skip processing.  returning false means we DON'T want to skip
528
-     *               processing
529
-     */
530
-    protected function _skip_registration_for_processing(EE_Registration $registration)
531
-    {
532
-        if (empty($this->filtered_reg_status)) {
533
-            return false;
534
-        }
535
-
536
-        // if we made it here then we just compare the filtered_reg_status with the registration status and return that
537
-        return $this->filtered_reg_status !== $registration->status_ID();
538
-    }
21
+	/**
22
+	 * user id for logged in user when data collected
23
+	 *
24
+	 * @var string $user_id
25
+	 */
26
+	public $user_id;
27
+
28
+	/**
29
+	 * IP Address of browser used
30
+	 *
31
+	 * @var string $ip_address
32
+	 */
33
+	public $ip_address;
34
+
35
+	/**
36
+	 * browser
37
+	 *
38
+	 * @var string $user_agent
39
+	 */
40
+	public $user_agent;
41
+
42
+	/**
43
+	 * Unix timestamp
44
+	 *
45
+	 * @var string $init_access
46
+	 */
47
+	public $init_access;
48
+
49
+	/**
50
+	 * Unix timestamp
51
+	 *
52
+	 * @var string $last_access
53
+	 */
54
+	public $last_access;
55
+
56
+	/**
57
+	 * The registrations details from the cart
58
+	 *
59
+	 * @var array $reg_info
60
+	 */
61
+	public $reg_info;
62
+
63
+	/**
64
+	 * Some data handlers can set what reg status all the registrations are filtered by.
65
+	 * The status should match a EEM_Registration status constant.
66
+	 *
67
+	 * @var string $filtered_reg_status
68
+	 */
69
+	public $filtered_reg_status;
70
+
71
+	/**
72
+	 * will hold an array of events assembled from $reg_info
73
+	 *
74
+	 * @var EE_Event[] $events
75
+	 */
76
+	public $events;
77
+
78
+	/**
79
+	 * holds an array of datetimes assembled from the incoming data.
80
+	 *
81
+	 * @var EE_Datetime[] $datetimes
82
+	 */
83
+	public $datetimes;
84
+
85
+	/**
86
+	 * holds an array of tickets assembled from the incoming data.
87
+	 *
88
+	 * @var EE_Ticket[] $tickets
89
+	 */
90
+	public $tickets;
91
+
92
+	/**
93
+	 * holds an array with a key of parent line item and values are an array of children of that line item.
94
+	 *
95
+	 * @since 4.5.0
96
+	 * @var EE_Line_Item[] $line_items_with_children
97
+	 */
98
+	public $line_items_with_children;
99
+
100
+	/**
101
+	 * will hold an array of attendees assembled from the $reg_info
102
+	 *
103
+	 * @var EE_Attendee[] $attendees
104
+	 */
105
+	public $attendees;
106
+
107
+	/**
108
+	 * will hold an array of cached registration objects and info assembled from reg_info
109
+	 *
110
+	 * @var array $registrations
111
+	 */
112
+	public $registrations;
113
+
114
+	/**
115
+	 * will hold an array of answers assembled from the $reg_info
116
+	 *
117
+	 * @var EE_Answer[] $answers
118
+	 */
119
+	public $answers;
120
+
121
+	/**
122
+	 * will hold an array of questions assembled from the $reg_info (indexed by Answer ID);
123
+	 *
124
+	 * @var EE_Question[] $questions
125
+	 */
126
+	public $questions;
127
+
128
+	/**
129
+	 * Will hold billing data assembled from $billing_info (if present)
130
+	 *
131
+	 * @var mixed (array|null) $billing
132
+	 */
133
+	public $billing;
134
+
135
+	/**
136
+	 * The total amount of tax for the transaction
137
+	 *
138
+	 * @var float $taxes
139
+	 */
140
+	public $taxes;
141
+
142
+	/**
143
+	 * Holds the line items related to taxes
144
+	 *
145
+	 * @since 4.5.0
146
+	 * @var EE_Line_Item[] $tax_line_items
147
+	 */
148
+	public $tax_line_items;
149
+
150
+	/**
151
+	 * Hold the line items which aren't taxes and don't relate
152
+	 * to tickets. So: promotions and miscellaneous charges
153
+	 *
154
+	 * @since 4.5
155
+	 * @var EE_Line_Item[] $additional_line_items
156
+	 */
157
+	public $additional_line_items;
158
+
159
+	/**
160
+	 * Holds the grand total EE_Line_Item
161
+	 *
162
+	 * @var EE_Line_Item $grand_total_line_item
163
+	 */
164
+	public $grand_total_line_item;
165
+
166
+	/**
167
+	 * holds the grand total price object
168
+	 * currently not used.
169
+	 *
170
+	 * @var null $grand_total_price_object
171
+	 */
172
+	public $grand_total_price_object;
173
+
174
+	/**
175
+	 * total number of tickets
176
+	 *
177
+	 * @var int $total_ticket_count
178
+	 */
179
+	public $total_ticket_count;
180
+
181
+	/**
182
+	 * Will hold the final transaction object (EE_Transaction)
183
+	 *
184
+	 * @var EE_Transaction $txn
185
+	 */
186
+	public $txn;
187
+
188
+	/**
189
+	 * Holds the payments related to a transaction
190
+	 *
191
+	 * @since 4.5.0
192
+	 * @var EE_Payment[] $payments
193
+	 */
194
+	public $payments;
195
+
196
+	/**
197
+	 * Holds the first related payment related for a transaction
198
+	 *
199
+	 * @since 4.5.0
200
+	 * @var EE_Payment $payment
201
+	 */
202
+	public $payment;
203
+
204
+	/**
205
+	 * Will hold the label for the txn status
206
+	 *
207
+	 * @var string $txn_status
208
+	 */
209
+	public $txn_status;
210
+
211
+	/**
212
+	 * Will hold the final registration object (EE_Registration)
213
+	 *
214
+	 * @var EE_Registration[] $reg_objs
215
+	 */
216
+	public $reg_objs;
217
+
218
+	/**
219
+	 * Will hold an array of primary attendee data (if present)
220
+	 *
221
+	 * @var array $primary_attendee_data
222
+	 */
223
+	public $primary_attendee_data;
224
+
225
+	/**
226
+	 * This is just an internal object used for passing around the incoming data.
227
+	 *
228
+	 * @var mixed $_data
229
+	 */
230
+	protected $_data;
231
+
232
+	/**
233
+	 * This is just an internal object used for passing around the incoming data.
234
+	 *
235
+	 * @var mixed $incoming_data
236
+	 */
237
+	public $incoming_data;
238
+
239
+	/**
240
+	 * hold objects that might be created
241
+	 *
242
+	 * @type EE_Registration $reg_obj
243
+	 */
244
+	public $reg_obj;
245
+
246
+
247
+	/**
248
+	 * constructor
249
+	 *
250
+	 * @param mixed $data incoming data object|array.  Suggested that child classes use type hinting for expected
251
+	 *                    data object.  But here parent will be generic because we don't know what's coming in.
252
+	 */
253
+	public function __construct($data)
254
+	{
255
+		$this->_data = $data;
256
+		$this->_setup_data();
257
+	}
258
+
259
+
260
+	/**
261
+	 * Every child class has to setup the data object !
262
+	 *
263
+	 * @return void
264
+	 */
265
+	abstract protected function _setup_data();
266
+
267
+
268
+	/**
269
+	 * Returns database safe representation of the data later used to when instantiating this object.
270
+	 *
271
+	 * @param mixed $data The incoming data to be prepped.
272
+	 * @return mixed   The prepped data for db
273
+	 */
274
+	public static function convert_data_for_persistent_storage($data)
275
+	{
276
+		return $data;
277
+	}
278
+
279
+
280
+	/**
281
+	 * Data that has been stored in persistent storage that was prepped by _convert_data_for_persistent_storage
282
+	 * can be sent into this method and converted back into the format used for instantiating with this data handler.
283
+	 *
284
+	 * @param $data
285
+	 * @return mixed
286
+	 */
287
+	public static function convert_data_from_persistent_storage($data)
288
+	{
289
+		return $data;
290
+	}
291
+
292
+
293
+	/**
294
+	 * only purpose is to return the data
295
+	 *
296
+	 * @access public
297
+	 * @return mixed the formatted data object!
298
+	 */
299
+	public function data()
300
+	{
301
+		return $this->_data;
302
+	}
303
+
304
+
305
+	/**
306
+	 * This helper method can be used by any incoming data handlers to setup the data correctly.  All that is required
307
+	 * is that $this->reg_objs be set.
308
+	 *
309
+	 * @throws EE_Error
310
+	 * @throws InvalidDataTypeException
311
+	 * @throws InvalidInterfaceException
312
+	 * @throws InvalidArgumentException
313
+	 * @throws EntityNotFoundException
314
+	 */
315
+	protected function _assemble_data()
316
+	{
317
+		// verify that reg_objs is set
318
+		if (
319
+			! is_array($this->reg_objs)
320
+			&& ! reset($this->reg_objs) instanceof EE_Registration
321
+		) {
322
+			throw new EE_Error(
323
+				esc_html__(
324
+					'In order to assemble the data correctly, the "reg_objs" property must be an array of EE_Registration objects',
325
+					'event_espresso'
326
+				)
327
+			);
328
+		}
329
+
330
+		// get all attendee and events associated with the registrations in this transaction
331
+		$events             = $event_setup = $evtcache = $tickets = $datetimes = array();
332
+		$answers            = $questions = $attendees = $line_items = $registrations = array();
333
+		$total_ticket_count = 0;
334
+
335
+		if (! empty($this->reg_objs)) {
336
+			$event_attendee_count = array();
337
+			/** @var EE_Registration $reg */
338
+			foreach ($this->reg_objs as $reg) {
339
+				if (
340
+					$this->_skip_registration_for_processing($reg)
341
+				) {
342
+					continue;
343
+				}
344
+
345
+				$evt_id = $reg->event_ID();
346
+				/** @type EE_Ticket $ticket */
347
+				$ticket          = $reg->get_first_related('Ticket');
348
+				$attendee = $reg->attendee();
349
+				$event = $reg->event();
350
+				// if none of the following entities are available, then we can't setup other data reliably,
351
+				// so let's just skip.
352
+				if (
353
+					! $ticket instanceof EE_Ticket
354
+					|| ! $attendee instanceof EE_Attendee
355
+					|| ! $event instanceof EE_Event
356
+				) {
357
+					continue;
358
+				}
359
+				$relateddatetime = $ticket->datetimes();
360
+				$total_ticket_count++;
361
+				$tickets[ $ticket->ID() ]['ticket']                        = $ticket;
362
+				$tickets[ $ticket->ID() ]['count']                         = is_array($tickets[ $ticket->ID() ])
363
+																		   && isset($tickets[ $ticket->ID() ]['count'])
364
+					? $tickets[ $ticket->ID() ]['count'] + 1
365
+					: 1;
366
+				$tickets[ $ticket->ID() ]['att_objs'][ $attendee->ID() ] = $attendee;
367
+				$tickets[ $ticket->ID() ]['dtt_objs']                      = $relateddatetime;
368
+				$tickets[ $ticket->ID() ]['reg_objs'][ $reg->ID() ]          = $reg;
369
+				$tickets[ $ticket->ID() ]['EE_Event']                      = $event;
370
+				$evtcache[ $evt_id ]                                       = $event;
371
+				$eventsetup[ $evt_id ]['reg_objs'][ $reg->ID() ]             = $reg;
372
+				$eventsetup[ $evt_id ]['tkt_objs'][ $ticket->ID() ]          = $ticket;
373
+				$eventsetup[ $evt_id ]['att_objs'][ $attendee->ID() ]    = $attendee;
374
+				$event_attendee_count[ $evt_id ]                           = isset($event_attendee_count[ $evt_id ])
375
+					? $event_attendee_count[ $evt_id ] + 1
376
+					: 0;
377
+				$attendees[ $reg->attendee_ID() ]['line_ref'][]            = $evt_id;
378
+				$attendees[ $reg->attendee_ID() ]['att_obj']               = $attendee;
379
+				$attendees[ $reg->attendee_ID() ]['reg_objs'][ $reg->ID() ]  = $reg;
380
+				// $attendees[ $reg->attendee_ID() ]['registration_id'] = $reg->ID();
381
+				$attendees[ $reg->attendee_ID() ]['attendee_email']          = $attendee->email();
382
+				$attendees[ $reg->attendee_ID() ]['tkt_objs'][ $ticket->ID() ] = $ticket;
383
+				$attendees[ $reg->attendee_ID() ]['evt_objs'][ $evt_id ]       = $event;
384
+
385
+				// registrations
386
+				$registrations[ $reg->ID() ]['tkt_obj'] = $ticket;
387
+				$registrations[ $reg->ID() ]['evt_obj'] = $event;
388
+				$registrations[ $reg->ID() ]['reg_obj'] = $reg;
389
+				$registrations[ $reg->ID() ]['att_obj'] = $attendee;
390
+
391
+				// set up answer objects
392
+				$rel_ans = $reg->get_many_related('Answer');
393
+				foreach ($rel_ans as $ansid => $answer) {
394
+					if (! isset($questions[ $ansid ])) {
395
+						$questions[ $ansid ] = $answer->get_first_related('Question');
396
+					}
397
+					$answers[ $ansid ]                               = $answer;
398
+					$registrations[ $reg->ID() ]['ans_objs'][ $ansid ] = $answer;
399
+				}
400
+				/**
401
+				 * @var int $dtt_id
402
+				 * @var EE_Datetime $datetime
403
+				 */
404
+				foreach ($relateddatetime as $dtt_id => $datetime) {
405
+					$eventsetup[ $evt_id ]['dtt_objs'][ $dtt_id ]       = $datetime;
406
+					$registrations[ $reg->ID() ]['dtt_objs'][ $dtt_id ] = $datetime;
407
+
408
+					if (isset($datetimes[ $dtt_id ])) {
409
+						continue; // already have this info in the datetimes array.
410
+					}
411
+
412
+					$datetimes[ $dtt_id ]['tkt_objs'][]           = $ticket;
413
+					$datetimes[ $dtt_id ]['datetime']             = $datetime;
414
+					$datetimes[ $dtt_id ]['evt_objs'][ $evt_id ]    = $event;
415
+					$datetimes[ $dtt_id ]['reg_objs'][ $reg->ID() ] = $reg;
416
+				}
417
+			}
418
+
419
+			// let's loop through the unique event=>reg items and setup data on them
420
+
421
+			if (! empty($eventsetup)) {
422
+				foreach ($eventsetup as $evt_id => $items) {
423
+					$ticket_line_items_for_event = array();
424
+					if ($this->txn instanceof EE_Transaction) {
425
+						$ticket_line_items_for_event = EEM_Line_Item::instance()->get_all(
426
+							array(
427
+								array(
428
+									'Ticket.Datetime.EVT_ID' => $evt_id,
429
+									'TXN_ID'                 => $this->txn->ID(),
430
+								),
431
+								'default_where_conditions' => 'none',
432
+							)
433
+						);
434
+					}
435
+					$events[ $evt_id ] = array(
436
+						'ID'              => $evt_id,
437
+						'event'           => $evtcache[ $evt_id ],
438
+						'name'            => $evtcache[ $evt_id ] instanceof EE_Event ? $evtcache[ $evt_id ]->name() : '',
439
+						'total_attendees' => $event_attendee_count[ $evt_id ],
440
+						'reg_objs'        => $items['reg_objs'],
441
+						'tkt_objs'        => $items['tkt_objs'],
442
+						'att_objs'        => $items['att_objs'],
443
+						'dtt_objs'        => isset($items['dtt_objs']) ? $items['dtt_objs'] : array(),
444
+						'line_items'      => $ticket_line_items_for_event,
445
+					);
446
+
447
+					// make sure the tickets have the line items setup for them.
448
+					foreach ($ticket_line_items_for_event as $line_id => $line_item) {
449
+						// only add the ticket line items if we already have this ticket in the $tickets array.
450
+						if ($line_item instanceof EE_Line_Item && isset($tickets[ $line_item->ticket()->ID() ])) {
451
+							$tickets[ $line_item->ticket()->ID() ]['line_item']      = $line_item;
452
+							$tickets[ $line_item->ticket()->ID() ]['sub_line_items'] = $line_item->children();
453
+							$line_items[ $line_item->ID() ]['children']              = $line_item->children();
454
+							$line_items[ $line_item->ID() ]['EE_Ticket']             = $line_item->ticket();
455
+						}
456
+					}
457
+				}
458
+			}
459
+
460
+			$this->grand_total_line_item = $this->txn instanceof EE_Transaction
461
+				? $this->txn->total_line_item()
462
+				: null;
463
+		}
464
+
465
+		// lets set the attendees and events properties
466
+		$this->attendees                = $attendees;
467
+		$this->events                   = $events;
468
+		$this->tickets                  = $tickets;
469
+		$this->line_items_with_children = $line_items;
470
+		$this->datetimes                = $datetimes;
471
+		$this->questions                = $questions;
472
+		$this->answers                  = $answers;
473
+		$this->total_ticket_count       = $total_ticket_count;
474
+		$this->registrations            = $registrations;
475
+
476
+		if ($this->txn instanceof EE_Transaction) {
477
+			$this->tax_line_items        = $this->txn->tax_items();
478
+			$this->additional_line_items = $this->txn->non_ticket_line_items();
479
+			$this->payments              = $this->txn->payments();
480
+
481
+			// setup primary registration if we have a single transaction object to work with
482
+
483
+			// let's get just the primary_attendee_data!  First we get the primary registration object.
484
+			$primary_reg = $this->txn->primary_registration();
485
+			// verify
486
+			if ($primary_reg instanceof EE_Registration) {
487
+				// get attendee object
488
+				if ($primary_reg->attendee() instanceof EE_Attendee) {
489
+					// now we can setup the primary_attendee_data array
490
+					$this->primary_attendee_data = array(
491
+						'registration_id' => $primary_reg->ID(),
492
+						'att_obj'         => $primary_reg->attendee(),
493
+						'reg_obj'         => $primary_reg,
494
+						'primary_att_obj' => $primary_reg->attendee(),
495
+						'primary_reg_obj' => $primary_reg,
496
+					);
497
+				} else {
498
+					EE_Error::add_error(
499
+						esc_html__(
500
+							'Incoming data does not have a valid Attendee object for the primary registrant.',
501
+							'event_espresso'
502
+						),
503
+						__FILE__,
504
+						__FUNCTION__,
505
+						__LINE__
506
+					);
507
+				}
508
+			} else {
509
+				EE_Error::add_error(
510
+					esc_html__(
511
+						'Incoming data does not have a valid Registration object for the primary registrant.',
512
+						'event_espresso'
513
+					),
514
+					__FILE__,
515
+					__FUNCTION__,
516
+					__LINE__
517
+				);
518
+			}
519
+		}
520
+	}
521
+
522
+	/**
523
+	 * This simply considers whether the given registration should be processed or not based on comparison with the
524
+	 * filtered_reg_status property.
525
+	 *
526
+	 * @param EE_Registration $registration
527
+	 * @return bool  returning true means we DO want to skip processing.  returning false means we DON'T want to skip
528
+	 *               processing
529
+	 */
530
+	protected function _skip_registration_for_processing(EE_Registration $registration)
531
+	{
532
+		if (empty($this->filtered_reg_status)) {
533
+			return false;
534
+		}
535
+
536
+		// if we made it here then we just compare the filtered_reg_status with the registration status and return that
537
+		return $this->filtered_reg_status !== $registration->status_ID();
538
+	}
539 539
 }
Please login to merge, or discard this patch.
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -332,7 +332,7 @@  discard block
 block discarded – undo
332 332
         $answers            = $questions = $attendees = $line_items = $registrations = array();
333 333
         $total_ticket_count = 0;
334 334
 
335
-        if (! empty($this->reg_objs)) {
335
+        if ( ! empty($this->reg_objs)) {
336 336
             $event_attendee_count = array();
337 337
             /** @var EE_Registration $reg */
338 338
             foreach ($this->reg_objs as $reg) {
@@ -344,7 +344,7 @@  discard block
 block discarded – undo
344 344
 
345 345
                 $evt_id = $reg->event_ID();
346 346
                 /** @type EE_Ticket $ticket */
347
-                $ticket          = $reg->get_first_related('Ticket');
347
+                $ticket = $reg->get_first_related('Ticket');
348 348
                 $attendee = $reg->attendee();
349 349
                 $event = $reg->event();
350 350
                 // if none of the following entities are available, then we can't setup other data reliably,
@@ -358,67 +358,67 @@  discard block
 block discarded – undo
358 358
                 }
359 359
                 $relateddatetime = $ticket->datetimes();
360 360
                 $total_ticket_count++;
361
-                $tickets[ $ticket->ID() ]['ticket']                        = $ticket;
362
-                $tickets[ $ticket->ID() ]['count']                         = is_array($tickets[ $ticket->ID() ])
363
-                                                                           && isset($tickets[ $ticket->ID() ]['count'])
364
-                    ? $tickets[ $ticket->ID() ]['count'] + 1
361
+                $tickets[$ticket->ID()]['ticket']                        = $ticket;
362
+                $tickets[$ticket->ID()]['count']                         = is_array($tickets[$ticket->ID()])
363
+                                                                           && isset($tickets[$ticket->ID()]['count'])
364
+                    ? $tickets[$ticket->ID()]['count'] + 1
365 365
                     : 1;
366
-                $tickets[ $ticket->ID() ]['att_objs'][ $attendee->ID() ] = $attendee;
367
-                $tickets[ $ticket->ID() ]['dtt_objs']                      = $relateddatetime;
368
-                $tickets[ $ticket->ID() ]['reg_objs'][ $reg->ID() ]          = $reg;
369
-                $tickets[ $ticket->ID() ]['EE_Event']                      = $event;
370
-                $evtcache[ $evt_id ]                                       = $event;
371
-                $eventsetup[ $evt_id ]['reg_objs'][ $reg->ID() ]             = $reg;
372
-                $eventsetup[ $evt_id ]['tkt_objs'][ $ticket->ID() ]          = $ticket;
373
-                $eventsetup[ $evt_id ]['att_objs'][ $attendee->ID() ]    = $attendee;
374
-                $event_attendee_count[ $evt_id ]                           = isset($event_attendee_count[ $evt_id ])
375
-                    ? $event_attendee_count[ $evt_id ] + 1
366
+                $tickets[$ticket->ID()]['att_objs'][$attendee->ID()] = $attendee;
367
+                $tickets[$ticket->ID()]['dtt_objs']                      = $relateddatetime;
368
+                $tickets[$ticket->ID()]['reg_objs'][$reg->ID()]          = $reg;
369
+                $tickets[$ticket->ID()]['EE_Event']                      = $event;
370
+                $evtcache[$evt_id]                                       = $event;
371
+                $eventsetup[$evt_id]['reg_objs'][$reg->ID()]             = $reg;
372
+                $eventsetup[$evt_id]['tkt_objs'][$ticket->ID()]          = $ticket;
373
+                $eventsetup[$evt_id]['att_objs'][$attendee->ID()] = $attendee;
374
+                $event_attendee_count[$evt_id]                           = isset($event_attendee_count[$evt_id])
375
+                    ? $event_attendee_count[$evt_id] + 1
376 376
                     : 0;
377
-                $attendees[ $reg->attendee_ID() ]['line_ref'][]            = $evt_id;
378
-                $attendees[ $reg->attendee_ID() ]['att_obj']               = $attendee;
379
-                $attendees[ $reg->attendee_ID() ]['reg_objs'][ $reg->ID() ]  = $reg;
377
+                $attendees[$reg->attendee_ID()]['line_ref'][]            = $evt_id;
378
+                $attendees[$reg->attendee_ID()]['att_obj']               = $attendee;
379
+                $attendees[$reg->attendee_ID()]['reg_objs'][$reg->ID()]  = $reg;
380 380
                 // $attendees[ $reg->attendee_ID() ]['registration_id'] = $reg->ID();
381
-                $attendees[ $reg->attendee_ID() ]['attendee_email']          = $attendee->email();
382
-                $attendees[ $reg->attendee_ID() ]['tkt_objs'][ $ticket->ID() ] = $ticket;
383
-                $attendees[ $reg->attendee_ID() ]['evt_objs'][ $evt_id ]       = $event;
381
+                $attendees[$reg->attendee_ID()]['attendee_email']          = $attendee->email();
382
+                $attendees[$reg->attendee_ID()]['tkt_objs'][$ticket->ID()] = $ticket;
383
+                $attendees[$reg->attendee_ID()]['evt_objs'][$evt_id]       = $event;
384 384
 
385 385
                 // registrations
386
-                $registrations[ $reg->ID() ]['tkt_obj'] = $ticket;
387
-                $registrations[ $reg->ID() ]['evt_obj'] = $event;
388
-                $registrations[ $reg->ID() ]['reg_obj'] = $reg;
389
-                $registrations[ $reg->ID() ]['att_obj'] = $attendee;
386
+                $registrations[$reg->ID()]['tkt_obj'] = $ticket;
387
+                $registrations[$reg->ID()]['evt_obj'] = $event;
388
+                $registrations[$reg->ID()]['reg_obj'] = $reg;
389
+                $registrations[$reg->ID()]['att_obj'] = $attendee;
390 390
 
391 391
                 // set up answer objects
392 392
                 $rel_ans = $reg->get_many_related('Answer');
393 393
                 foreach ($rel_ans as $ansid => $answer) {
394
-                    if (! isset($questions[ $ansid ])) {
395
-                        $questions[ $ansid ] = $answer->get_first_related('Question');
394
+                    if ( ! isset($questions[$ansid])) {
395
+                        $questions[$ansid] = $answer->get_first_related('Question');
396 396
                     }
397
-                    $answers[ $ansid ]                               = $answer;
398
-                    $registrations[ $reg->ID() ]['ans_objs'][ $ansid ] = $answer;
397
+                    $answers[$ansid] = $answer;
398
+                    $registrations[$reg->ID()]['ans_objs'][$ansid] = $answer;
399 399
                 }
400 400
                 /**
401 401
                  * @var int $dtt_id
402 402
                  * @var EE_Datetime $datetime
403 403
                  */
404 404
                 foreach ($relateddatetime as $dtt_id => $datetime) {
405
-                    $eventsetup[ $evt_id ]['dtt_objs'][ $dtt_id ]       = $datetime;
406
-                    $registrations[ $reg->ID() ]['dtt_objs'][ $dtt_id ] = $datetime;
405
+                    $eventsetup[$evt_id]['dtt_objs'][$dtt_id]       = $datetime;
406
+                    $registrations[$reg->ID()]['dtt_objs'][$dtt_id] = $datetime;
407 407
 
408
-                    if (isset($datetimes[ $dtt_id ])) {
408
+                    if (isset($datetimes[$dtt_id])) {
409 409
                         continue; // already have this info in the datetimes array.
410 410
                     }
411 411
 
412
-                    $datetimes[ $dtt_id ]['tkt_objs'][]           = $ticket;
413
-                    $datetimes[ $dtt_id ]['datetime']             = $datetime;
414
-                    $datetimes[ $dtt_id ]['evt_objs'][ $evt_id ]    = $event;
415
-                    $datetimes[ $dtt_id ]['reg_objs'][ $reg->ID() ] = $reg;
412
+                    $datetimes[$dtt_id]['tkt_objs'][]           = $ticket;
413
+                    $datetimes[$dtt_id]['datetime']             = $datetime;
414
+                    $datetimes[$dtt_id]['evt_objs'][$evt_id]    = $event;
415
+                    $datetimes[$dtt_id]['reg_objs'][$reg->ID()] = $reg;
416 416
                 }
417 417
             }
418 418
 
419 419
             // let's loop through the unique event=>reg items and setup data on them
420 420
 
421
-            if (! empty($eventsetup)) {
421
+            if ( ! empty($eventsetup)) {
422 422
                 foreach ($eventsetup as $evt_id => $items) {
423 423
                     $ticket_line_items_for_event = array();
424 424
                     if ($this->txn instanceof EE_Transaction) {
@@ -432,11 +432,11 @@  discard block
 block discarded – undo
432 432
                             )
433 433
                         );
434 434
                     }
435
-                    $events[ $evt_id ] = array(
435
+                    $events[$evt_id] = array(
436 436
                         'ID'              => $evt_id,
437
-                        'event'           => $evtcache[ $evt_id ],
438
-                        'name'            => $evtcache[ $evt_id ] instanceof EE_Event ? $evtcache[ $evt_id ]->name() : '',
439
-                        'total_attendees' => $event_attendee_count[ $evt_id ],
437
+                        'event'           => $evtcache[$evt_id],
438
+                        'name'            => $evtcache[$evt_id] instanceof EE_Event ? $evtcache[$evt_id]->name() : '',
439
+                        'total_attendees' => $event_attendee_count[$evt_id],
440 440
                         'reg_objs'        => $items['reg_objs'],
441 441
                         'tkt_objs'        => $items['tkt_objs'],
442 442
                         'att_objs'        => $items['att_objs'],
@@ -447,11 +447,11 @@  discard block
 block discarded – undo
447 447
                     // make sure the tickets have the line items setup for them.
448 448
                     foreach ($ticket_line_items_for_event as $line_id => $line_item) {
449 449
                         // only add the ticket line items if we already have this ticket in the $tickets array.
450
-                        if ($line_item instanceof EE_Line_Item && isset($tickets[ $line_item->ticket()->ID() ])) {
451
-                            $tickets[ $line_item->ticket()->ID() ]['line_item']      = $line_item;
452
-                            $tickets[ $line_item->ticket()->ID() ]['sub_line_items'] = $line_item->children();
453
-                            $line_items[ $line_item->ID() ]['children']              = $line_item->children();
454
-                            $line_items[ $line_item->ID() ]['EE_Ticket']             = $line_item->ticket();
450
+                        if ($line_item instanceof EE_Line_Item && isset($tickets[$line_item->ticket()->ID()])) {
451
+                            $tickets[$line_item->ticket()->ID()]['line_item']      = $line_item;
452
+                            $tickets[$line_item->ticket()->ID()]['sub_line_items'] = $line_item->children();
453
+                            $line_items[$line_item->ID()]['children']              = $line_item->children();
454
+                            $line_items[$line_item->ID()]['EE_Ticket']             = $line_item->ticket();
455 455
                         }
456 456
                     }
457 457
                 }
Please login to merge, or discard this patch.
core/libraries/payment_methods/EE_Payment_Method_Manager.lib.php 2 patches
Indentation   +557 added lines, -557 removed lines patch added patch discarded remove patch
@@ -17,561 +17,561 @@
 block discarded – undo
17 17
 class EE_Payment_Method_Manager implements ResettableInterface
18 18
 {
19 19
 
20
-    /**
21
-     * prefix added to all payment method capabilities names
22
-     */
23
-    const   CAPABILITIES_PREFIX = 'ee_payment_method_';
24
-
25
-    /**
26
-     * @var EE_Payment_Method_Manager $_instance
27
-     */
28
-    private static $_instance;
29
-
30
-    /**
31
-     * @var boolean
32
-     */
33
-    protected $payment_method_caps_initialized = false;
34
-
35
-    /**
36
-     * @var array keys are class names without 'EE_PMT_', values are their filepaths
37
-     */
38
-    protected $_payment_method_types = array();
39
-
40
-    /**
41
-     * @var EE_PMT_Base[]
42
-     */
43
-    protected $payment_method_objects = array();
44
-
45
-
46
-    /**
47
-     * EE_Payment_Method_Manager constructor.
48
-     *
49
-     * @throws EE_Error
50
-     * @throws DomainException
51
-     */
52
-    public function __construct()
53
-    {
54
-        // if in admin lets ensure caps are set.
55
-        if (is_admin()) {
56
-            $this->_register_payment_methods();
57
-            // set them immediately
58
-            $this->initializePaymentMethodCaps();
59
-            // plus any time they get reset
60
-            add_filter(
61
-                'FHEE__EE_Capabilities__addCaps__capabilities_to_add',
62
-                array($this, 'addPaymentMethodCapsDuringReset')
63
-            );
64
-        }
65
-    }
66
-
67
-
68
-    /**
69
-     * @singleton method used to instantiate class object
70
-     * @return EE_Payment_Method_Manager instance
71
-     * @throws DomainException
72
-     * @throws EE_Error
73
-     */
74
-    public static function instance()
75
-    {
76
-        // check if class object is instantiated, and instantiated properly
77
-        if (! self::$_instance instanceof EE_Payment_Method_Manager) {
78
-            EE_Registry::instance()->load_lib('PMT_Base');
79
-            self::$_instance = new self();
80
-        }
81
-        return self::$_instance;
82
-    }
83
-
84
-
85
-    /**
86
-     * Resets the instance and returns a new one
87
-     *
88
-     * @return EE_Payment_Method_Manager
89
-     * @throws DomainException
90
-     * @throws EE_Error
91
-     */
92
-    public static function reset()
93
-    {
94
-        self::$_instance = null;
95
-        return self::instance();
96
-    }
97
-
98
-
99
-    /**
100
-     * If necessary, re-register payment methods
101
-     *
102
-     * @param boolean $force_recheck whether to recheck for payment method types,
103
-     *                               or just re-use the PMTs we found last time we checked during this request (if
104
-     *                               we have not yet checked during this request, then we need to check anyways)
105
-     */
106
-    public function maybe_register_payment_methods($force_recheck = false)
107
-    {
108
-        if (! $this->_payment_method_types || $force_recheck) {
109
-            $this->_register_payment_methods();
110
-        }
111
-    }
112
-
113
-
114
-    /**
115
-     * register_payment_methods
116
-     *
117
-     * @return array
118
-     */
119
-    protected function _register_payment_methods()
120
-    {
121
-        // grab list of installed modules
122
-        $pm_to_register = glob(EE_PAYMENT_METHODS . '*', GLOB_ONLYDIR);
123
-        // filter list of modules to register
124
-        $pm_to_register = apply_filters(
125
-            'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register',
126
-            $pm_to_register
127
-        );
128
-        // remove any duplicates if that should happen for some reason
129
-        $pm_to_register = array_unique($pm_to_register);
130
-        // loop through folders
131
-        foreach ($pm_to_register as $pm_path) {
132
-            $this->register_payment_method($pm_path);
133
-        }
134
-        do_action('FHEE__EE_Payment_Method_Manager__register_payment_methods__registered_payment_methods');
135
-        // filter list of installed modules
136
-        // keep them organized alphabetically by the payment method type's name
137
-        ksort($this->_payment_method_types);
138
-        return apply_filters(
139
-            'FHEE__EE_Payment_Method_Manager__register_payment_methods__installed_payment_methods',
140
-            $this->_payment_method_types
141
-        );
142
-    }
143
-
144
-
145
-    /**
146
-     * register_payment_method- makes core aware of this payment method
147
-     *
148
-     * @param string $payment_method_path - full path up to and including payment method folder
149
-     * @return boolean
150
-     */
151
-    public function register_payment_method($payment_method_path = '')
152
-    {
153
-        do_action('AHEE__EE_Payment_Method_Manager__register_payment_method__begin', $payment_method_path);
154
-        $module_ext = '.pm.php';
155
-        // make all separators match
156
-        $payment_method_path = rtrim(str_replace('/\\', '/', $payment_method_path), '/');
157
-        // grab and sanitize module name
158
-        $module_dir = basename($payment_method_path);
159
-        // create class name from module directory name
160
-        $module = str_replace(array('_', ' '), array(' ', '_'), $module_dir);
161
-        // add class prefix
162
-        $module_class = 'EE_PMT_' . $module;
163
-        // does the module exist ?
164
-        if (! is_readable($payment_method_path . '/' . $module_class . $module_ext)) {
165
-            $msg = sprintf(
166
-                esc_html__(
167
-                    'The requested %s payment method file could not be found or is not readable due to file permissions.',
168
-                    'event_espresso'
169
-                ),
170
-                $module
171
-            );
172
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
173
-            return false;
174
-        }
175
-        // load the module class file
176
-        require_once($payment_method_path . '/' . $module_class . $module_ext);
177
-        // verify that class exists
178
-        if (! class_exists($module_class)) {
179
-            $msg = sprintf(
180
-                esc_html__('The requested %s module class does not exist.', 'event_espresso'),
181
-                $module_class
182
-            );
183
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
184
-            return false;
185
-        }
186
-        // add to array of registered modules
187
-        $this->_payment_method_types[ $module ] = $payment_method_path . '/' . $module_class . $module_ext;
188
-        ksort($this->_payment_method_types);
189
-        return true;
190
-    }
191
-
192
-
193
-    /**
194
-     * Checks if a payment method has been registered, and if so includes it
195
-     *
196
-     * @param string  $payment_method_name like 'PayPal_Pro', (ie class name without the prefix 'EEPM_')
197
-     * @param boolean $force_recheck       whether to force re-checking for new payment method types
198
-     * @return boolean
199
-     */
200
-    public function payment_method_type_exists($payment_method_name, $force_recheck = false)
201
-    {
202
-        if (
203
-            $force_recheck
204
-            || ! is_array($this->_payment_method_types)
205
-            || ! isset($this->_payment_method_types[ $payment_method_name ])
206
-        ) {
207
-            $this->maybe_register_payment_methods($force_recheck);
208
-        }
209
-        if (isset($this->_payment_method_types[ $payment_method_name ])) {
210
-            require_once($this->_payment_method_types[ $payment_method_name ]);
211
-            return true;
212
-        }
213
-        return false;
214
-    }
215
-
216
-
217
-    /**
218
-     * Returns all the class names of the various payment method types
219
-     *
220
-     * @param boolean $with_prefixes TRUE: get payment method type class names; false just their 'names'
221
-     *                               (what you'd find in wp_esp_payment_method.PMD_type)
222
-     * @param boolean $force_recheck whether to force re-checking for new payment method types
223
-     * @return array
224
-     */
225
-    public function payment_method_type_names($with_prefixes = false, $force_recheck = false)
226
-    {
227
-        $this->maybe_register_payment_methods($force_recheck);
228
-        if ($with_prefixes) {
229
-            $classnames = array_keys($this->_payment_method_types);
230
-            $payment_methods = array();
231
-            foreach ($classnames as $classname) {
232
-                $payment_methods[] = $this->payment_method_class_from_type($classname);
233
-            }
234
-            return $payment_methods;
235
-        }
236
-        return array_keys($this->_payment_method_types);
237
-    }
238
-
239
-
240
-    /**
241
-     * Gets an object of each payment method type, none of which are bound to a
242
-     * payment method instance
243
-     *
244
-     * @param boolean $force_recheck whether to force re-checking for new payment method types
245
-     * @return EE_PMT_Base[]
246
-     */
247
-    public function payment_method_types($force_recheck = false)
248
-    {
249
-        if ($force_recheck || empty($this->payment_method_objects)) {
250
-            $this->maybe_register_payment_methods($force_recheck);
251
-            foreach ($this->payment_method_type_names(true) as $classname) {
252
-                if (! isset($this->payment_method_objects[ $classname ])) {
253
-                    $this->payment_method_objects[ $classname ] = new $classname();
254
-                }
255
-            }
256
-        }
257
-        return $this->payment_method_objects;
258
-    }
259
-
260
-
261
-    /**
262
-     * Changes the payment method's class name into the payment method type's name
263
-     * (as used on the payment method's table's PMD_type field)
264
-     *
265
-     * @param string $classname
266
-     * @return string
267
-     */
268
-    public function payment_method_type_sans_class_prefix($classname)
269
-    {
270
-        return str_replace('EE_PMT_', '', $classname);
271
-    }
272
-
273
-
274
-    /**
275
-     * Does the opposite of payment-method_type_sans_prefix
276
-     *
277
-     * @param string $type
278
-     * @return string
279
-     */
280
-    public function payment_method_class_from_type($type)
281
-    {
282
-        return 'EE_PMT_' . $type;
283
-    }
284
-
285
-
286
-    /**
287
-     * Activates a payment method of the given type.
288
-     *
289
-     * @param string $payment_method_type the PMT_type; for EE_PMT_Invoice this would be 'Invoice'
290
-     * @return EE_Payment_Method
291
-     * @throws InvalidDataTypeException
292
-     * @throws EE_Error
293
-     */
294
-    public function activate_a_payment_method_of_type($payment_method_type)
295
-    {
296
-        $this->maybe_register_payment_methods();
297
-        $payment_method = EEM_Payment_Method::instance()->get_one_of_type($payment_method_type);
298
-        if (! $payment_method instanceof EE_Payment_Method) {
299
-            $pm_type_class = $this->payment_method_class_from_type($payment_method_type);
300
-            if (class_exists($pm_type_class)) {
301
-                /** @var $pm_type_obj EE_PMT_Base */
302
-                $pm_type_obj = new $pm_type_class();
303
-                $payment_method = EEM_Payment_Method::instance()->get_one_by_slug($pm_type_obj->system_name());
304
-                if (! $payment_method) {
305
-                    $payment_method = $this->create_payment_method_of_type($pm_type_obj);
306
-                }
307
-                $payment_method->set_type($payment_method_type);
308
-                $this->initialize_payment_method($payment_method);
309
-            } else {
310
-                throw new EE_Error(
311
-                    sprintf(
312
-                        esc_html__(
313
-                            'There is no payment method of type %1$s, so it could not be activated',
314
-                            'event_espresso'
315
-                        ),
316
-                        $pm_type_class
317
-                    )
318
-                );
319
-            }
320
-        }
321
-        $payment_method->set_active();
322
-        $payment_method->save();
323
-        /** @type EE_Message_Resource_Manager $message_resource_manager */
324
-        // if this was the invoice message type, make sure users can view their invoices
325
-        if (
326
-            $payment_method->type() === 'Invoice'
327
-            && (
328
-            ! EEH_MSG_Template::is_mt_active('invoice')
329
-            )
330
-        ) {
331
-            $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
332
-            /** @type EE_Message_Resource_Manager $message_resource_manager */
333
-            $message_resource_manager->ensure_message_type_is_active('invoice', 'html');
334
-            new PersistentAdminNotice(
335
-                'invoice_pm_requirements_notice',
336
-                sprintf(
337
-                    esc_html__(
338
-                        'The Invoice payment method has been activated. It requires the %1$sinvoice message%2$s type to be active, so it was automatically activated for you.',
339
-                        'event_espresso'
340
-                    ),
341
-                    '<a href="' . admin_url('admin.php?page=espresso_messages&action=settings') . '">',
342
-                    '</a>'
343
-                ),
344
-                true
345
-            );
346
-        }
347
-        return $payment_method;
348
-    }
349
-
350
-
351
-    /**
352
-     * Creates a payment method of the specified type. Does not save it.
353
-     *
354
-     * @global WP_User    $current_user
355
-     * @param EE_PMT_Base $pm_type_obj
356
-     * @return EE_Payment_Method
357
-     * @throws EE_Error
358
-     */
359
-    public function create_payment_method_of_type($pm_type_obj)
360
-    {
361
-        global $current_user;
362
-        $payment_method = EE_Payment_Method::new_instance(
363
-            array(
364
-                'PMD_type'       => $pm_type_obj->system_name(),
365
-                'PMD_name'       => $pm_type_obj->defaultFrontendName(),
366
-                'PMD_admin_name' => $pm_type_obj->pretty_name(),
367
-                'PMD_slug'       => $pm_type_obj->system_name(),// automatically converted to slug
368
-                'PMD_wp_user'    => $current_user->ID,
369
-                'PMD_order'      => EEM_Payment_Method::instance()->count(
370
-                    array(array('PMD_type' => array('!=', 'Admin_Only')))
371
-                ) * 10,
372
-            )
373
-        );
374
-        return $payment_method;
375
-    }
376
-
377
-
378
-    /**
379
-     * Sets the initial payment method properties (including extra meta)
380
-     *
381
-     * @param EE_Payment_Method $payment_method
382
-     * @return EE_Payment_Method
383
-     * @throws EE_Error
384
-     */
385
-    public function initialize_payment_method($payment_method)
386
-    {
387
-        $pm_type_obj = $payment_method->type_obj();
388
-        $payment_method->set_description($pm_type_obj->default_description());
389
-        if (! $payment_method->button_url()) {
390
-            $payment_method->set_button_url($pm_type_obj->default_button_url());
391
-        }
392
-        // now add setup its default extra meta properties
393
-        $extra_metas = $pm_type_obj->settings_form()->extra_meta_inputs();
394
-        if (! empty($extra_metas)) {
395
-            // verify the payment method has an ID before adding extra meta
396
-            if (! $payment_method->ID()) {
397
-                $payment_method->save();
398
-            }
399
-            foreach ($extra_metas as $meta_name => $input) {
400
-                $payment_method->update_extra_meta($meta_name, $input->raw_value());
401
-            }
402
-        }
403
-        return $payment_method;
404
-    }
405
-
406
-
407
-    /**
408
-     * Makes sure the payment method is related to the specified payment method
409
-     *
410
-     * @deprecated in 4.9.40 because the currency payment method table is being deprecated
411
-     * @param EE_Payment_Method $payment_method
412
-     * @return EE_Payment_Method
413
-     * @throws EE_Error
414
-     */
415
-    public function set_usable_currencies_on_payment_method($payment_method)
416
-    {
417
-        EE_Error::doing_it_wrong(
418
-            'EE_Payment_Method_Manager::set_usable_currencies_on_payment_method',
419
-            esc_html__(
420
-                'We no longer define what currencies are usable by payment methods. Its not used nor efficient.',
421
-                'event_espresso'
422
-            ),
423
-            '4.9.40'
424
-        );
425
-        return $payment_method;
426
-    }
427
-
428
-
429
-    /**
430
-     * Deactivates a payment method of the given payment method slug.
431
-     *
432
-     * @param string $payment_method_slug The slug for the payment method to deactivate.
433
-     * @return int count of rows updated.
434
-     * @throws EE_Error
435
-     */
436
-    public function deactivate_payment_method($payment_method_slug)
437
-    {
438
-        EE_Log::instance()->log(
439
-            __FILE__,
440
-            __FUNCTION__,
441
-            sprintf(
442
-                esc_html__(
443
-                    'Payment method with slug %1$s is being deactivated by site admin',
444
-                    'event_espresso'
445
-                ),
446
-                $payment_method_slug
447
-            ),
448
-            'payment_method_change'
449
-        );
450
-        $count_updated = EEM_Payment_Method::instance()->update(
451
-            array('PMD_scope' => array()),
452
-            array(array('PMD_slug' => $payment_method_slug))
453
-        );
454
-        do_action(
455
-            'AHEE__EE_Payment_Method_Manager__deactivate_payment_method__after_deactivating_payment_method',
456
-            $payment_method_slug,
457
-            $count_updated
458
-        );
459
-        return $count_updated;
460
-    }
461
-
462
-
463
-    /**
464
-     * initializes payment method access caps via EE_Capabilities::init_role_caps()
465
-     * upon EE_Payment_Method_Manager construction
466
-     *
467
-     * @throws EE_Error
468
-     * @throws DomainException
469
-     */
470
-    protected function initializePaymentMethodCaps()
471
-    {
472
-        // don't do this twice
473
-        if ($this->payment_method_caps_initialized) {
474
-            return;
475
-        }
476
-        EE_Capabilities::instance()->addCaps(
477
-            $this->getPaymentMethodCaps()
478
-        );
479
-        $this->payment_method_caps_initialized = true;
480
-    }
481
-
482
-
483
-    /**
484
-     * array  of dynamic payment method access caps.
485
-     * at the time of writing, october 20 2014, these are the caps added:
486
-     *  ee_payment_method_admin_only
487
-     *  ee_payment_method_aim
488
-     *  ee_payment_method_bank
489
-     *  ee_payment_method_check
490
-     *  ee_payment_method_invoice
491
-     *  ee_payment_method_mijireh
492
-     *  ee_payment_method_paypal_pro
493
-     *  ee_payment_method_paypal_standard
494
-     * Any other payment methods added to core or via addons will also get
495
-     * their related capability automatically added too, so long as they are
496
-     * registered properly using EE_Register_Payment_Method::register()
497
-     *
498
-     * @return array
499
-     * @throws DomainException
500
-     */
501
-    protected function getPaymentMethodCaps()
502
-    {
503
-        $caps = array();
504
-        foreach ($this->payment_method_type_names() as $payment_method_name) {
505
-            $caps = $this->addPaymentMethodCap($payment_method_name, $caps);
506
-        }
507
-        return $caps;
508
-    }
509
-
510
-
511
-    /**
512
-     * @param string $payment_method_name
513
-     * @param array  $payment_method_caps
514
-     * @param string $role
515
-     * @return array
516
-     * @throws DomainException
517
-     */
518
-    public function addPaymentMethodCap($payment_method_name, array $payment_method_caps, $role = 'administrator')
519
-    {
520
-        if (empty($payment_method_name)) {
521
-            throw new DomainException(
522
-                esc_html__(
523
-                    'The name of a payment method must be specified to add capabilities.',
524
-                    'event_espresso'
525
-                )
526
-            );
527
-        }
528
-        if (empty($role)) {
529
-            throw new DomainException(
530
-                sprintf(
531
-                    esc_html__(
532
-                        'No role was supplied while trying to add capabilities for the %1$s payment method.',
533
-                        'event_espresso'
534
-                    ),
535
-                    $payment_method_name
536
-                )
537
-            );
538
-        }
539
-        if (! isset($payment_method_caps[ $role ])) {
540
-            $payment_method_caps[ $role ] = array();
541
-        }
542
-        $payment_method_caps[ $role ][] = EE_Payment_Method_Manager::CAPABILITIES_PREFIX
543
-                                          . strtolower($payment_method_name);
544
-        return $payment_method_caps;
545
-    }
546
-
547
-
548
-    /**
549
-     * callback for FHEE__EE_Capabilities__init_role_caps__caps_map filter
550
-     * to add dynamic payment method access caps when capabilities are reset
551
-     * (or if that filter is called and PM caps are not already set)
552
-     *
553
-     * @param array $caps capabilities being filtered
554
-     * @param bool  $reset
555
-     * @return array
556
-     * @throws DomainException
557
-     */
558
-    public function addPaymentMethodCapsDuringReset(array $caps, $reset = false)
559
-    {
560
-        if ($reset || ! $this->payment_method_caps_initialized) {
561
-            $this->payment_method_caps_initialized = true;
562
-            $caps = array_merge_recursive($caps, $this->getPaymentMethodCaps());
563
-        }
564
-        return $caps;
565
-    }
566
-
567
-
568
-    /**
569
-     * @deprecated 4.9.42
570
-     * @param $caps
571
-     * @return mixed
572
-     */
573
-    public function add_payment_method_caps($caps)
574
-    {
575
-        return $caps;
576
-    }
20
+	/**
21
+	 * prefix added to all payment method capabilities names
22
+	 */
23
+	const   CAPABILITIES_PREFIX = 'ee_payment_method_';
24
+
25
+	/**
26
+	 * @var EE_Payment_Method_Manager $_instance
27
+	 */
28
+	private static $_instance;
29
+
30
+	/**
31
+	 * @var boolean
32
+	 */
33
+	protected $payment_method_caps_initialized = false;
34
+
35
+	/**
36
+	 * @var array keys are class names without 'EE_PMT_', values are their filepaths
37
+	 */
38
+	protected $_payment_method_types = array();
39
+
40
+	/**
41
+	 * @var EE_PMT_Base[]
42
+	 */
43
+	protected $payment_method_objects = array();
44
+
45
+
46
+	/**
47
+	 * EE_Payment_Method_Manager constructor.
48
+	 *
49
+	 * @throws EE_Error
50
+	 * @throws DomainException
51
+	 */
52
+	public function __construct()
53
+	{
54
+		// if in admin lets ensure caps are set.
55
+		if (is_admin()) {
56
+			$this->_register_payment_methods();
57
+			// set them immediately
58
+			$this->initializePaymentMethodCaps();
59
+			// plus any time they get reset
60
+			add_filter(
61
+				'FHEE__EE_Capabilities__addCaps__capabilities_to_add',
62
+				array($this, 'addPaymentMethodCapsDuringReset')
63
+			);
64
+		}
65
+	}
66
+
67
+
68
+	/**
69
+	 * @singleton method used to instantiate class object
70
+	 * @return EE_Payment_Method_Manager instance
71
+	 * @throws DomainException
72
+	 * @throws EE_Error
73
+	 */
74
+	public static function instance()
75
+	{
76
+		// check if class object is instantiated, and instantiated properly
77
+		if (! self::$_instance instanceof EE_Payment_Method_Manager) {
78
+			EE_Registry::instance()->load_lib('PMT_Base');
79
+			self::$_instance = new self();
80
+		}
81
+		return self::$_instance;
82
+	}
83
+
84
+
85
+	/**
86
+	 * Resets the instance and returns a new one
87
+	 *
88
+	 * @return EE_Payment_Method_Manager
89
+	 * @throws DomainException
90
+	 * @throws EE_Error
91
+	 */
92
+	public static function reset()
93
+	{
94
+		self::$_instance = null;
95
+		return self::instance();
96
+	}
97
+
98
+
99
+	/**
100
+	 * If necessary, re-register payment methods
101
+	 *
102
+	 * @param boolean $force_recheck whether to recheck for payment method types,
103
+	 *                               or just re-use the PMTs we found last time we checked during this request (if
104
+	 *                               we have not yet checked during this request, then we need to check anyways)
105
+	 */
106
+	public function maybe_register_payment_methods($force_recheck = false)
107
+	{
108
+		if (! $this->_payment_method_types || $force_recheck) {
109
+			$this->_register_payment_methods();
110
+		}
111
+	}
112
+
113
+
114
+	/**
115
+	 * register_payment_methods
116
+	 *
117
+	 * @return array
118
+	 */
119
+	protected function _register_payment_methods()
120
+	{
121
+		// grab list of installed modules
122
+		$pm_to_register = glob(EE_PAYMENT_METHODS . '*', GLOB_ONLYDIR);
123
+		// filter list of modules to register
124
+		$pm_to_register = apply_filters(
125
+			'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register',
126
+			$pm_to_register
127
+		);
128
+		// remove any duplicates if that should happen for some reason
129
+		$pm_to_register = array_unique($pm_to_register);
130
+		// loop through folders
131
+		foreach ($pm_to_register as $pm_path) {
132
+			$this->register_payment_method($pm_path);
133
+		}
134
+		do_action('FHEE__EE_Payment_Method_Manager__register_payment_methods__registered_payment_methods');
135
+		// filter list of installed modules
136
+		// keep them organized alphabetically by the payment method type's name
137
+		ksort($this->_payment_method_types);
138
+		return apply_filters(
139
+			'FHEE__EE_Payment_Method_Manager__register_payment_methods__installed_payment_methods',
140
+			$this->_payment_method_types
141
+		);
142
+	}
143
+
144
+
145
+	/**
146
+	 * register_payment_method- makes core aware of this payment method
147
+	 *
148
+	 * @param string $payment_method_path - full path up to and including payment method folder
149
+	 * @return boolean
150
+	 */
151
+	public function register_payment_method($payment_method_path = '')
152
+	{
153
+		do_action('AHEE__EE_Payment_Method_Manager__register_payment_method__begin', $payment_method_path);
154
+		$module_ext = '.pm.php';
155
+		// make all separators match
156
+		$payment_method_path = rtrim(str_replace('/\\', '/', $payment_method_path), '/');
157
+		// grab and sanitize module name
158
+		$module_dir = basename($payment_method_path);
159
+		// create class name from module directory name
160
+		$module = str_replace(array('_', ' '), array(' ', '_'), $module_dir);
161
+		// add class prefix
162
+		$module_class = 'EE_PMT_' . $module;
163
+		// does the module exist ?
164
+		if (! is_readable($payment_method_path . '/' . $module_class . $module_ext)) {
165
+			$msg = sprintf(
166
+				esc_html__(
167
+					'The requested %s payment method file could not be found or is not readable due to file permissions.',
168
+					'event_espresso'
169
+				),
170
+				$module
171
+			);
172
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
173
+			return false;
174
+		}
175
+		// load the module class file
176
+		require_once($payment_method_path . '/' . $module_class . $module_ext);
177
+		// verify that class exists
178
+		if (! class_exists($module_class)) {
179
+			$msg = sprintf(
180
+				esc_html__('The requested %s module class does not exist.', 'event_espresso'),
181
+				$module_class
182
+			);
183
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
184
+			return false;
185
+		}
186
+		// add to array of registered modules
187
+		$this->_payment_method_types[ $module ] = $payment_method_path . '/' . $module_class . $module_ext;
188
+		ksort($this->_payment_method_types);
189
+		return true;
190
+	}
191
+
192
+
193
+	/**
194
+	 * Checks if a payment method has been registered, and if so includes it
195
+	 *
196
+	 * @param string  $payment_method_name like 'PayPal_Pro', (ie class name without the prefix 'EEPM_')
197
+	 * @param boolean $force_recheck       whether to force re-checking for new payment method types
198
+	 * @return boolean
199
+	 */
200
+	public function payment_method_type_exists($payment_method_name, $force_recheck = false)
201
+	{
202
+		if (
203
+			$force_recheck
204
+			|| ! is_array($this->_payment_method_types)
205
+			|| ! isset($this->_payment_method_types[ $payment_method_name ])
206
+		) {
207
+			$this->maybe_register_payment_methods($force_recheck);
208
+		}
209
+		if (isset($this->_payment_method_types[ $payment_method_name ])) {
210
+			require_once($this->_payment_method_types[ $payment_method_name ]);
211
+			return true;
212
+		}
213
+		return false;
214
+	}
215
+
216
+
217
+	/**
218
+	 * Returns all the class names of the various payment method types
219
+	 *
220
+	 * @param boolean $with_prefixes TRUE: get payment method type class names; false just their 'names'
221
+	 *                               (what you'd find in wp_esp_payment_method.PMD_type)
222
+	 * @param boolean $force_recheck whether to force re-checking for new payment method types
223
+	 * @return array
224
+	 */
225
+	public function payment_method_type_names($with_prefixes = false, $force_recheck = false)
226
+	{
227
+		$this->maybe_register_payment_methods($force_recheck);
228
+		if ($with_prefixes) {
229
+			$classnames = array_keys($this->_payment_method_types);
230
+			$payment_methods = array();
231
+			foreach ($classnames as $classname) {
232
+				$payment_methods[] = $this->payment_method_class_from_type($classname);
233
+			}
234
+			return $payment_methods;
235
+		}
236
+		return array_keys($this->_payment_method_types);
237
+	}
238
+
239
+
240
+	/**
241
+	 * Gets an object of each payment method type, none of which are bound to a
242
+	 * payment method instance
243
+	 *
244
+	 * @param boolean $force_recheck whether to force re-checking for new payment method types
245
+	 * @return EE_PMT_Base[]
246
+	 */
247
+	public function payment_method_types($force_recheck = false)
248
+	{
249
+		if ($force_recheck || empty($this->payment_method_objects)) {
250
+			$this->maybe_register_payment_methods($force_recheck);
251
+			foreach ($this->payment_method_type_names(true) as $classname) {
252
+				if (! isset($this->payment_method_objects[ $classname ])) {
253
+					$this->payment_method_objects[ $classname ] = new $classname();
254
+				}
255
+			}
256
+		}
257
+		return $this->payment_method_objects;
258
+	}
259
+
260
+
261
+	/**
262
+	 * Changes the payment method's class name into the payment method type's name
263
+	 * (as used on the payment method's table's PMD_type field)
264
+	 *
265
+	 * @param string $classname
266
+	 * @return string
267
+	 */
268
+	public function payment_method_type_sans_class_prefix($classname)
269
+	{
270
+		return str_replace('EE_PMT_', '', $classname);
271
+	}
272
+
273
+
274
+	/**
275
+	 * Does the opposite of payment-method_type_sans_prefix
276
+	 *
277
+	 * @param string $type
278
+	 * @return string
279
+	 */
280
+	public function payment_method_class_from_type($type)
281
+	{
282
+		return 'EE_PMT_' . $type;
283
+	}
284
+
285
+
286
+	/**
287
+	 * Activates a payment method of the given type.
288
+	 *
289
+	 * @param string $payment_method_type the PMT_type; for EE_PMT_Invoice this would be 'Invoice'
290
+	 * @return EE_Payment_Method
291
+	 * @throws InvalidDataTypeException
292
+	 * @throws EE_Error
293
+	 */
294
+	public function activate_a_payment_method_of_type($payment_method_type)
295
+	{
296
+		$this->maybe_register_payment_methods();
297
+		$payment_method = EEM_Payment_Method::instance()->get_one_of_type($payment_method_type);
298
+		if (! $payment_method instanceof EE_Payment_Method) {
299
+			$pm_type_class = $this->payment_method_class_from_type($payment_method_type);
300
+			if (class_exists($pm_type_class)) {
301
+				/** @var $pm_type_obj EE_PMT_Base */
302
+				$pm_type_obj = new $pm_type_class();
303
+				$payment_method = EEM_Payment_Method::instance()->get_one_by_slug($pm_type_obj->system_name());
304
+				if (! $payment_method) {
305
+					$payment_method = $this->create_payment_method_of_type($pm_type_obj);
306
+				}
307
+				$payment_method->set_type($payment_method_type);
308
+				$this->initialize_payment_method($payment_method);
309
+			} else {
310
+				throw new EE_Error(
311
+					sprintf(
312
+						esc_html__(
313
+							'There is no payment method of type %1$s, so it could not be activated',
314
+							'event_espresso'
315
+						),
316
+						$pm_type_class
317
+					)
318
+				);
319
+			}
320
+		}
321
+		$payment_method->set_active();
322
+		$payment_method->save();
323
+		/** @type EE_Message_Resource_Manager $message_resource_manager */
324
+		// if this was the invoice message type, make sure users can view their invoices
325
+		if (
326
+			$payment_method->type() === 'Invoice'
327
+			&& (
328
+			! EEH_MSG_Template::is_mt_active('invoice')
329
+			)
330
+		) {
331
+			$message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
332
+			/** @type EE_Message_Resource_Manager $message_resource_manager */
333
+			$message_resource_manager->ensure_message_type_is_active('invoice', 'html');
334
+			new PersistentAdminNotice(
335
+				'invoice_pm_requirements_notice',
336
+				sprintf(
337
+					esc_html__(
338
+						'The Invoice payment method has been activated. It requires the %1$sinvoice message%2$s type to be active, so it was automatically activated for you.',
339
+						'event_espresso'
340
+					),
341
+					'<a href="' . admin_url('admin.php?page=espresso_messages&action=settings') . '">',
342
+					'</a>'
343
+				),
344
+				true
345
+			);
346
+		}
347
+		return $payment_method;
348
+	}
349
+
350
+
351
+	/**
352
+	 * Creates a payment method of the specified type. Does not save it.
353
+	 *
354
+	 * @global WP_User    $current_user
355
+	 * @param EE_PMT_Base $pm_type_obj
356
+	 * @return EE_Payment_Method
357
+	 * @throws EE_Error
358
+	 */
359
+	public function create_payment_method_of_type($pm_type_obj)
360
+	{
361
+		global $current_user;
362
+		$payment_method = EE_Payment_Method::new_instance(
363
+			array(
364
+				'PMD_type'       => $pm_type_obj->system_name(),
365
+				'PMD_name'       => $pm_type_obj->defaultFrontendName(),
366
+				'PMD_admin_name' => $pm_type_obj->pretty_name(),
367
+				'PMD_slug'       => $pm_type_obj->system_name(),// automatically converted to slug
368
+				'PMD_wp_user'    => $current_user->ID,
369
+				'PMD_order'      => EEM_Payment_Method::instance()->count(
370
+					array(array('PMD_type' => array('!=', 'Admin_Only')))
371
+				) * 10,
372
+			)
373
+		);
374
+		return $payment_method;
375
+	}
376
+
377
+
378
+	/**
379
+	 * Sets the initial payment method properties (including extra meta)
380
+	 *
381
+	 * @param EE_Payment_Method $payment_method
382
+	 * @return EE_Payment_Method
383
+	 * @throws EE_Error
384
+	 */
385
+	public function initialize_payment_method($payment_method)
386
+	{
387
+		$pm_type_obj = $payment_method->type_obj();
388
+		$payment_method->set_description($pm_type_obj->default_description());
389
+		if (! $payment_method->button_url()) {
390
+			$payment_method->set_button_url($pm_type_obj->default_button_url());
391
+		}
392
+		// now add setup its default extra meta properties
393
+		$extra_metas = $pm_type_obj->settings_form()->extra_meta_inputs();
394
+		if (! empty($extra_metas)) {
395
+			// verify the payment method has an ID before adding extra meta
396
+			if (! $payment_method->ID()) {
397
+				$payment_method->save();
398
+			}
399
+			foreach ($extra_metas as $meta_name => $input) {
400
+				$payment_method->update_extra_meta($meta_name, $input->raw_value());
401
+			}
402
+		}
403
+		return $payment_method;
404
+	}
405
+
406
+
407
+	/**
408
+	 * Makes sure the payment method is related to the specified payment method
409
+	 *
410
+	 * @deprecated in 4.9.40 because the currency payment method table is being deprecated
411
+	 * @param EE_Payment_Method $payment_method
412
+	 * @return EE_Payment_Method
413
+	 * @throws EE_Error
414
+	 */
415
+	public function set_usable_currencies_on_payment_method($payment_method)
416
+	{
417
+		EE_Error::doing_it_wrong(
418
+			'EE_Payment_Method_Manager::set_usable_currencies_on_payment_method',
419
+			esc_html__(
420
+				'We no longer define what currencies are usable by payment methods. Its not used nor efficient.',
421
+				'event_espresso'
422
+			),
423
+			'4.9.40'
424
+		);
425
+		return $payment_method;
426
+	}
427
+
428
+
429
+	/**
430
+	 * Deactivates a payment method of the given payment method slug.
431
+	 *
432
+	 * @param string $payment_method_slug The slug for the payment method to deactivate.
433
+	 * @return int count of rows updated.
434
+	 * @throws EE_Error
435
+	 */
436
+	public function deactivate_payment_method($payment_method_slug)
437
+	{
438
+		EE_Log::instance()->log(
439
+			__FILE__,
440
+			__FUNCTION__,
441
+			sprintf(
442
+				esc_html__(
443
+					'Payment method with slug %1$s is being deactivated by site admin',
444
+					'event_espresso'
445
+				),
446
+				$payment_method_slug
447
+			),
448
+			'payment_method_change'
449
+		);
450
+		$count_updated = EEM_Payment_Method::instance()->update(
451
+			array('PMD_scope' => array()),
452
+			array(array('PMD_slug' => $payment_method_slug))
453
+		);
454
+		do_action(
455
+			'AHEE__EE_Payment_Method_Manager__deactivate_payment_method__after_deactivating_payment_method',
456
+			$payment_method_slug,
457
+			$count_updated
458
+		);
459
+		return $count_updated;
460
+	}
461
+
462
+
463
+	/**
464
+	 * initializes payment method access caps via EE_Capabilities::init_role_caps()
465
+	 * upon EE_Payment_Method_Manager construction
466
+	 *
467
+	 * @throws EE_Error
468
+	 * @throws DomainException
469
+	 */
470
+	protected function initializePaymentMethodCaps()
471
+	{
472
+		// don't do this twice
473
+		if ($this->payment_method_caps_initialized) {
474
+			return;
475
+		}
476
+		EE_Capabilities::instance()->addCaps(
477
+			$this->getPaymentMethodCaps()
478
+		);
479
+		$this->payment_method_caps_initialized = true;
480
+	}
481
+
482
+
483
+	/**
484
+	 * array  of dynamic payment method access caps.
485
+	 * at the time of writing, october 20 2014, these are the caps added:
486
+	 *  ee_payment_method_admin_only
487
+	 *  ee_payment_method_aim
488
+	 *  ee_payment_method_bank
489
+	 *  ee_payment_method_check
490
+	 *  ee_payment_method_invoice
491
+	 *  ee_payment_method_mijireh
492
+	 *  ee_payment_method_paypal_pro
493
+	 *  ee_payment_method_paypal_standard
494
+	 * Any other payment methods added to core or via addons will also get
495
+	 * their related capability automatically added too, so long as they are
496
+	 * registered properly using EE_Register_Payment_Method::register()
497
+	 *
498
+	 * @return array
499
+	 * @throws DomainException
500
+	 */
501
+	protected function getPaymentMethodCaps()
502
+	{
503
+		$caps = array();
504
+		foreach ($this->payment_method_type_names() as $payment_method_name) {
505
+			$caps = $this->addPaymentMethodCap($payment_method_name, $caps);
506
+		}
507
+		return $caps;
508
+	}
509
+
510
+
511
+	/**
512
+	 * @param string $payment_method_name
513
+	 * @param array  $payment_method_caps
514
+	 * @param string $role
515
+	 * @return array
516
+	 * @throws DomainException
517
+	 */
518
+	public function addPaymentMethodCap($payment_method_name, array $payment_method_caps, $role = 'administrator')
519
+	{
520
+		if (empty($payment_method_name)) {
521
+			throw new DomainException(
522
+				esc_html__(
523
+					'The name of a payment method must be specified to add capabilities.',
524
+					'event_espresso'
525
+				)
526
+			);
527
+		}
528
+		if (empty($role)) {
529
+			throw new DomainException(
530
+				sprintf(
531
+					esc_html__(
532
+						'No role was supplied while trying to add capabilities for the %1$s payment method.',
533
+						'event_espresso'
534
+					),
535
+					$payment_method_name
536
+				)
537
+			);
538
+		}
539
+		if (! isset($payment_method_caps[ $role ])) {
540
+			$payment_method_caps[ $role ] = array();
541
+		}
542
+		$payment_method_caps[ $role ][] = EE_Payment_Method_Manager::CAPABILITIES_PREFIX
543
+										  . strtolower($payment_method_name);
544
+		return $payment_method_caps;
545
+	}
546
+
547
+
548
+	/**
549
+	 * callback for FHEE__EE_Capabilities__init_role_caps__caps_map filter
550
+	 * to add dynamic payment method access caps when capabilities are reset
551
+	 * (or if that filter is called and PM caps are not already set)
552
+	 *
553
+	 * @param array $caps capabilities being filtered
554
+	 * @param bool  $reset
555
+	 * @return array
556
+	 * @throws DomainException
557
+	 */
558
+	public function addPaymentMethodCapsDuringReset(array $caps, $reset = false)
559
+	{
560
+		if ($reset || ! $this->payment_method_caps_initialized) {
561
+			$this->payment_method_caps_initialized = true;
562
+			$caps = array_merge_recursive($caps, $this->getPaymentMethodCaps());
563
+		}
564
+		return $caps;
565
+	}
566
+
567
+
568
+	/**
569
+	 * @deprecated 4.9.42
570
+	 * @param $caps
571
+	 * @return mixed
572
+	 */
573
+	public function add_payment_method_caps($caps)
574
+	{
575
+		return $caps;
576
+	}
577 577
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
     public static function instance()
75 75
     {
76 76
         // check if class object is instantiated, and instantiated properly
77
-        if (! self::$_instance instanceof EE_Payment_Method_Manager) {
77
+        if ( ! self::$_instance instanceof EE_Payment_Method_Manager) {
78 78
             EE_Registry::instance()->load_lib('PMT_Base');
79 79
             self::$_instance = new self();
80 80
         }
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
      */
106 106
     public function maybe_register_payment_methods($force_recheck = false)
107 107
     {
108
-        if (! $this->_payment_method_types || $force_recheck) {
108
+        if ( ! $this->_payment_method_types || $force_recheck) {
109 109
             $this->_register_payment_methods();
110 110
         }
111 111
     }
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
     protected function _register_payment_methods()
120 120
     {
121 121
         // grab list of installed modules
122
-        $pm_to_register = glob(EE_PAYMENT_METHODS . '*', GLOB_ONLYDIR);
122
+        $pm_to_register = glob(EE_PAYMENT_METHODS.'*', GLOB_ONLYDIR);
123 123
         // filter list of modules to register
124 124
         $pm_to_register = apply_filters(
125 125
             'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register',
@@ -159,9 +159,9 @@  discard block
 block discarded – undo
159 159
         // create class name from module directory name
160 160
         $module = str_replace(array('_', ' '), array(' ', '_'), $module_dir);
161 161
         // add class prefix
162
-        $module_class = 'EE_PMT_' . $module;
162
+        $module_class = 'EE_PMT_'.$module;
163 163
         // does the module exist ?
164
-        if (! is_readable($payment_method_path . '/' . $module_class . $module_ext)) {
164
+        if ( ! is_readable($payment_method_path.'/'.$module_class.$module_ext)) {
165 165
             $msg = sprintf(
166 166
                 esc_html__(
167 167
                     'The requested %s payment method file could not be found or is not readable due to file permissions.',
@@ -169,22 +169,22 @@  discard block
 block discarded – undo
169 169
                 ),
170 170
                 $module
171 171
             );
172
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
172
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
173 173
             return false;
174 174
         }
175 175
         // load the module class file
176
-        require_once($payment_method_path . '/' . $module_class . $module_ext);
176
+        require_once($payment_method_path.'/'.$module_class.$module_ext);
177 177
         // verify that class exists
178
-        if (! class_exists($module_class)) {
178
+        if ( ! class_exists($module_class)) {
179 179
             $msg = sprintf(
180 180
                 esc_html__('The requested %s module class does not exist.', 'event_espresso'),
181 181
                 $module_class
182 182
             );
183
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
183
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
184 184
             return false;
185 185
         }
186 186
         // add to array of registered modules
187
-        $this->_payment_method_types[ $module ] = $payment_method_path . '/' . $module_class . $module_ext;
187
+        $this->_payment_method_types[$module] = $payment_method_path.'/'.$module_class.$module_ext;
188 188
         ksort($this->_payment_method_types);
189 189
         return true;
190 190
     }
@@ -202,12 +202,12 @@  discard block
 block discarded – undo
202 202
         if (
203 203
             $force_recheck
204 204
             || ! is_array($this->_payment_method_types)
205
-            || ! isset($this->_payment_method_types[ $payment_method_name ])
205
+            || ! isset($this->_payment_method_types[$payment_method_name])
206 206
         ) {
207 207
             $this->maybe_register_payment_methods($force_recheck);
208 208
         }
209
-        if (isset($this->_payment_method_types[ $payment_method_name ])) {
210
-            require_once($this->_payment_method_types[ $payment_method_name ]);
209
+        if (isset($this->_payment_method_types[$payment_method_name])) {
210
+            require_once($this->_payment_method_types[$payment_method_name]);
211 211
             return true;
212 212
         }
213 213
         return false;
@@ -249,8 +249,8 @@  discard block
 block discarded – undo
249 249
         if ($force_recheck || empty($this->payment_method_objects)) {
250 250
             $this->maybe_register_payment_methods($force_recheck);
251 251
             foreach ($this->payment_method_type_names(true) as $classname) {
252
-                if (! isset($this->payment_method_objects[ $classname ])) {
253
-                    $this->payment_method_objects[ $classname ] = new $classname();
252
+                if ( ! isset($this->payment_method_objects[$classname])) {
253
+                    $this->payment_method_objects[$classname] = new $classname();
254 254
                 }
255 255
             }
256 256
         }
@@ -279,7 +279,7 @@  discard block
 block discarded – undo
279 279
      */
280 280
     public function payment_method_class_from_type($type)
281 281
     {
282
-        return 'EE_PMT_' . $type;
282
+        return 'EE_PMT_'.$type;
283 283
     }
284 284
 
285 285
 
@@ -295,13 +295,13 @@  discard block
 block discarded – undo
295 295
     {
296 296
         $this->maybe_register_payment_methods();
297 297
         $payment_method = EEM_Payment_Method::instance()->get_one_of_type($payment_method_type);
298
-        if (! $payment_method instanceof EE_Payment_Method) {
298
+        if ( ! $payment_method instanceof EE_Payment_Method) {
299 299
             $pm_type_class = $this->payment_method_class_from_type($payment_method_type);
300 300
             if (class_exists($pm_type_class)) {
301 301
                 /** @var $pm_type_obj EE_PMT_Base */
302 302
                 $pm_type_obj = new $pm_type_class();
303 303
                 $payment_method = EEM_Payment_Method::instance()->get_one_by_slug($pm_type_obj->system_name());
304
-                if (! $payment_method) {
304
+                if ( ! $payment_method) {
305 305
                     $payment_method = $this->create_payment_method_of_type($pm_type_obj);
306 306
                 }
307 307
                 $payment_method->set_type($payment_method_type);
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
                         'The Invoice payment method has been activated. It requires the %1$sinvoice message%2$s type to be active, so it was automatically activated for you.',
339 339
                         'event_espresso'
340 340
                     ),
341
-                    '<a href="' . admin_url('admin.php?page=espresso_messages&action=settings') . '">',
341
+                    '<a href="'.admin_url('admin.php?page=espresso_messages&action=settings').'">',
342 342
                     '</a>'
343 343
                 ),
344 344
                 true
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
                 'PMD_type'       => $pm_type_obj->system_name(),
365 365
                 'PMD_name'       => $pm_type_obj->defaultFrontendName(),
366 366
                 'PMD_admin_name' => $pm_type_obj->pretty_name(),
367
-                'PMD_slug'       => $pm_type_obj->system_name(),// automatically converted to slug
367
+                'PMD_slug'       => $pm_type_obj->system_name(), // automatically converted to slug
368 368
                 'PMD_wp_user'    => $current_user->ID,
369 369
                 'PMD_order'      => EEM_Payment_Method::instance()->count(
370 370
                     array(array('PMD_type' => array('!=', 'Admin_Only')))
@@ -386,14 +386,14 @@  discard block
 block discarded – undo
386 386
     {
387 387
         $pm_type_obj = $payment_method->type_obj();
388 388
         $payment_method->set_description($pm_type_obj->default_description());
389
-        if (! $payment_method->button_url()) {
389
+        if ( ! $payment_method->button_url()) {
390 390
             $payment_method->set_button_url($pm_type_obj->default_button_url());
391 391
         }
392 392
         // now add setup its default extra meta properties
393 393
         $extra_metas = $pm_type_obj->settings_form()->extra_meta_inputs();
394
-        if (! empty($extra_metas)) {
394
+        if ( ! empty($extra_metas)) {
395 395
             // verify the payment method has an ID before adding extra meta
396
-            if (! $payment_method->ID()) {
396
+            if ( ! $payment_method->ID()) {
397 397
                 $payment_method->save();
398 398
             }
399 399
             foreach ($extra_metas as $meta_name => $input) {
@@ -536,10 +536,10 @@  discard block
 block discarded – undo
536 536
                 )
537 537
             );
538 538
         }
539
-        if (! isset($payment_method_caps[ $role ])) {
540
-            $payment_method_caps[ $role ] = array();
539
+        if ( ! isset($payment_method_caps[$role])) {
540
+            $payment_method_caps[$role] = array();
541 541
         }
542
-        $payment_method_caps[ $role ][] = EE_Payment_Method_Manager::CAPABILITIES_PREFIX
542
+        $payment_method_caps[$role][] = EE_Payment_Method_Manager::CAPABILITIES_PREFIX
543 543
                                           . strtolower($payment_method_name);
544 544
         return $payment_method_caps;
545 545
     }
Please login to merge, or discard this patch.
core/libraries/shortcodes/EE_Recipient_Details_Shortcodes.lib.php 2 patches
Indentation   +351 added lines, -351 removed lines patch added patch discarded remove patch
@@ -19,355 +19,355 @@
 block discarded – undo
19 19
 class EE_Recipient_Details_Shortcodes extends EE_Shortcodes
20 20
 {
21 21
 
22
-    protected $_recipient;
23
-
24
-    protected $_registrations_for_recipient;
25
-
26
-
27
-    protected function _init_props()
28
-    {
29
-        $this->label = esc_html__('Recipient Details Shortcodes', 'event_espresso');
30
-        $this->description = esc_html__('All shortcodes specific to recipient registration data', 'event_espresso');
31
-        $this->_shortcodes = array(
32
-            '[RECIPIENT_FNAME]'                  => esc_html__(
33
-                'Parses to the first name of the recipient for the message.',
34
-                'event_espresso'
35
-            ),
36
-            '[RECIPIENT_LNAME]'                  => esc_html__(
37
-                'Parses to the last name of the recipient for the message.',
38
-                'event_espresso'
39
-            ),
40
-            '[RECIPIENT_EMAIL]'                  => esc_html__(
41
-                'Parses to the email address of the recipient for the message.',
42
-                'event_espresso'
43
-            ),
44
-            '[RECIPIENT_REGISTRATION_ID]'        => esc_html__(
45
-                'Parses to the registration ID of the recipient for the message.',
46
-                'event_espresso'
47
-            ),
48
-            '[RECIPIENT_REGISTRATION_CODE]'      => esc_html__(
49
-                'Parses to the registration code of the recipient for the message.',
50
-                'event_espresso'
51
-            ),
52
-            '[RECIPIENT_EDIT_REGISTRATION_LINK]' => esc_html__(
53
-                'Parses to a link for frontend editing of the registration for the recipient.',
54
-                'event_espresso'
55
-            ),
56
-            '[RECIPIENT_PHONE_NUMBER]'           => esc_html__(
57
-                'The Phone Number for the recipient of the message.',
58
-                'event_espresso'
59
-            ),
60
-            '[RECIPIENT_ADDRESS]'                => esc_html__(
61
-                'The Address for the recipient of the message.',
62
-                'event_espresso'
63
-            ),
64
-            '[RECIPIENT_ADDRESS2]'               => esc_html__(
65
-                'Whatever was in the address 2 field for the recipient of the message.',
66
-                'event_espresso'
67
-            ),
68
-            '[RECIPIENT_CITY]'                   => esc_html__(
69
-                'The city for the recipient of the message.',
70
-                'event_espresso'
71
-            ),
72
-            '[RECIPIENT_ZIP_PC]'                 => esc_html__(
73
-                'The ZIP (or Postal) Code for the recipient of the message.',
74
-                'event_espresso'
75
-            ),
76
-            '[RECIPIENT_ADDRESS_STATE]'          => esc_html__(
77
-                'The state/province for the recipient of the message.',
78
-                'event_espresso'
79
-            ),
80
-            '[RECIPIENT_COUNTRY]'                => esc_html__(
81
-                'The country for the recipient of the message.',
82
-                'event_espresso'
83
-            ),
84
-            '[RECIPIENT_ANSWER_*]'               => esc_html__(
85
-                'This is a special dynamic shortcode.  After the "*", add the exact text of an existing question, and if there is an answer for that question for this recipient, then it will be output in place of this shortcode.',
86
-                'event_espresso'
87
-            ),
88
-            '[RECIPIENT_TOTAL_AMOUNT_PAID]'      => esc_html__(
89
-                'If a single registration related to the recipient is available, that is used to retrieve the total amount that has been paid for this recipient.  Otherwise the value of 0 is printed.',
90
-                'event_espresso'
91
-            ),
92
-        );
93
-    }
94
-
95
-
96
-    /**
97
-     * @access protected
98
-     * @param  string $shortcode the shortcode to be parsed.
99
-     * @return string parsed shortcode
100
-     */
101
-    protected function _parser($shortcode)
102
-    {
103
-
104
-        // make sure we end up with a copy of the EE_Messages_Addressee object
105
-        $this->_recipient = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null;
106
-        $this->_recipient = ! $this->_recipient instanceof EE_Messages_Addressee
107
-                            && is_array($this->_data)
108
-                            && isset($this->_data['data'])
109
-                            && $this->_data['data'] instanceof EE_Messages_Addressee
110
-            ? $this->_data['data']
111
-            :
112
-            $this->_recipient;
113
-        $this->_recipient = ! $this->_recipient instanceof EE_Messages_Addressee
114
-                            && ! empty($this->_extra_data['data'])
115
-                            && $this->_extra_data['data'] instanceof EE_Messages_Addressee
116
-            ? $this->_extra_data['data']
117
-            : $this->_recipient;
118
-
119
-        if (! $this->_recipient instanceof EE_Messages_Addressee) {
120
-            return '';
121
-        }
122
-
123
-        $attendee = $this->_recipient->att_obj;
124
-        if (! $attendee instanceof EE_Attendee) {
125
-            return '';
126
-        }
127
-
128
-        $this->_registrations_for_recipient = isset($this->_recipient->attendees[ $attendee->ID() ]['reg_objs'])
129
-            ? $this->_recipient->attendees[ $attendee->ID() ]['reg_objs']
130
-            : array();
131
-
132
-        switch ($shortcode) {
133
-            case '[RECIPIENT_FNAME]':
134
-                return $attendee->fname();
135
-                break;
136
-
137
-            case '[RECIPIENT_LNAME]':
138
-                return $attendee->lname();
139
-                break;
140
-
141
-            case '[RECIPIENT_EMAIL]':
142
-                return $attendee->email();
143
-                break;
144
-
145
-            case '[RECIPIENT_REGISTRATION_ID]':
146
-                if (! $this->_recipient->reg_obj instanceof EE_Registration) {
147
-                    return '';
148
-                }
149
-                return $this->_get_reg_id();
150
-                break;
151
-
152
-            case '[RECIPIENT_REGISTRATION_CODE]':
153
-                if (! $this->_recipient->reg_obj instanceof EE_Registration) {
154
-                    return '';
155
-                }
156
-                return $this->_get_reg_code();
157
-                break;
158
-
159
-            case '[RECIPIENT_EDIT_REGISTRATION_LINK]':
160
-                if (! $this->_recipient->reg_obj instanceof EE_Registration) {
161
-                    return '';
162
-                }
163
-                return $this->_recipient->reg_obj->edit_attendee_information_url();
164
-                break;
165
-
166
-            case '[RECIPIENT_PHONE_NUMBER]':
167
-                return $attendee->phone();
168
-                break;
169
-
170
-            case '[RECIPIENT_ADDRESS]':
171
-                return $attendee->address();
172
-                break;
173
-
174
-            case '[RECIPIENT_ADDRESS2]':
175
-                return $attendee->address2();
176
-                break;
177
-
178
-            case '[RECIPIENT_CITY]':
179
-                return $attendee->city();
180
-                break;
181
-
182
-            case '[RECIPIENT_ZIP_PC]':
183
-                return $attendee->zip();
184
-                break;
185
-
186
-            case '[RECIPIENT_ADDRESS_STATE]':
187
-                $state_obj = $attendee->state_obj();
188
-                return $state_obj instanceof EE_State ? $state_obj->name() : '';
189
-                break;
190
-
191
-            case '[RECIPIENT_COUNTRY]':
192
-                $country_obj = $attendee->country_obj();
193
-                return $country_obj instanceof EE_Country ? $country_obj->name() : '';
194
-                break;
195
-            case '[RECIPIENT_TOTAL_AMOUNT_PAID]':
196
-                return $this->_recipient->reg_obj instanceof EE_Registration
197
-                    ? $this->_recipient->reg_obj->pretty_paid()
198
-                    : 0;
199
-                break;
200
-        }
201
-
202
-        if (strpos($shortcode, '[RECIPIENT_ANSWER_*') !== false) {
203
-            $shortcode = str_replace('[RECIPIENT_ANSWER_*', '', $shortcode);
204
-            $shortcode = trim(str_replace(']', '', $shortcode));
205
-
206
-
207
-            // now let's figure out what question has this text
208
-            if (empty($this->_recipient->questions) || ! $this->_recipient->reg_obj instanceof EE_Registration) {
209
-                return '';
210
-            }
211
-
212
-            foreach ($this->_recipient->questions as $ansid => $question) {
213
-                if (
214
-                    $question instanceof EE_Question
215
-                    && trim($question->display_text()) === trim($shortcode)
216
-                    && isset($this->_recipient->registrations[ $this->_recipient->reg_obj->ID() ]['ans_objs'][ $ansid ])
217
-                ) {
218
-                    $recipient_ansid = $this->_recipient->registrations[ $this->_recipient->reg_obj->ID() ]['ans_objs'][ $ansid ];
219
-
220
-                    // what we show for the answer depends on the question type!
221
-                    switch ($question->get('QST_type')) {
222
-                        case EEM_Question::QST_type_state:
223
-                            $state = EEM_State::instance()->get_one_by_ID($recipient_ansid->get('ANS_value'));
224
-                            $answer = $state instanceof EE_State ? $state->name() : '';
225
-                            break;
226
-
227
-                        case EEM_Question::QST_type_country:
228
-                            $country = EEM_Country::instance()->get_one_by_ID($recipient_ansid->get('ANS_value'));
229
-                            $answer = $country instanceof EE_Country ? $country->name() : '';
230
-                            break;
231
-
232
-                        default:
233
-                            $answer = $recipient_ansid->get_pretty('ANS_value', 'no_wpautop');
234
-                            break;
235
-                    }
236
-
237
-                    return $answer;
238
-                    break;
239
-                }
240
-            }
241
-        }
242
-
243
-        return '';
244
-    }
245
-
246
-
247
-    /**
248
-     * Returns the EE_Messages_Addressee object for the recipient.
249
-     *
250
-     * @since 4.5.0
251
-     *
252
-     * @return EE_Messages_Addressee
253
-     */
254
-    public function get_recipient()
255
-    {
256
-        return $this->_recipient;
257
-    }
258
-
259
-
260
-    /**
261
-     * returns the reg code for the recipient depending on the context and whether the recipient has multiple
262
-     * registrations or not.
263
-     *
264
-     * @return string
265
-     */
266
-    protected function _get_reg_code()
267
-    {
268
-
269
-        // if only one related registration for the recipient then just return that reg code.
270
-        if (count($this->_registrations_for_recipient) <= 1) {
271
-            return $this->_recipient->reg_obj->reg_code();
272
-        }
273
-
274
-        // k more than one registration so let's see if we can get specific to context
275
-        // are we parsing event_list?
276
-        if ($this->_data instanceof EE_Event) {
277
-            $reg_code = array();
278
-            // loop through registrations for recipient and see if there is a match for this event
279
-            foreach ($this->_registrations_for_recipient as $reg) {
280
-                if ($reg instanceof EE_Registration && $reg->event_ID() == $this->_data->ID()) {
281
-                    $reg_code[] = $reg->reg_code();
282
-                }
283
-            }
284
-            return implode(', ', $reg_code);
285
-        }
286
-
287
-        // are we parsing ticket list?
288
-        if ($this->_data instanceof EE_Ticket) {
289
-            $reg_code = array();
290
-            // loop through each registration for recipient and see if there is a match for this ticket
291
-            foreach ($this->_registrations_for_recipient as $reg) {
292
-                if ($reg instanceof EE_Registration && $reg->ticket_ID() == $this->_data->ID()) {
293
-                    $reg_code = $reg->reg_code();
294
-                }
295
-            }
296
-            return implode(', ', $reg_code);
297
-        }
298
-
299
-        // do we have a specific reg_obj?  Let's use it
300
-        if ($this->_data instanceof EE_Messages_Addressee && $this->_data->reg_obj instanceof EE_Registration) {
301
-            return $this->_data->reg_obj->reg_code();
302
-        }
303
-
304
-        // do we have a specific reg_obj?  Let's use it
305
-        if ($this->_data instanceof EE_Messages_Addressee && $this->_data->reg_obj instanceof EE_Registration) {
306
-            return $this->_data->reg_obj->reg_code();
307
-        }
308
-
309
-        // not able to determine the single reg code so let's return a comma delimited list of reg codes.
310
-        $reg_code = array();
311
-        foreach ($this->_registrations_for_recipient as $reg) {
312
-            if ($reg instanceof EE_Registration) {
313
-                $reg_code[] = $reg->reg_code();
314
-            }
315
-        }
316
-        return implode(', ', $reg_code);
317
-    }
318
-
319
-
320
-    /**
321
-     * returns the reg ID for the recipient depending on the context and whether the recipient has multiple
322
-     * registrations or not.
323
-     *
324
-     * @return int|string
325
-     */
326
-    protected function _get_reg_id()
327
-    {
328
-
329
-        // if only one related registration for the recipient then just return that reg code.
330
-        if (count($this->_registrations_for_recipient) <= 1) {
331
-            return $this->_recipient->reg_obj->ID();
332
-        }
333
-
334
-        // k more than one registration so let's see if we can get specific to context
335
-        // are we parsing event_list?
336
-        if ($this->_data instanceof EE_Event) {
337
-            $registration_ids = array();
338
-            // loop through registrations for recipient and see if there is a match for this event
339
-            foreach ($this->_registrations_for_recipient as $reg) {
340
-                if ($reg instanceof EE_Registration && $reg->event_ID() == $this->_data->ID()) {
341
-                    $registration_ids[] = $reg->ID();
342
-                }
343
-            }
344
-            return implode(', ', $registration_ids);
345
-        }
346
-
347
-        // are we parsing ticket list?
348
-        if ($this->_data instanceof EE_Ticket) {
349
-            $registration_ids = array();
350
-            // loop through each registration for recipient and see if there is a match for this ticket
351
-            foreach ($this->_registrations_for_recipient as $reg) {
352
-                if ($reg instanceof EE_Registration && $reg->ticket_ID() == $this->_data->ID()) {
353
-                    $registration_ids = $reg->ID();
354
-                }
355
-            }
356
-            return implode(', ', $registration_ids);
357
-        }
358
-
359
-        // do we have a specific reg_obj?  Let's use it
360
-        if ($this->_data instanceof EE_Messages_Addressee && $this->_data->reg_obj instanceof EE_Registration) {
361
-            return $this->_data->reg_obj->ID();
362
-        }
363
-
364
-        // not able to determine the single reg code so let's return a comma delimited list of reg codes.
365
-        $registration_ids = array();
366
-        foreach ($this->_registrations_for_recipient as $reg) {
367
-            if ($reg instanceof EE_Registration) {
368
-                $registration_ids[] = $reg->ID();
369
-            }
370
-        }
371
-        return implode(', ', $registration_ids);
372
-    }
22
+	protected $_recipient;
23
+
24
+	protected $_registrations_for_recipient;
25
+
26
+
27
+	protected function _init_props()
28
+	{
29
+		$this->label = esc_html__('Recipient Details Shortcodes', 'event_espresso');
30
+		$this->description = esc_html__('All shortcodes specific to recipient registration data', 'event_espresso');
31
+		$this->_shortcodes = array(
32
+			'[RECIPIENT_FNAME]'                  => esc_html__(
33
+				'Parses to the first name of the recipient for the message.',
34
+				'event_espresso'
35
+			),
36
+			'[RECIPIENT_LNAME]'                  => esc_html__(
37
+				'Parses to the last name of the recipient for the message.',
38
+				'event_espresso'
39
+			),
40
+			'[RECIPIENT_EMAIL]'                  => esc_html__(
41
+				'Parses to the email address of the recipient for the message.',
42
+				'event_espresso'
43
+			),
44
+			'[RECIPIENT_REGISTRATION_ID]'        => esc_html__(
45
+				'Parses to the registration ID of the recipient for the message.',
46
+				'event_espresso'
47
+			),
48
+			'[RECIPIENT_REGISTRATION_CODE]'      => esc_html__(
49
+				'Parses to the registration code of the recipient for the message.',
50
+				'event_espresso'
51
+			),
52
+			'[RECIPIENT_EDIT_REGISTRATION_LINK]' => esc_html__(
53
+				'Parses to a link for frontend editing of the registration for the recipient.',
54
+				'event_espresso'
55
+			),
56
+			'[RECIPIENT_PHONE_NUMBER]'           => esc_html__(
57
+				'The Phone Number for the recipient of the message.',
58
+				'event_espresso'
59
+			),
60
+			'[RECIPIENT_ADDRESS]'                => esc_html__(
61
+				'The Address for the recipient of the message.',
62
+				'event_espresso'
63
+			),
64
+			'[RECIPIENT_ADDRESS2]'               => esc_html__(
65
+				'Whatever was in the address 2 field for the recipient of the message.',
66
+				'event_espresso'
67
+			),
68
+			'[RECIPIENT_CITY]'                   => esc_html__(
69
+				'The city for the recipient of the message.',
70
+				'event_espresso'
71
+			),
72
+			'[RECIPIENT_ZIP_PC]'                 => esc_html__(
73
+				'The ZIP (or Postal) Code for the recipient of the message.',
74
+				'event_espresso'
75
+			),
76
+			'[RECIPIENT_ADDRESS_STATE]'          => esc_html__(
77
+				'The state/province for the recipient of the message.',
78
+				'event_espresso'
79
+			),
80
+			'[RECIPIENT_COUNTRY]'                => esc_html__(
81
+				'The country for the recipient of the message.',
82
+				'event_espresso'
83
+			),
84
+			'[RECIPIENT_ANSWER_*]'               => esc_html__(
85
+				'This is a special dynamic shortcode.  After the "*", add the exact text of an existing question, and if there is an answer for that question for this recipient, then it will be output in place of this shortcode.',
86
+				'event_espresso'
87
+			),
88
+			'[RECIPIENT_TOTAL_AMOUNT_PAID]'      => esc_html__(
89
+				'If a single registration related to the recipient is available, that is used to retrieve the total amount that has been paid for this recipient.  Otherwise the value of 0 is printed.',
90
+				'event_espresso'
91
+			),
92
+		);
93
+	}
94
+
95
+
96
+	/**
97
+	 * @access protected
98
+	 * @param  string $shortcode the shortcode to be parsed.
99
+	 * @return string parsed shortcode
100
+	 */
101
+	protected function _parser($shortcode)
102
+	{
103
+
104
+		// make sure we end up with a copy of the EE_Messages_Addressee object
105
+		$this->_recipient = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null;
106
+		$this->_recipient = ! $this->_recipient instanceof EE_Messages_Addressee
107
+							&& is_array($this->_data)
108
+							&& isset($this->_data['data'])
109
+							&& $this->_data['data'] instanceof EE_Messages_Addressee
110
+			? $this->_data['data']
111
+			:
112
+			$this->_recipient;
113
+		$this->_recipient = ! $this->_recipient instanceof EE_Messages_Addressee
114
+							&& ! empty($this->_extra_data['data'])
115
+							&& $this->_extra_data['data'] instanceof EE_Messages_Addressee
116
+			? $this->_extra_data['data']
117
+			: $this->_recipient;
118
+
119
+		if (! $this->_recipient instanceof EE_Messages_Addressee) {
120
+			return '';
121
+		}
122
+
123
+		$attendee = $this->_recipient->att_obj;
124
+		if (! $attendee instanceof EE_Attendee) {
125
+			return '';
126
+		}
127
+
128
+		$this->_registrations_for_recipient = isset($this->_recipient->attendees[ $attendee->ID() ]['reg_objs'])
129
+			? $this->_recipient->attendees[ $attendee->ID() ]['reg_objs']
130
+			: array();
131
+
132
+		switch ($shortcode) {
133
+			case '[RECIPIENT_FNAME]':
134
+				return $attendee->fname();
135
+				break;
136
+
137
+			case '[RECIPIENT_LNAME]':
138
+				return $attendee->lname();
139
+				break;
140
+
141
+			case '[RECIPIENT_EMAIL]':
142
+				return $attendee->email();
143
+				break;
144
+
145
+			case '[RECIPIENT_REGISTRATION_ID]':
146
+				if (! $this->_recipient->reg_obj instanceof EE_Registration) {
147
+					return '';
148
+				}
149
+				return $this->_get_reg_id();
150
+				break;
151
+
152
+			case '[RECIPIENT_REGISTRATION_CODE]':
153
+				if (! $this->_recipient->reg_obj instanceof EE_Registration) {
154
+					return '';
155
+				}
156
+				return $this->_get_reg_code();
157
+				break;
158
+
159
+			case '[RECIPIENT_EDIT_REGISTRATION_LINK]':
160
+				if (! $this->_recipient->reg_obj instanceof EE_Registration) {
161
+					return '';
162
+				}
163
+				return $this->_recipient->reg_obj->edit_attendee_information_url();
164
+				break;
165
+
166
+			case '[RECIPIENT_PHONE_NUMBER]':
167
+				return $attendee->phone();
168
+				break;
169
+
170
+			case '[RECIPIENT_ADDRESS]':
171
+				return $attendee->address();
172
+				break;
173
+
174
+			case '[RECIPIENT_ADDRESS2]':
175
+				return $attendee->address2();
176
+				break;
177
+
178
+			case '[RECIPIENT_CITY]':
179
+				return $attendee->city();
180
+				break;
181
+
182
+			case '[RECIPIENT_ZIP_PC]':
183
+				return $attendee->zip();
184
+				break;
185
+
186
+			case '[RECIPIENT_ADDRESS_STATE]':
187
+				$state_obj = $attendee->state_obj();
188
+				return $state_obj instanceof EE_State ? $state_obj->name() : '';
189
+				break;
190
+
191
+			case '[RECIPIENT_COUNTRY]':
192
+				$country_obj = $attendee->country_obj();
193
+				return $country_obj instanceof EE_Country ? $country_obj->name() : '';
194
+				break;
195
+			case '[RECIPIENT_TOTAL_AMOUNT_PAID]':
196
+				return $this->_recipient->reg_obj instanceof EE_Registration
197
+					? $this->_recipient->reg_obj->pretty_paid()
198
+					: 0;
199
+				break;
200
+		}
201
+
202
+		if (strpos($shortcode, '[RECIPIENT_ANSWER_*') !== false) {
203
+			$shortcode = str_replace('[RECIPIENT_ANSWER_*', '', $shortcode);
204
+			$shortcode = trim(str_replace(']', '', $shortcode));
205
+
206
+
207
+			// now let's figure out what question has this text
208
+			if (empty($this->_recipient->questions) || ! $this->_recipient->reg_obj instanceof EE_Registration) {
209
+				return '';
210
+			}
211
+
212
+			foreach ($this->_recipient->questions as $ansid => $question) {
213
+				if (
214
+					$question instanceof EE_Question
215
+					&& trim($question->display_text()) === trim($shortcode)
216
+					&& isset($this->_recipient->registrations[ $this->_recipient->reg_obj->ID() ]['ans_objs'][ $ansid ])
217
+				) {
218
+					$recipient_ansid = $this->_recipient->registrations[ $this->_recipient->reg_obj->ID() ]['ans_objs'][ $ansid ];
219
+
220
+					// what we show for the answer depends on the question type!
221
+					switch ($question->get('QST_type')) {
222
+						case EEM_Question::QST_type_state:
223
+							$state = EEM_State::instance()->get_one_by_ID($recipient_ansid->get('ANS_value'));
224
+							$answer = $state instanceof EE_State ? $state->name() : '';
225
+							break;
226
+
227
+						case EEM_Question::QST_type_country:
228
+							$country = EEM_Country::instance()->get_one_by_ID($recipient_ansid->get('ANS_value'));
229
+							$answer = $country instanceof EE_Country ? $country->name() : '';
230
+							break;
231
+
232
+						default:
233
+							$answer = $recipient_ansid->get_pretty('ANS_value', 'no_wpautop');
234
+							break;
235
+					}
236
+
237
+					return $answer;
238
+					break;
239
+				}
240
+			}
241
+		}
242
+
243
+		return '';
244
+	}
245
+
246
+
247
+	/**
248
+	 * Returns the EE_Messages_Addressee object for the recipient.
249
+	 *
250
+	 * @since 4.5.0
251
+	 *
252
+	 * @return EE_Messages_Addressee
253
+	 */
254
+	public function get_recipient()
255
+	{
256
+		return $this->_recipient;
257
+	}
258
+
259
+
260
+	/**
261
+	 * returns the reg code for the recipient depending on the context and whether the recipient has multiple
262
+	 * registrations or not.
263
+	 *
264
+	 * @return string
265
+	 */
266
+	protected function _get_reg_code()
267
+	{
268
+
269
+		// if only one related registration for the recipient then just return that reg code.
270
+		if (count($this->_registrations_for_recipient) <= 1) {
271
+			return $this->_recipient->reg_obj->reg_code();
272
+		}
273
+
274
+		// k more than one registration so let's see if we can get specific to context
275
+		// are we parsing event_list?
276
+		if ($this->_data instanceof EE_Event) {
277
+			$reg_code = array();
278
+			// loop through registrations for recipient and see if there is a match for this event
279
+			foreach ($this->_registrations_for_recipient as $reg) {
280
+				if ($reg instanceof EE_Registration && $reg->event_ID() == $this->_data->ID()) {
281
+					$reg_code[] = $reg->reg_code();
282
+				}
283
+			}
284
+			return implode(', ', $reg_code);
285
+		}
286
+
287
+		// are we parsing ticket list?
288
+		if ($this->_data instanceof EE_Ticket) {
289
+			$reg_code = array();
290
+			// loop through each registration for recipient and see if there is a match for this ticket
291
+			foreach ($this->_registrations_for_recipient as $reg) {
292
+				if ($reg instanceof EE_Registration && $reg->ticket_ID() == $this->_data->ID()) {
293
+					$reg_code = $reg->reg_code();
294
+				}
295
+			}
296
+			return implode(', ', $reg_code);
297
+		}
298
+
299
+		// do we have a specific reg_obj?  Let's use it
300
+		if ($this->_data instanceof EE_Messages_Addressee && $this->_data->reg_obj instanceof EE_Registration) {
301
+			return $this->_data->reg_obj->reg_code();
302
+		}
303
+
304
+		// do we have a specific reg_obj?  Let's use it
305
+		if ($this->_data instanceof EE_Messages_Addressee && $this->_data->reg_obj instanceof EE_Registration) {
306
+			return $this->_data->reg_obj->reg_code();
307
+		}
308
+
309
+		// not able to determine the single reg code so let's return a comma delimited list of reg codes.
310
+		$reg_code = array();
311
+		foreach ($this->_registrations_for_recipient as $reg) {
312
+			if ($reg instanceof EE_Registration) {
313
+				$reg_code[] = $reg->reg_code();
314
+			}
315
+		}
316
+		return implode(', ', $reg_code);
317
+	}
318
+
319
+
320
+	/**
321
+	 * returns the reg ID for the recipient depending on the context and whether the recipient has multiple
322
+	 * registrations or not.
323
+	 *
324
+	 * @return int|string
325
+	 */
326
+	protected function _get_reg_id()
327
+	{
328
+
329
+		// if only one related registration for the recipient then just return that reg code.
330
+		if (count($this->_registrations_for_recipient) <= 1) {
331
+			return $this->_recipient->reg_obj->ID();
332
+		}
333
+
334
+		// k more than one registration so let's see if we can get specific to context
335
+		// are we parsing event_list?
336
+		if ($this->_data instanceof EE_Event) {
337
+			$registration_ids = array();
338
+			// loop through registrations for recipient and see if there is a match for this event
339
+			foreach ($this->_registrations_for_recipient as $reg) {
340
+				if ($reg instanceof EE_Registration && $reg->event_ID() == $this->_data->ID()) {
341
+					$registration_ids[] = $reg->ID();
342
+				}
343
+			}
344
+			return implode(', ', $registration_ids);
345
+		}
346
+
347
+		// are we parsing ticket list?
348
+		if ($this->_data instanceof EE_Ticket) {
349
+			$registration_ids = array();
350
+			// loop through each registration for recipient and see if there is a match for this ticket
351
+			foreach ($this->_registrations_for_recipient as $reg) {
352
+				if ($reg instanceof EE_Registration && $reg->ticket_ID() == $this->_data->ID()) {
353
+					$registration_ids = $reg->ID();
354
+				}
355
+			}
356
+			return implode(', ', $registration_ids);
357
+		}
358
+
359
+		// do we have a specific reg_obj?  Let's use it
360
+		if ($this->_data instanceof EE_Messages_Addressee && $this->_data->reg_obj instanceof EE_Registration) {
361
+			return $this->_data->reg_obj->ID();
362
+		}
363
+
364
+		// not able to determine the single reg code so let's return a comma delimited list of reg codes.
365
+		$registration_ids = array();
366
+		foreach ($this->_registrations_for_recipient as $reg) {
367
+			if ($reg instanceof EE_Registration) {
368
+				$registration_ids[] = $reg->ID();
369
+			}
370
+		}
371
+		return implode(', ', $registration_ids);
372
+	}
373 373
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -116,17 +116,17 @@  discard block
 block discarded – undo
116 116
             ? $this->_extra_data['data']
117 117
             : $this->_recipient;
118 118
 
119
-        if (! $this->_recipient instanceof EE_Messages_Addressee) {
119
+        if ( ! $this->_recipient instanceof EE_Messages_Addressee) {
120 120
             return '';
121 121
         }
122 122
 
123 123
         $attendee = $this->_recipient->att_obj;
124
-        if (! $attendee instanceof EE_Attendee) {
124
+        if ( ! $attendee instanceof EE_Attendee) {
125 125
             return '';
126 126
         }
127 127
 
128
-        $this->_registrations_for_recipient = isset($this->_recipient->attendees[ $attendee->ID() ]['reg_objs'])
129
-            ? $this->_recipient->attendees[ $attendee->ID() ]['reg_objs']
128
+        $this->_registrations_for_recipient = isset($this->_recipient->attendees[$attendee->ID()]['reg_objs'])
129
+            ? $this->_recipient->attendees[$attendee->ID()]['reg_objs']
130 130
             : array();
131 131
 
132 132
         switch ($shortcode) {
@@ -143,21 +143,21 @@  discard block
 block discarded – undo
143 143
                 break;
144 144
 
145 145
             case '[RECIPIENT_REGISTRATION_ID]':
146
-                if (! $this->_recipient->reg_obj instanceof EE_Registration) {
146
+                if ( ! $this->_recipient->reg_obj instanceof EE_Registration) {
147 147
                     return '';
148 148
                 }
149 149
                 return $this->_get_reg_id();
150 150
                 break;
151 151
 
152 152
             case '[RECIPIENT_REGISTRATION_CODE]':
153
-                if (! $this->_recipient->reg_obj instanceof EE_Registration) {
153
+                if ( ! $this->_recipient->reg_obj instanceof EE_Registration) {
154 154
                     return '';
155 155
                 }
156 156
                 return $this->_get_reg_code();
157 157
                 break;
158 158
 
159 159
             case '[RECIPIENT_EDIT_REGISTRATION_LINK]':
160
-                if (! $this->_recipient->reg_obj instanceof EE_Registration) {
160
+                if ( ! $this->_recipient->reg_obj instanceof EE_Registration) {
161 161
                     return '';
162 162
                 }
163 163
                 return $this->_recipient->reg_obj->edit_attendee_information_url();
@@ -213,9 +213,9 @@  discard block
 block discarded – undo
213 213
                 if (
214 214
                     $question instanceof EE_Question
215 215
                     && trim($question->display_text()) === trim($shortcode)
216
-                    && isset($this->_recipient->registrations[ $this->_recipient->reg_obj->ID() ]['ans_objs'][ $ansid ])
216
+                    && isset($this->_recipient->registrations[$this->_recipient->reg_obj->ID()]['ans_objs'][$ansid])
217 217
                 ) {
218
-                    $recipient_ansid = $this->_recipient->registrations[ $this->_recipient->reg_obj->ID() ]['ans_objs'][ $ansid ];
218
+                    $recipient_ansid = $this->_recipient->registrations[$this->_recipient->reg_obj->ID()]['ans_objs'][$ansid];
219 219
 
220 220
                     // what we show for the answer depends on the question type!
221 221
                     switch ($question->get('QST_type')) {
Please login to merge, or discard this patch.
core/libraries/shortcodes/EE_Event_Shortcodes.lib.php 2 patches
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 
135 135
 
136 136
         // If there is no event objecdt by now then get out.
137
-        if (! $this->_event instanceof EE_Event) {
137
+        if ( ! $this->_event instanceof EE_Event) {
138 138
             return '';
139 139
         }
140 140
 
@@ -187,11 +187,11 @@  discard block
 block discarded – undo
187 187
                 $image = $this->_event->feature_image_url(array(600, 300));
188 188
                 // @todo: eventually we should make this an attribute shortcode so that em can send along what size they want returned.
189 189
                 return ! empty($image)
190
-                    ? '<img src="' . $image . '" alt="'
190
+                    ? '<img src="'.$image.'" alt="'
191 191
                       . sprintf(
192 192
                           esc_attr__('%s Feature Image', 'event_espresso'),
193 193
                           $this->_event->get('EVT_name')
194
-                      ) . '" />'
194
+                      ).'" />'
195 195
                     : '';
196 196
                 break;
197 197
 
@@ -253,7 +253,7 @@  discard block
 block discarded – undo
253 253
             // Check if a do_shortcode attribute was set to true and if so run $event_meta through that function.
254 254
             if (
255 255
                 apply_filters('FHEE__EventEspresso_core_libraries_shortcodes_EE_Event_Shortcodes___parser__event_meta_do_shortcode', false)
256
-                || !empty($attrs['do_shortcode']) && filter_var($attrs['do_shortcode'], FILTER_VALIDATE_BOOLEAN)
256
+                || ! empty($attrs['do_shortcode']) && filter_var($attrs['do_shortcode'], FILTER_VALIDATE_BOOLEAN)
257 257
             ) {
258 258
                 return do_shortcode($event_meta);
259 259
             }
@@ -271,11 +271,11 @@  discard block
 block discarded – undo
271 271
 
272 272
         if (strpos($shortcode, '[EVENT_IMAGE_*') !== false) {
273 273
             $attrs = $this->_get_shortcode_attrs($shortcode);
274
-            $width = empty($attrs['width']) ? '' : ' width="' . $attrs['width'] . '"';
275
-            $height = empty($attrs['height']) ? '' : ' height="' . $attrs['height'] . '"';
274
+            $width = empty($attrs['width']) ? '' : ' width="'.$attrs['width'].'"';
275
+            $height = empty($attrs['height']) ? '' : ' height="'.$attrs['height'].'"';
276 276
 
277 277
             // Size may be set to a string such as 'tumbnail' or "width, height" eg - '200,200'
278
-            if (! empty($attrs['size'])) {
278
+            if ( ! empty($attrs['size'])) {
279 279
                 $size = explode(',', $attrs['size']);
280 280
                 if (count($size) === 1) {
281 281
                     $size = $size[0];
@@ -287,11 +287,11 @@  discard block
 block discarded – undo
287 287
             $image = $this->_event->feature_image_url($size);
288 288
 
289 289
             return ! empty($image)
290
-                ? '<img src="' . $image . '" alt="'
290
+                ? '<img src="'.$image.'" alt="'
291 291
                   . sprintf(
292 292
                       esc_attr__('%s Feature Image', 'event_espresso'),
293 293
                       $this->_event->get('EVT_name')
294
-                  ) . '"' . $width . $height . '/>'
294
+                  ).'"'.$width.$height.'/>'
295 295
                 : '';
296 296
         }
297 297
 
@@ -310,6 +310,6 @@  discard block
 block discarded – undo
310 310
     {
311 311
         $url = get_permalink($event->ID());
312 312
 
313
-        return $full_link ? '<a href="' . $url . '">' . $event->get('EVT_name') . '</a>' : $url;
313
+        return $full_link ? '<a href="'.$url.'">'.$event->get('EVT_name').'</a>' : $url;
314 314
     }
315 315
 }
Please login to merge, or discard this patch.
Indentation   +293 added lines, -293 removed lines patch added patch discarded remove patch
@@ -19,297 +19,297 @@
 block discarded – undo
19 19
 {
20 20
 
21 21
 
22
-    /**
23
-     * Will hold the EE_Event if available
24
-     *
25
-     * @var EE_Event
26
-     */
27
-    protected $_event;
28
-
29
-
30
-    public function __construct()
31
-    {
32
-        parent::__construct();
33
-    }
34
-
35
-
36
-    protected function _init_props()
37
-    {
38
-        $this->label = esc_html__('Event Shortcodes', 'event_espresso');
39
-        $this->description = esc_html__('All shortcodes specific to event related data', 'event_espresso');
40
-        $this->_shortcodes = array(
41
-            '[EVENT_ID]'                              => esc_html__(
42
-                'Will be replaced by the event ID of an event',
43
-                'event_espresso'
44
-            ),
45
-            '[EVENT]'                                 => esc_html__('The name of the event', 'event_espresso'),
46
-            '[EVENT_NAME]'                            => esc_html__(
47
-                "This also can be used for the name of the event",
48
-                'event_espresso'
49
-            ),
50
-            '[EVENT_PHONE]'                           => esc_html__(
51
-                'The phone number for the event (usually an info number)',
52
-                'event_espresso'
53
-            ),
54
-            '[EVENT_DESCRIPTION]'                     => esc_html__('The description of the event', 'event_espresso'),
55
-            '[EVENT_EXCERPT]'                         => esc_html__(
56
-                'This gets parsed to the value for the excerpt field in the event or blank if there is no excerpt.',
57
-                'event_espresso'
58
-            ),
59
-            '[EVENT_LINK]'                            => esc_html__('A link associated with the event', 'event_espresso'),
60
-            '[EVENT_URL]'                             => esc_html__(
61
-                'A link to the event set up on the host site.',
62
-                'event_espresso'
63
-            ),
64
-            '[VIRTUAL_URL]'                           => esc_html__(
65
-                'What was used for the "URL of Event" field in the Venue settings',
66
-                'event_espresso'
67
-            ),
68
-            '[VIRTUAL_PHONE]'                         => esc_html__(
69
-                'An alternate phone number for the event. Typically used as a "call-in" number',
70
-                'event_espresso'
71
-            ),
72
-            '[EVENT_IMAGE]'                           => esc_html__(
73
-                'This will parse to the Feature image for the event.',
74
-                'event_espresso'
75
-            ),
76
-            '[EVENT_IMAGE_*]'                         => sprintf(
77
-                esc_html__(
78
-                    'This will parse to the Feature image for the event, %1$ssize%2$s can be set to determine the size of the image loaded by the shortcode. The %1$swidth%2$s and/or %1$sheight%2$s can also be set to determine the width and height of the image when output. By default the shortcode will load the %1$sthumbnail%2$s image size.',
79
-                    'event_espresso'
80
-                ),
81
-                '<code>',
82
-                '</code>'
83
-            ),
84
-            '[EVENT_TOTAL_AVAILABLE_SPACES_*]'        => sprintf(
85
-                esc_html__(
86
-                    'This will parse to the total available spaces for an event. Calculating total spaces is approximate because it is dependent on the complexity of limits on your event.  There are two methods of calculation (which can be indicated by the %1$smethod%2$s param on the shortcode).  %1$scurrent%2$s which will do a more accurate calculation of total available spaces based on current sales, and %1$sfull%2$s which will be the maximum total available spaces that is on the event in optimal conditions. The shortcode will default to current.',
87
-                    'event_espresso'
88
-                ),
89
-                '<code>',
90
-                '</code>'
91
-            ),
92
-            '[EVENT_TOTAL_SPOTS_TAKEN]'               => esc_html__(
93
-                'This shortcode will parse to the output the total approved registrations for this event',
94
-                'event_espresso'
95
-            ),
96
-            '[EVENT_FACEBOOK_URL]'                    => esc_html__(
97
-                'This will return the Facebook URL for the event if you have it set via custom field in your event, otherwise it will use the Facebook URL set in "Your Organization Settings". To set the facebook url in your event, add a custom field with the key as <code>event_facebook</code> and the value as your facebook url.',
98
-                'event_espresso'
99
-            ),
100
-            '[EVENT_TWITTER_URL]'                     => esc_html__(
101
-                'This will return the Twitter URL for the event if you have it set via custom field in your event, otherwise it will use the Twitter URL set in "Your Organization Settings". To set the facebook url in your event, add a custom field with the key as <code>event_twitter</code> and the value as your facebook url',
102
-                'event_espresso'
103
-            ),
104
-            '[EVENT_META_*]'                          => sprintf(
105
-                esc_html__(
106
-                    'This is a special dynamic shortcode. After the "*", add the exact name for your custom field, if there is a value set for that custom field within the event then it will be output in place of this shortcode. If you use shortcodes within your custom fields set %1$sdo_shortcode=true%2$s at the end of the shortcode to run the value through the do_shortcode function. ',
107
-                    'event_espresso'
108
-                ),
109
-                '<code>',
110
-                '</code>'
111
-            ),
112
-            '[REGISTRATION_LIST_TABLE_FOR_EVENT_URL]' => esc_html__(
113
-                'This parses to the url for the registration list table filtered by registrations for this event.',
114
-                'event_espresso'
115
-            ),
116
-        );
117
-    }
118
-
119
-
120
-    protected function _parser($shortcode)
121
-    {
122
-
123
-
124
-        $this->_event = $this->_data instanceof EE_Event ? $this->_data : null;
125
-
126
-        // if no event, then let's see if there is a reg_obj.  If there IS, then we'll try and grab the event from the reg_obj instead.
127
-        if (empty($this->_event)) {
128
-            $aee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null;
129
-            $aee = $this->_extra_data instanceof EE_Messages_Addressee ? $this->_extra_data : $aee;
130
-
131
-            $this->_event = $aee instanceof EE_Messages_Addressee && $aee->reg_obj instanceof EE_Registration
132
-                ? $aee->reg_obj->event() : null;
133
-        }
134
-
135
-
136
-        // If there is no event objecdt by now then get out.
137
-        if (! $this->_event instanceof EE_Event) {
138
-            return '';
139
-        }
140
-
141
-        switch ($shortcode) {
142
-            case '[EVENT_ID]':
143
-                return $this->_event->ID();
144
-                break;
145
-
146
-            case '[EVENT]':
147
-            case '[EVENT_NAME]':
148
-                return $this->_event->get('EVT_name');
149
-                break;
150
-
151
-            case '[EVENT_PHONE]':
152
-                return $this->_event->get('EVT_phone');
153
-                break;
154
-
155
-            case '[EVENT_DESCRIPTION]':
156
-                return $this->_event->get('EVT_desc');
157
-                break;
158
-
159
-            case '[EVENT_EXCERPT]':
160
-                return $this->_event->get('EVT_short_desc');
161
-                break;
162
-
163
-            case '[EVENT_LINK]':
164
-                return $this->_get_event_link($this->_event);
165
-                break;
166
-
167
-            case '[EVENT_URL]':
168
-                return $this->_get_event_link($this->_event, false);
169
-                break;
170
-
171
-            case '[VIRTUAL_URL]':
172
-                $venue = $this->_event->get_first_related('Venue');
173
-                if (empty($venue)) {
174
-                    return '';
175
-                }
176
-                return $venue->get('VNU_virtual_url');
177
-
178
-            case '[VIRTUAL_PHONE]':
179
-                $venue = $this->_event->get_first_related('Venue');
180
-                if (empty($venue)) {
181
-                    return '';
182
-                }
183
-                return $venue->get('VNU_virtual_phone');
184
-                break;
185
-
186
-            case '[EVENT_IMAGE]':
187
-                $image = $this->_event->feature_image_url(array(600, 300));
188
-                // @todo: eventually we should make this an attribute shortcode so that em can send along what size they want returned.
189
-                return ! empty($image)
190
-                    ? '<img src="' . $image . '" alt="'
191
-                      . sprintf(
192
-                          esc_attr__('%s Feature Image', 'event_espresso'),
193
-                          $this->_event->get('EVT_name')
194
-                      ) . '" />'
195
-                    : '';
196
-                break;
197
-
198
-            case '[EVENT_FACEBOOK_URL]':
199
-                $facebook_url = $this->_event->get_post_meta('event_facebook', true);
200
-                return empty($facebook_url) ? EE_Registry::instance()->CFG->organization->get_pretty('facebook')
201
-                    : $facebook_url;
202
-                break;
203
-
204
-            case '[EVENT_TWITTER_URL]':
205
-                $twitter_url = $this->_event->get_post_meta('event_twitter', true);
206
-                return empty($twitter_url) ? EE_Registry::instance()->CFG->organization->get_pretty('twitter')
207
-                    : $twitter_url;
208
-                break;
209
-
210
-            case '[EVENT_AUTHOR_EMAIL]':
211
-                $author_id = $this->_event->get('EVT_wp_user');
212
-                $user_data = get_userdata((int) $author_id);
213
-                return $user_data->user_email;
214
-                break;
215
-
216
-            case '[EVENT_TOTAL_SPOTS_TAKEN]':
217
-                return EEM_Registration::instance()->count(
218
-                    array(array('EVT_ID' => $this->_event->ID(), 'STS_ID' => EEM_Registration::status_id_approved)),
219
-                    'REG_ID',
220
-                    true
221
-                );
222
-                break;
223
-
224
-            case '[REGISTRATION_LIST_TABLE_FOR_EVENT_URL]':
225
-                return EEH_URL::add_query_args_and_nonce(
226
-                    array(
227
-                        'event_id' => $this->_event->ID(),
228
-                        'page'     => 'espresso_registrations',
229
-                        'action'   => 'default',
230
-                    ),
231
-                    admin_url('admin.php'),
232
-                    true
233
-                );
234
-                break;
235
-        }
236
-
237
-        if (strpos($shortcode, '[EVENT_META_*') !== false) {
238
-            // Strip the shortcode itself from $shortcode leaving any attributes set.
239
-            // Removing the * is correct here as _* is used to indiciate a dynamic shortcode.
240
-            $shortcode = str_replace('[EVENT_META_*', '', $shortcode);
241
-            $shortcode = trim(str_replace(']', '', $shortcode));
242
-            // Get any attributes set on this shortcode.
243
-            $attrs = $this->_get_shortcode_attrs($shortcode);
244
-            // The meta_key set on the shortcode should always be the first value in the array.
245
-            $meta_key = $attrs[0];
246
-            // Pull the meta value from the event post.
247
-            $event_meta = $this->_event->get_post_meta($meta_key, true);
248
-            // If we have no event_meta, just return an empty string.
249
-            if (empty($event_meta)) {
250
-                return '';
251
-            }
252
-            // Add a filter to allow all instances of EVENT_META_* to run through do_shortcode, default to false.
253
-            // Check if a do_shortcode attribute was set to true and if so run $event_meta through that function.
254
-            if (
255
-                apply_filters('FHEE__EventEspresso_core_libraries_shortcodes_EE_Event_Shortcodes___parser__event_meta_do_shortcode', false)
256
-                || !empty($attrs['do_shortcode']) && filter_var($attrs['do_shortcode'], FILTER_VALIDATE_BOOLEAN)
257
-            ) {
258
-                return do_shortcode($event_meta);
259
-            }
260
-            // Still here? We just need to return the event_meta value as is.
261
-            return $event_meta;
262
-        }
263
-
264
-        if (strpos($shortcode, '[EVENT_TOTAL_AVAILABLE_SPACES_*') !== false) {
265
-            $attrs = $this->_get_shortcode_attrs($shortcode);
266
-            $method = empty($attrs['method']) ? 'current' : $attrs['method'];
267
-            $method = $method === 'current';
268
-            $available = $this->_event->total_available_spaces($method);
269
-            return $available === EE_INF ? '&infin;' : $available;
270
-        }
271
-
272
-        if (strpos($shortcode, '[EVENT_IMAGE_*') !== false) {
273
-            $attrs = $this->_get_shortcode_attrs($shortcode);
274
-            $width = empty($attrs['width']) ? '' : ' width="' . $attrs['width'] . '"';
275
-            $height = empty($attrs['height']) ? '' : ' height="' . $attrs['height'] . '"';
276
-
277
-            // Size may be set to a string such as 'tumbnail' or "width, height" eg - '200,200'
278
-            if (! empty($attrs['size'])) {
279
-                $size = explode(',', $attrs['size']);
280
-                if (count($size) === 1) {
281
-                    $size = $size[0];
282
-                }
283
-            } else {
284
-                $size = 'thumbnail';
285
-            }
286
-
287
-            $image = $this->_event->feature_image_url($size);
288
-
289
-            return ! empty($image)
290
-                ? '<img src="' . $image . '" alt="'
291
-                  . sprintf(
292
-                      esc_attr__('%s Feature Image', 'event_espresso'),
293
-                      $this->_event->get('EVT_name')
294
-                  ) . '"' . $width . $height . '/>'
295
-                : '';
296
-        }
297
-
298
-        return '';
299
-    }
300
-
301
-
302
-    /**
303
-     * returns the link to the event
304
-     *
305
-     * @param  boolean $full_link if TRUE (default) we return the html for the name of the event linked to the event.
306
-     *                            Otherwise we just return the url of the event.
307
-     * @return string
308
-     */
309
-    private function _get_event_link($event, $full_link = true)
310
-    {
311
-        $url = get_permalink($event->ID());
312
-
313
-        return $full_link ? '<a href="' . $url . '">' . $event->get('EVT_name') . '</a>' : $url;
314
-    }
22
+	/**
23
+	 * Will hold the EE_Event if available
24
+	 *
25
+	 * @var EE_Event
26
+	 */
27
+	protected $_event;
28
+
29
+
30
+	public function __construct()
31
+	{
32
+		parent::__construct();
33
+	}
34
+
35
+
36
+	protected function _init_props()
37
+	{
38
+		$this->label = esc_html__('Event Shortcodes', 'event_espresso');
39
+		$this->description = esc_html__('All shortcodes specific to event related data', 'event_espresso');
40
+		$this->_shortcodes = array(
41
+			'[EVENT_ID]'                              => esc_html__(
42
+				'Will be replaced by the event ID of an event',
43
+				'event_espresso'
44
+			),
45
+			'[EVENT]'                                 => esc_html__('The name of the event', 'event_espresso'),
46
+			'[EVENT_NAME]'                            => esc_html__(
47
+				"This also can be used for the name of the event",
48
+				'event_espresso'
49
+			),
50
+			'[EVENT_PHONE]'                           => esc_html__(
51
+				'The phone number for the event (usually an info number)',
52
+				'event_espresso'
53
+			),
54
+			'[EVENT_DESCRIPTION]'                     => esc_html__('The description of the event', 'event_espresso'),
55
+			'[EVENT_EXCERPT]'                         => esc_html__(
56
+				'This gets parsed to the value for the excerpt field in the event or blank if there is no excerpt.',
57
+				'event_espresso'
58
+			),
59
+			'[EVENT_LINK]'                            => esc_html__('A link associated with the event', 'event_espresso'),
60
+			'[EVENT_URL]'                             => esc_html__(
61
+				'A link to the event set up on the host site.',
62
+				'event_espresso'
63
+			),
64
+			'[VIRTUAL_URL]'                           => esc_html__(
65
+				'What was used for the "URL of Event" field in the Venue settings',
66
+				'event_espresso'
67
+			),
68
+			'[VIRTUAL_PHONE]'                         => esc_html__(
69
+				'An alternate phone number for the event. Typically used as a "call-in" number',
70
+				'event_espresso'
71
+			),
72
+			'[EVENT_IMAGE]'                           => esc_html__(
73
+				'This will parse to the Feature image for the event.',
74
+				'event_espresso'
75
+			),
76
+			'[EVENT_IMAGE_*]'                         => sprintf(
77
+				esc_html__(
78
+					'This will parse to the Feature image for the event, %1$ssize%2$s can be set to determine the size of the image loaded by the shortcode. The %1$swidth%2$s and/or %1$sheight%2$s can also be set to determine the width and height of the image when output. By default the shortcode will load the %1$sthumbnail%2$s image size.',
79
+					'event_espresso'
80
+				),
81
+				'<code>',
82
+				'</code>'
83
+			),
84
+			'[EVENT_TOTAL_AVAILABLE_SPACES_*]'        => sprintf(
85
+				esc_html__(
86
+					'This will parse to the total available spaces for an event. Calculating total spaces is approximate because it is dependent on the complexity of limits on your event.  There are two methods of calculation (which can be indicated by the %1$smethod%2$s param on the shortcode).  %1$scurrent%2$s which will do a more accurate calculation of total available spaces based on current sales, and %1$sfull%2$s which will be the maximum total available spaces that is on the event in optimal conditions. The shortcode will default to current.',
87
+					'event_espresso'
88
+				),
89
+				'<code>',
90
+				'</code>'
91
+			),
92
+			'[EVENT_TOTAL_SPOTS_TAKEN]'               => esc_html__(
93
+				'This shortcode will parse to the output the total approved registrations for this event',
94
+				'event_espresso'
95
+			),
96
+			'[EVENT_FACEBOOK_URL]'                    => esc_html__(
97
+				'This will return the Facebook URL for the event if you have it set via custom field in your event, otherwise it will use the Facebook URL set in "Your Organization Settings". To set the facebook url in your event, add a custom field with the key as <code>event_facebook</code> and the value as your facebook url.',
98
+				'event_espresso'
99
+			),
100
+			'[EVENT_TWITTER_URL]'                     => esc_html__(
101
+				'This will return the Twitter URL for the event if you have it set via custom field in your event, otherwise it will use the Twitter URL set in "Your Organization Settings". To set the facebook url in your event, add a custom field with the key as <code>event_twitter</code> and the value as your facebook url',
102
+				'event_espresso'
103
+			),
104
+			'[EVENT_META_*]'                          => sprintf(
105
+				esc_html__(
106
+					'This is a special dynamic shortcode. After the "*", add the exact name for your custom field, if there is a value set for that custom field within the event then it will be output in place of this shortcode. If you use shortcodes within your custom fields set %1$sdo_shortcode=true%2$s at the end of the shortcode to run the value through the do_shortcode function. ',
107
+					'event_espresso'
108
+				),
109
+				'<code>',
110
+				'</code>'
111
+			),
112
+			'[REGISTRATION_LIST_TABLE_FOR_EVENT_URL]' => esc_html__(
113
+				'This parses to the url for the registration list table filtered by registrations for this event.',
114
+				'event_espresso'
115
+			),
116
+		);
117
+	}
118
+
119
+
120
+	protected function _parser($shortcode)
121
+	{
122
+
123
+
124
+		$this->_event = $this->_data instanceof EE_Event ? $this->_data : null;
125
+
126
+		// if no event, then let's see if there is a reg_obj.  If there IS, then we'll try and grab the event from the reg_obj instead.
127
+		if (empty($this->_event)) {
128
+			$aee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null;
129
+			$aee = $this->_extra_data instanceof EE_Messages_Addressee ? $this->_extra_data : $aee;
130
+
131
+			$this->_event = $aee instanceof EE_Messages_Addressee && $aee->reg_obj instanceof EE_Registration
132
+				? $aee->reg_obj->event() : null;
133
+		}
134
+
135
+
136
+		// If there is no event objecdt by now then get out.
137
+		if (! $this->_event instanceof EE_Event) {
138
+			return '';
139
+		}
140
+
141
+		switch ($shortcode) {
142
+			case '[EVENT_ID]':
143
+				return $this->_event->ID();
144
+				break;
145
+
146
+			case '[EVENT]':
147
+			case '[EVENT_NAME]':
148
+				return $this->_event->get('EVT_name');
149
+				break;
150
+
151
+			case '[EVENT_PHONE]':
152
+				return $this->_event->get('EVT_phone');
153
+				break;
154
+
155
+			case '[EVENT_DESCRIPTION]':
156
+				return $this->_event->get('EVT_desc');
157
+				break;
158
+
159
+			case '[EVENT_EXCERPT]':
160
+				return $this->_event->get('EVT_short_desc');
161
+				break;
162
+
163
+			case '[EVENT_LINK]':
164
+				return $this->_get_event_link($this->_event);
165
+				break;
166
+
167
+			case '[EVENT_URL]':
168
+				return $this->_get_event_link($this->_event, false);
169
+				break;
170
+
171
+			case '[VIRTUAL_URL]':
172
+				$venue = $this->_event->get_first_related('Venue');
173
+				if (empty($venue)) {
174
+					return '';
175
+				}
176
+				return $venue->get('VNU_virtual_url');
177
+
178
+			case '[VIRTUAL_PHONE]':
179
+				$venue = $this->_event->get_first_related('Venue');
180
+				if (empty($venue)) {
181
+					return '';
182
+				}
183
+				return $venue->get('VNU_virtual_phone');
184
+				break;
185
+
186
+			case '[EVENT_IMAGE]':
187
+				$image = $this->_event->feature_image_url(array(600, 300));
188
+				// @todo: eventually we should make this an attribute shortcode so that em can send along what size they want returned.
189
+				return ! empty($image)
190
+					? '<img src="' . $image . '" alt="'
191
+					  . sprintf(
192
+						  esc_attr__('%s Feature Image', 'event_espresso'),
193
+						  $this->_event->get('EVT_name')
194
+					  ) . '" />'
195
+					: '';
196
+				break;
197
+
198
+			case '[EVENT_FACEBOOK_URL]':
199
+				$facebook_url = $this->_event->get_post_meta('event_facebook', true);
200
+				return empty($facebook_url) ? EE_Registry::instance()->CFG->organization->get_pretty('facebook')
201
+					: $facebook_url;
202
+				break;
203
+
204
+			case '[EVENT_TWITTER_URL]':
205
+				$twitter_url = $this->_event->get_post_meta('event_twitter', true);
206
+				return empty($twitter_url) ? EE_Registry::instance()->CFG->organization->get_pretty('twitter')
207
+					: $twitter_url;
208
+				break;
209
+
210
+			case '[EVENT_AUTHOR_EMAIL]':
211
+				$author_id = $this->_event->get('EVT_wp_user');
212
+				$user_data = get_userdata((int) $author_id);
213
+				return $user_data->user_email;
214
+				break;
215
+
216
+			case '[EVENT_TOTAL_SPOTS_TAKEN]':
217
+				return EEM_Registration::instance()->count(
218
+					array(array('EVT_ID' => $this->_event->ID(), 'STS_ID' => EEM_Registration::status_id_approved)),
219
+					'REG_ID',
220
+					true
221
+				);
222
+				break;
223
+
224
+			case '[REGISTRATION_LIST_TABLE_FOR_EVENT_URL]':
225
+				return EEH_URL::add_query_args_and_nonce(
226
+					array(
227
+						'event_id' => $this->_event->ID(),
228
+						'page'     => 'espresso_registrations',
229
+						'action'   => 'default',
230
+					),
231
+					admin_url('admin.php'),
232
+					true
233
+				);
234
+				break;
235
+		}
236
+
237
+		if (strpos($shortcode, '[EVENT_META_*') !== false) {
238
+			// Strip the shortcode itself from $shortcode leaving any attributes set.
239
+			// Removing the * is correct here as _* is used to indiciate a dynamic shortcode.
240
+			$shortcode = str_replace('[EVENT_META_*', '', $shortcode);
241
+			$shortcode = trim(str_replace(']', '', $shortcode));
242
+			// Get any attributes set on this shortcode.
243
+			$attrs = $this->_get_shortcode_attrs($shortcode);
244
+			// The meta_key set on the shortcode should always be the first value in the array.
245
+			$meta_key = $attrs[0];
246
+			// Pull the meta value from the event post.
247
+			$event_meta = $this->_event->get_post_meta($meta_key, true);
248
+			// If we have no event_meta, just return an empty string.
249
+			if (empty($event_meta)) {
250
+				return '';
251
+			}
252
+			// Add a filter to allow all instances of EVENT_META_* to run through do_shortcode, default to false.
253
+			// Check if a do_shortcode attribute was set to true and if so run $event_meta through that function.
254
+			if (
255
+				apply_filters('FHEE__EventEspresso_core_libraries_shortcodes_EE_Event_Shortcodes___parser__event_meta_do_shortcode', false)
256
+				|| !empty($attrs['do_shortcode']) && filter_var($attrs['do_shortcode'], FILTER_VALIDATE_BOOLEAN)
257
+			) {
258
+				return do_shortcode($event_meta);
259
+			}
260
+			// Still here? We just need to return the event_meta value as is.
261
+			return $event_meta;
262
+		}
263
+
264
+		if (strpos($shortcode, '[EVENT_TOTAL_AVAILABLE_SPACES_*') !== false) {
265
+			$attrs = $this->_get_shortcode_attrs($shortcode);
266
+			$method = empty($attrs['method']) ? 'current' : $attrs['method'];
267
+			$method = $method === 'current';
268
+			$available = $this->_event->total_available_spaces($method);
269
+			return $available === EE_INF ? '&infin;' : $available;
270
+		}
271
+
272
+		if (strpos($shortcode, '[EVENT_IMAGE_*') !== false) {
273
+			$attrs = $this->_get_shortcode_attrs($shortcode);
274
+			$width = empty($attrs['width']) ? '' : ' width="' . $attrs['width'] . '"';
275
+			$height = empty($attrs['height']) ? '' : ' height="' . $attrs['height'] . '"';
276
+
277
+			// Size may be set to a string such as 'tumbnail' or "width, height" eg - '200,200'
278
+			if (! empty($attrs['size'])) {
279
+				$size = explode(',', $attrs['size']);
280
+				if (count($size) === 1) {
281
+					$size = $size[0];
282
+				}
283
+			} else {
284
+				$size = 'thumbnail';
285
+			}
286
+
287
+			$image = $this->_event->feature_image_url($size);
288
+
289
+			return ! empty($image)
290
+				? '<img src="' . $image . '" alt="'
291
+				  . sprintf(
292
+					  esc_attr__('%s Feature Image', 'event_espresso'),
293
+					  $this->_event->get('EVT_name')
294
+				  ) . '"' . $width . $height . '/>'
295
+				: '';
296
+		}
297
+
298
+		return '';
299
+	}
300
+
301
+
302
+	/**
303
+	 * returns the link to the event
304
+	 *
305
+	 * @param  boolean $full_link if TRUE (default) we return the html for the name of the event linked to the event.
306
+	 *                            Otherwise we just return the url of the event.
307
+	 * @return string
308
+	 */
309
+	private function _get_event_link($event, $full_link = true)
310
+	{
311
+		$url = get_permalink($event->ID());
312
+
313
+		return $full_link ? '<a href="' . $url . '">' . $event->get('EVT_name') . '</a>' : $url;
314
+	}
315 315
 }
Please login to merge, or discard this patch.