| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Utilities; |
| 4 |
|
| 5 |
use WPDataAccess\Data_Apps\WPDA_App_Container; |
| 6 |
use WPDataAccess\Plugin_Table_Models\WPDA_App_Model; |
| 7 |
use WPDataAccess\WPDA; |
| 8 |
|
| 9 |
class WPDA_Add_App_To_Menu { |
| 10 |
|
| 11 |
private $wp_admin_toolbar = array(); |
| 12 |
|
| 13 |
public function add_apps_to_menu() { |
| 14 |
|
| 15 |
$apps = WPDA_App_Model::add_to_dashboard_menu(); |
| 16 |
foreach ( $apps as $app ) { |
| 17 |
$settings = json_decode( $app['app_settings'], true ); |
| 18 |
if ( |
| 19 |
! isset( |
| 20 |
$settings['rest_api']['authorization'], |
| 21 |
$settings['rest_api']['authorized_roles'], |
| 22 |
$settings['rest_api']['authorized_users'], |
| 23 |
$settings['settings']['app_menu_title'] |
| 24 |
) |
| 25 |
) { |
| 26 |
continue; |
| 27 |
} |
| 28 |
if ( $settings['rest_api']['authorization'] === 'authorized' ) { |
| 29 |
$authorized_roles = $settings['rest_api']['authorized_roles']; |
| 30 |
$current_user_roles = WPDA::get_current_user_roles(); |
| 31 |
if ( 0 === count( array_intersect( $authorized_roles, $current_user_roles ) ) ) { |
| 32 |
$authorized_users = $settings['rest_api']['authorized_users']; |
| 33 |
$current_user_name = WPDA::get_current_user_login(); |
| 34 |
if ( ! in_array( $current_user_name, $authorized_users ) ) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
$title = $settings['settings']['app_menu_title']; |
| 41 |
$icon = 'dashicons-database-view'; |
| 42 |
switch ($app['app_type']) { |
| 43 |
case 1: |
| 44 |
case 3: |
| 45 |
case 4: |
| 46 |
$icon = 'dashicons-edit'; |
| 47 |
break; |
| 48 |
case 2: |
| 49 |
$icon = 'dashicons-location'; |
| 50 |
break; |
| 51 |
case 6: |
| 52 |
$icon = 'dashicons-chart-bar'; |
| 53 |
break; |
| 54 |
case 7: |
| 55 |
$icon = 'dashicons-dashboard'; |
| 56 |
break; |
| 57 |
} |
| 58 |
if ( is_admin() ) { |
| 59 |
// Add app to dashboard menu |
| 60 |
add_menu_page( |
| 61 |
$title, |
| 62 |
$title, |
| 63 |
WPDA::get_current_user_capability(), |
| 64 |
$title, |
| 65 |
null, |
| 66 |
$icon |
| 67 |
); |
| 68 |
|
| 69 |
add_submenu_page( |
| 70 |
$title, |
| 71 |
$title, |
| 72 |
$title, |
| 73 |
WPDA::get_current_user_capability(), |
| 74 |
$title, |
| 75 |
function() use ( $app, $title, $settings ) { |
| 76 |
// Style not enqueued from container class. |
| 77 |
// Adding container style manually. |
| 78 |
$fullscreen = isset( $settings['settings']['app_start_full_screen'] ) ? $settings['settings']['app_start_full_screen'] : false; |
| 79 |
$args = array( |
| 80 |
'app_id' => $app['app_id'], |
| 81 |
'feedback' => true, |
| 82 |
'fullscreen' => $fullscreen, |
| 83 |
); |
| 84 |
$app_container = new WPDA_App_Container( $args ); |
| 85 |
?> |
| 86 |
<div class="wrap wpda-dashboard-app"> |
| 87 |
<h1 class="wp-heading-inline"> |
| 88 |
<?php echo esc_html( $title ); ?> |
| 89 |
</h1> |
| 90 |
<?php $app_container->show(); ?> |
| 91 |
</div> |
| 92 |
<style> |
| 93 |
.wrap.wpda-dashboard-app .pp-container-app { |
| 94 |
margin: 20px 0 0 0; |
| 95 |
} |
| 96 |
</style> |
| 97 |
<?php |
| 98 |
} |
| 99 |
); |
| 100 |
} else { |
| 101 |
// Add app to WordPress toolbar |
| 102 |
$this->wp_admin_toolbar[ $app['app_id'] ][] = array( |
| 103 |
'menu_id' => $title, |
| 104 |
'menu_name' => $title, |
| 105 |
'menu_title' => $title, |
| 106 |
'page_title' => $title, |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! is_admin() ) { |
| 112 |
foreach ( $this->wp_admin_toolbar as $pid => $toolbar ) { |
| 113 |
foreach ( $toolbar as $key => $menu ) { |
| 114 |
if ( 0 === $key ) { |
| 115 |
$this->add_item_to_toolbar( $pid, $menu ); |
| 116 |
} |
| 117 |
$this->add_item_to_toolbar( $menu['menu_id'], $menu, $pid ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
private function add_item_to_toolbar($pid, $menu, $parent = null ) { |
| 125 |
|
| 126 |
global $wp_admin_bar; |
| 127 |
$args = array( |
| 128 |
'id' => $pid, |
| 129 |
'title' => null === $parent ? $menu['menu_name'] : $menu['page_title'], |
| 130 |
'href' => admin_url('admin.php') . '?page=' . $menu['menu_id'], |
| 131 |
'zindex' => '9999', |
| 132 |
'meta' =>array( |
| 133 |
'class' => null === $parent ? 'wpda-wpdp-toolbar' : '', |
| 134 |
'title' => null === $parent ? $menu['menu_title'] : $menu['page_title'], |
| 135 |
) |
| 136 |
); |
| 137 |
if ( null !== $parent ) { |
| 138 |
$args['parent'] = $parent; |
| 139 |
} |
| 140 |
$wp_admin_bar->add_node( $args ); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
} |