| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Data_Apps { |
| 4 |
|
| 5 |
use WPDataAccess\API\WPDA_API_Core; |
| 6 |
use WPDataAccess\API\WPDA_Apps; |
| 7 |
use WPDataAccess\Plugin_Table_Models\WPDA_App_Apps_Model; |
| 8 |
use WPDataAccess\Plugin_Table_Models\WPDA_App_Model; |
| 9 |
use WPDataAccess\WPDA; |
| 10 |
|
| 11 |
class WPDA_App_Container extends WPDA_Container { |
| 12 |
|
| 13 |
private $app_id = ''; |
| 14 |
|
| 15 |
public function __construct( |
| 16 |
$args = array(), |
| 17 |
$shortcode_args = array() |
| 18 |
) { |
| 19 |
|
| 20 |
parent::__construct( $args ); |
| 21 |
|
| 22 |
if ( isset( $args['app_id'] ) ) { |
| 23 |
$this->app_id = $args['app_id']; |
| 24 |
} |
| 25 |
|
| 26 |
if ( |
| 27 |
isset( $args['builders'] ) && |
| 28 |
( |
| 29 |
false === $args['builders'] || |
| 30 |
'false' === $args['builders'] |
| 31 |
) |
| 32 |
) { |
| 33 |
$this->builders = false; |
| 34 |
} |
| 35 |
|
| 36 |
$this->shortcode_args = $shortcode_args; |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
private function get_app_metadata( $app_id ) { |
| 41 |
|
| 42 |
$app = new WPDA_Apps(); |
| 43 |
$response = $app->get_app_meta( $app_id ); |
| 44 |
|
| 45 |
if ( |
| 46 |
isset( $response->data['code'], $response->data['data'] ) && |
| 47 |
'ok' === $response->data['code'] |
| 48 |
) { |
| 49 |
return $response->data['data']; |
| 50 |
} |
| 51 |
|
| 52 |
return null; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
public function show() { |
| 57 |
|
| 58 |
$app = WPDA_App_Model::get_by_id( $this->app_id ); |
| 59 |
if ( false === $app ) { |
| 60 |
if ( ! $this->send_feedback() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$this->show_feedback( __( 'Not authorized', 'wp-data-access' ) ); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! $this->user_can_access( $app ) ) { |
| 69 |
if ( $this->pwa ) { |
| 70 |
esc_html_e( 'Not authorized', 'wp-data-access' ); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! $this->send_feedback() ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$this->show_feedback( __( 'Not authorized', 'wp-data-access' ) ); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$metadata = array(); |
| 83 |
$metadata_master = $this->get_app_metadata( $this->app_id); |
| 84 |
|
| 85 |
if ( null !== $metadata_master) { |
| 86 |
$metadata[$this->app_id] = $metadata_master; |
| 87 |
} |
| 88 |
|
| 89 |
if ( isset( $app[0]['app_type'] ) && '5' == $app[0]['app_type'] ) { |
| 90 |
// Add metadata children |
| 91 |
$detail_apps = WPDA_App_Apps_Model::select_all( $this->app_id ); |
| 92 |
foreach ( $detail_apps as $detail_app ) { |
| 93 |
if ( isset( $detail_app['app_id_detail'] ) ) { |
| 94 |
$app_id_detail = $detail_app['app_id_detail']; |
| 95 |
$metadata_detail = $this->get_app_metadata($app_id_detail); |
| 96 |
if (null !== $metadata_detail) { |
| 97 |
$metadata[$app_id_detail] = $metadata_detail; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$app_type_class = ''; |
| 104 |
switch ($app[0]['app_type']) { |
| 105 |
case '2': |
| 106 |
// Map |
| 107 |
$app_type_class = 'pp-container-map'; |
| 108 |
break; |
| 109 |
case '3': |
| 110 |
// Registration form |
| 111 |
$app_type_class = 'pp-container-registration'; |
| 112 |
break; |
| 113 |
case '5': |
| 114 |
// Data App |
| 115 |
$app_type_class = 'pp-container-apps'; |
| 116 |
break; |
| 117 |
case '6': |
| 118 |
// Chart |
| 119 |
$app_type_class = 'pp-container-chart'; |
| 120 |
break; |
| 121 |
case '7': |
| 122 |
// Chart |
| 123 |
$app_type_class = 'pp-container-dashboard'; |
| 124 |
break; |
| 125 |
} |
| 126 |
?> |
| 127 |
|
| 128 |
<div class="wpda-pp-container"> |
| 129 |
<div |
| 130 |
class="pp-container-app <?php echo esc_attr( $app_type_class ); ?>" |
| 131 |
data-source="{ 'id': '<?php echo esc_attr( $this->app_id ); ?>' }" |
| 132 |
|
| 133 |
<?php |
| 134 |
if ( null !== $this->filter_field_name && null !== $this->filter_field_value ) { |
| 135 |
?> |
| 136 |
data-filter_field_name="<?php echo esc_attr( $this->filter_field_name ); ?>" |
| 137 |
data-filter_field_value="<?php echo esc_attr( $this->filter_field_value ); ?>" |
| 138 |
<?php |
| 139 |
} |
| 140 |
|
| 141 |
if ( 0 < count( $this->shortcode_args ) ) { |
| 142 |
$field_names = array(); |
| 143 |
$field_values = array(); |
| 144 |
foreach ( $this->shortcode_args as $key => $value ) { |
| 145 |
// Sanitize field name |
| 146 |
$field_names[] = WPDA_API_Core::sanitize_db_identifier( $key ); |
| 147 |
// Sanitize field value |
| 148 |
$sanitized_value = sanitize_text_field( $value ); |
| 149 |
$sanitized_value = wp_strip_all_tags( $sanitized_value ); |
| 150 |
$sanitized_value = str_replace( array( '"', "'", '`' ), '', $sanitized_value ); |
| 151 |
$sanitized_value = str_replace( array( '<', '>' ), '', $sanitized_value ); |
| 152 |
$sanitized_value = str_replace( '=', '', $sanitized_value ); |
| 153 |
$field_values[] = $sanitized_value; |
| 154 |
} |
| 155 |
?> |
| 156 |
data-shortcode_field_name="<?php echo esc_attr( implode( ',', $field_names) ); ?>" |
| 157 |
data-shortcode_field_value="<?php echo esc_attr( implode( ',', $field_values ) ); ?>" |
| 158 |
<?php |
| 159 |
} |
| 160 |
?> |
| 161 |
></div> |
| 162 |
</div> |
| 163 |
|
| 164 |
<?php |
| 165 |
if ( 0 < count( $metadata ) ) { |
| 166 |
// Add metadata detail apps to decrease load time |
| 167 |
?> |
| 168 |
<script> |
| 169 |
<?php |
| 170 |
foreach ( $metadata as $app_id => $data ) { |
| 171 |
?> |
| 172 |
window.pp_app_<?php echo esc_attr( $app_id ); ?> = { |
| 173 |
metadata: <?php echo json_encode( $data, true ); ?> |
| 174 |
} |
| 175 |
<?php |
| 176 |
} |
| 177 |
?> |
| 178 |
</script> |
| 179 |
<?php |
| 180 |
} |
| 181 |
?> |
| 182 |
|
| 183 |
<?php |
| 184 |
|
| 185 |
$this->add_client('app', $this->app_id ); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
private function user_can_access( $app ) { |
| 190 |
|
| 191 |
if ( ! isset ( $app[0]['app_settings'] ) ) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
// Check access |
| 196 |
$app_settings_db = $app[0]['app_settings']; |
| 197 |
$app_settings = json_decode( (string) $app_settings_db, true ); |
| 198 |
if ( |
| 199 |
! isset( |
| 200 |
$app_settings['rest_api']['authorization'], |
| 201 |
$app_settings['rest_api']['authorized_roles'], |
| 202 |
$app_settings['rest_api']['authorized_users'] |
| 203 |
) || |
| 204 |
! is_array( $app_settings['rest_api']['authorized_roles'] ) || |
| 205 |
! is_array( $app_settings['rest_api']['authorized_users'] ) |
| 206 |
) { |
| 207 |
// App contain no rest api settings |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
if ( |
| 212 |
! WPDA::current_user_is_admin() && |
| 213 |
'anonymous' !== $app_settings['rest_api']['authorization'] |
| 214 |
) { |
| 215 |
// Check authorization |
| 216 |
// Check user role |
| 217 |
$user_roles = WPDA::get_current_user_roles(); |
| 218 |
if ( |
| 219 |
! is_array( $user_roles ) || |
| 220 |
empty( |
| 221 |
array_intersect( |
| 222 |
$app_settings['rest_api']['authorized_roles'], |
| 223 |
$user_roles |
| 224 |
) |
| 225 |
) |
| 226 |
) { |
| 227 |
// Check user login |
| 228 |
$user_login = WPDA::get_current_user_login(); |
| 229 |
if ( ! in_array( $user_login, $app_settings['rest_api']['authorized_users'] ) ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
// Anonymous access |
| 236 |
return true; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
} |