| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace WPDataAccess\Data_Apps { |
| 4 | 4 | |
| 5 | - use WPDataAccess\Plugin_Table_Models\WPDA_App_Model; | |
| 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; | |
| 6 | 9 | use WPDataAccess\WPDA; |
| 7 | 10 | |
| 8 | 11 | class WPDA_App_Container extends WPDA_Container { |
| 9 | 12 | |
| @@ -9,10 +12,11 @@ | ||
| 9 | 12 | |
| 10 | 13 | private $app_id = ''; |
| 11 | 14 | |
| 12 | 15 | public function __construct( |
| 13 | - $args = array() | |
| 14 | - ) { | |
| 16 | + $args = array(), | |
| 17 | + $shortcode_args = array() | |
| 18 | + ) { | |
| 15 | 19 | |
| 16 | 20 | parent::__construct( $args ); |
| 17 | 21 | |
| 18 | 22 | if ( isset( $args['app_id'] ) ) { |
| @@ -18,10 +22,38 @@ | ||
| 18 | 22 | if ( isset( $args['app_id'] ) ) { |
| 19 | 23 | $this->app_id = $args['app_id']; |
| 20 | 24 | } |
| 21 | 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 | + | |
| 22 | 38 | } |
| 23 | 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 | + | |
| 24 | 56 | public function show() { |
| 25 | 57 | |
| 26 | 58 | $app = WPDA_App_Model::get_by_id( $this->app_id ); |
| 27 | 59 | if ( false === $app ) { |
| @@ -28,13 +60,18 @@ | ||
| 28 | 60 | if ( ! $this->send_feedback() ) { |
| 29 | 61 | return; |
| 30 | 62 | } |
| 31 | 63 | |
| 32 | - $this->show_feedback( __( 'Invalid app id', 'wp-data-access' ) ); | |
| 64 | + $this->show_feedback( __( 'Not authorized', 'wp-data-access' ) ); | |
| 33 | 65 | return; |
| 34 | 66 | } |
| 35 | 67 | |
| 36 | 68 | if ( ! $this->user_can_access( $app ) ) { |
| 69 | + if ( $this->pwa ) { | |
| 70 | + esc_html_e( 'Not authorized', 'wp-data-access' ); | |
| 71 | + return; | |
| 72 | + } | |
| 73 | + | |
| 37 | 74 | if ( ! $this->send_feedback() ) { |
| 38 | 75 | return; |
| 39 | 76 | } |
| 40 | 77 | |
| @@ -41,20 +78,112 @@ | ||
| 41 | 78 | $this->show_feedback( __( 'Not authorized', 'wp-data-access' ) ); |
| 42 | 79 | return; |
| 43 | 80 | } |
| 44 | 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 | + } | |
| 45 | 126 | ?> |
| 46 | 127 | |
| 47 | 128 | <div class="wpda-pp-container"> |
| 48 | 129 | <div |
| 49 | - class="pp-container-app" | |
| 50 | - data-source="{ 'id': '<?php echo $this->app_id; ?>' }" | |
| 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 | + ?> | |
| 51 | 161 | ></div> |
| 52 | 162 | </div> |
| 53 | 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 | + | |
| 54 | 183 | <?php |
| 55 | 184 | |
| 56 | - $this->add_client(); | |
| 185 | + $this->add_client('app', $this->app_id ); | |
| 57 | 186 | |
| 58 | 187 | } |
| 59 | 188 | |
| 60 | 189 | private function user_can_access( $app ) { |
| @@ -79,9 +208,9 @@ | ||
| 79 | 208 | return false; |
| 80 | 209 | } |
| 81 | 210 | |
| 82 | 211 | if ( |
| 83 | - ! current_user_can( 'manage_options' ) && | |
| 212 | + ! WPDA::current_user_is_admin() && | |
| 84 | 213 | 'anonymous' !== $app_settings['rest_api']['authorization'] |
| 85 | 214 | ) { |
| 86 | 215 | // Check authorization |
| 87 | 216 | // Check user role |
| @@ -97,13 +226,14 @@ | ||
| 97 | 226 | ) { |
| 98 | 227 | // Check user login |
| 99 | 228 | $user_login = WPDA::get_current_user_login(); |
| 100 | 229 | if ( ! in_array( $user_login, $app_settings['rest_api']['authorized_users'] ) ) { |
| 101 | - return false; | |
| 230 | + return false; | |
| 102 | 231 | } |
| 103 | 232 | } |
| 104 | 233 | } |
| 105 | 234 | |
| 235 | + // Anonymous access | |
| 106 | 236 | return true; |
| 107 | 237 | |
| 108 | 238 | } |
| 109 | 239 | |