| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 3 | +use LearnPress\Helpers\Template; | |
| 4 | +use LearnPress\Models\UserModel; | |
| 5 | +use LearnPress\TemplateHooks\TemplateAJAX; | |
| 6 | + | |
| 2 | 7 | if ( ! class_exists( 'LP_Admin_Dashboard' ) ) { |
| 3 | 8 | /** |
| 4 | 9 | * Class LP_Admin_Dashboard |
| 5 | 10 | * |
| @@ -5,36 +10,39 @@ | ||
| 5 | 10 | * |
| 6 | 11 | * Displays widgets in admin. |
| 7 | 12 | */ |
| 8 | 13 | class LP_Admin_Dashboard { |
| 9 | - | |
| 10 | 14 | /** |
| 11 | 15 | * LP_Admin_Dashboard constructor. |
| 12 | 16 | */ |
| 13 | 17 | public function __construct() { |
| 18 | + // Ignore heartbeat requests. | |
| 19 | + if ( isset( $_POST['action'] ) && 'heartbeat' === $_POST['action'] ) { | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + | |
| 14 | 23 | add_action( 'wp_dashboard_setup', array( $this, 'register' ) ); |
| 15 | 24 | } |
| 16 | 25 | |
| 17 | 26 | public function register() { |
| 18 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'publish_lp_orders' ) ) { | |
| 27 | + $screens = [ | |
| 28 | + 'learn_press_dashboard_order_statuses' => [ | |
| 29 | + 'label' => esc_html__( 'LearnPress order status', 'learnpress' ), | |
| 30 | + 'callback' => [ $this, 'order_statuses' ], | |
| 31 | + ], | |
| 32 | + 'learn_press_dashboard_plugin_status' => [ | |
| 33 | + 'label' => esc_html__( 'LearnPress status', 'learnpress' ), | |
| 34 | + 'callback' => [ $this, 'plugin_status' ], | |
| 35 | + ], | |
| 36 | + ]; | |
| 37 | + | |
| 38 | + foreach ( $screens as $id => $screen ) { | |
| 19 | 39 | wp_add_dashboard_widget( |
| 20 | - 'learn_press_dashboard_order_statuses', | |
| 21 | - esc_html__( 'LearnPress order status', 'learnpress' ), | |
| 22 | - array( | |
| 23 | - $this, | |
| 24 | - 'order_statuses', | |
| 25 | - ) | |
| 40 | + $id, | |
| 41 | + $screen['label'], | |
| 42 | + $screen['callback'] | |
| 26 | 43 | ); |
| 27 | 44 | } |
| 28 | - | |
| 29 | - wp_add_dashboard_widget( | |
| 30 | - 'learn_press_dashboard_plugin_status', | |
| 31 | - esc_html__( 'LearnPress status', 'learnpress' ), | |
| 32 | - array( | |
| 33 | - $this, | |
| 34 | - 'plugin_status', | |
| 35 | - ) | |
| 36 | - ); | |
| 37 | 45 | } |
| 38 | 46 | |
| 39 | 47 | /** |
| 40 | 48 | * Order status widget |
| @@ -39,149 +47,137 @@ | ||
| 39 | 47 | /** |
| 40 | 48 | * Order status widget |
| 41 | 49 | */ |
| 42 | 50 | public function order_statuses() { |
| 43 | - ?> | |
| 44 | - <ul class="lp-order-statuses lp_append_data"> | |
| 45 | - <?php lp_skeleton_animation_html( 4, 100, 'height: 30px;border-radius:4px;' ); ?> | |
| 46 | - </ul> | |
| 47 | - <ul class="lp-order-statuses"> | |
| 48 | - <?php | |
| 49 | - $eduma_data = $this->_get_theme_info( 14058034 ); | |
| 51 | + $args = [ | |
| 52 | + 'id_url' => 'order-statistic-dashboard', | |
| 53 | + ]; | |
| 50 | 54 | |
| 51 | - if ( function_exists( 'learn_press_get_item_referral' ) ) { | |
| 52 | - $eduma_data['url'] = learn_press_get_item_referral( 14058034 ); | |
| 53 | - } | |
| 55 | + /** | |
| 56 | + * @uses order_statistic | |
| 57 | + */ | |
| 58 | + $callback = [ | |
| 59 | + 'class' => self::class, | |
| 60 | + 'method' => 'order_statistic', | |
| 61 | + ]; | |
| 54 | 62 | |
| 55 | - if ( ! empty( $eduma_data ) ) { | |
| 56 | - ?> | |
| 57 | - | |
| 58 | - <li class="clear"></li> | |
| 59 | - <li class="featured-theme"> | |
| 60 | - <?php if ( isset( $eduma_data['name'] ) && isset( $eduma_data['price_cents'] ) ) : ?> | |
| 61 | - <p> | |
| 62 | - <a href="<?php echo esc_url_raw( $eduma_data['url'] ); ?>"> | |
| 63 | - <?php echo esc_html( $eduma_data['name'] ); ?> | |
| 64 | - </a> - <?php printf( '%s%s', '$', $eduma_data['price_cents'] / 100 ); ?> | |
| 65 | - </p> | |
| 66 | - <?php endif; ?> | |
| 67 | - | |
| 68 | - <?php if ( isset( $eduma_data['rating']['count'] ) && isset( $eduma_data['rating']['rating'] ) ) : ?> | |
| 69 | - <div> | |
| 70 | - <?php | |
| 71 | - wp_star_rating( | |
| 72 | - array( | |
| 73 | - 'rating' => $eduma_data['rating']['rating'], | |
| 74 | - 'type' => 'rating', | |
| 75 | - 'number' => $eduma_data['rating']['count'], | |
| 76 | - ) | |
| 77 | - ); | |
| 78 | - ?> | |
| 79 | - | |
| 80 | - <span class="count-rating">(<?php echo esc_html( $eduma_data['rating']['count'] ); ?>)</span> | |
| 81 | - <span> | |
| 82 | - - <?php echo sprintf( '%d %s', esc_html( $eduma_data['number_of_sales'] ), esc_html__( ' sales', 'learnpress' ) ); ?> | |
| 83 | - </span> | |
| 84 | - </div> | |
| 85 | - <?php endif; ?> | |
| 86 | - | |
| 87 | - <?php if ( isset( $eduma_data['author_username'] ) ) : ?> | |
| 88 | - <p> | |
| 89 | - <?php esc_html_e( 'Created by: ', 'learnpress' ); ?> | |
| 90 | - <a href="https://thimpress.com/" class="author"><?php echo esc_html( $eduma_data['author_username'] ); ?></a> | |
| 91 | - </p> | |
| 92 | - <?php endif; ?> | |
| 93 | - </li> | |
| 94 | - <?php } ?> | |
| 95 | - </ul> | |
| 96 | - <?php | |
| 63 | + echo TemplateAJAX::load_content_via_ajax( $args, $callback ); | |
| 97 | 64 | } |
| 98 | 65 | |
| 99 | 66 | /** |
| 100 | - * Get total value of LP orders has completed. | |
| 67 | + * Get order statistic content | |
| 101 | 68 | * |
| 102 | - * @return int|string | |
| 69 | + * @return stdClass | |
| 103 | 70 | */ |
| 104 | - private function _get_order_total_raised() { | |
| 105 | - $orders = learn_press_get_orders( array( 'post_status' => 'lp-completed' ) ); | |
| 106 | - $total = 0; | |
| 71 | + public static function order_statistic(): stdClass { | |
| 72 | + // Check permission | |
| 73 | + if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { | |
| 74 | + wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'learnpress' ) ); | |
| 75 | + } | |
| 107 | 76 | |
| 108 | - if ( $orders ) { | |
| 109 | - foreach ( $orders as $order ) { | |
| 110 | - $order = learn_press_get_order( $order->ID ); | |
| 111 | - $total = $total + floatval( $order->order_total ); | |
| 112 | - } | |
| 113 | - } | |
| 77 | + $order_statuses = LP_Order::get_order_statuses(); | |
| 78 | + $lp_order_icons = LP_Order::get_icons_status(); | |
| 114 | 79 | |
| 115 | - return learn_press_format_price( $total, true ); | |
| 80 | + ob_start(); | |
| 81 | + $data = compact( 'order_statuses', 'lp_order_icons' ); | |
| 82 | + Template::instance()->get_admin_template( 'dashboard/html-orders', $data ); | |
| 83 | + $content = new stdClass(); | |
| 84 | + $content->content = sprintf( | |
| 85 | + '<ul class="lp-order-statuses lp_append_data">%s</ul>', | |
| 86 | + ob_get_clean() | |
| 87 | + ); | |
| 88 | + return $content; | |
| 116 | 89 | } |
| 117 | 90 | |
| 118 | 91 | /** |
| 119 | - * @param String $item_id - The ID of an Envato Marketplace item | |
| 120 | - * | |
| 121 | - * @returns mixed | |
| 92 | + * Plugin status widget | |
| 122 | 93 | */ |
| 123 | - private function _get_theme_info( $item_id ) { | |
| 124 | - $alls = LP_Plugins_Helper::get_related_themes(); | |
| 94 | + public function plugin_status() { | |
| 95 | + $args = [ | |
| 96 | + 'id_url' => 'plugin-status-dashboard', | |
| 97 | + ]; | |
| 125 | 98 | |
| 126 | - if ( ! $alls ) { | |
| 127 | - return false; | |
| 128 | - } | |
| 99 | + /** | |
| 100 | + * @uses plugin_status_content | |
| 101 | + */ | |
| 102 | + $callback = [ | |
| 103 | + 'class' => self::class, | |
| 104 | + 'method' => 'plugin_status_content', | |
| 105 | + ]; | |
| 129 | 106 | |
| 130 | - foreach ( $alls as $k => $types ) { | |
| 131 | - if ( isset( $types[ $item_id ] ) ) { | |
| 132 | - return $types[ $item_id ]; | |
| 133 | - } | |
| 107 | + echo TemplateAJAX::load_content_via_ajax( $args, $callback ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + public static function plugin_status_content() { | |
| 111 | + if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { | |
| 112 | + wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'learnpress' ) ); | |
| 134 | 113 | } |
| 135 | 114 | |
| 136 | - return false; | |
| 137 | - } | |
| 138 | - | |
| 139 | - /** | |
| 140 | - * Get data from wordpress.org | |
| 141 | - * | |
| 142 | - * @since 2.0 | |
| 143 | - */ | |
| 144 | - private function _get_data() { | |
| 145 | 115 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 146 | 116 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 147 | 117 | |
| 148 | - $api = get_transient( 'lp_plugin_status' ); | |
| 118 | + $api = plugins_api( | |
| 119 | + 'plugin_information', | |
| 120 | + array( | |
| 121 | + 'slug' => 'learnpress', | |
| 122 | + 'fields' => array( | |
| 123 | + 'active_installs' => true, | |
| 124 | + 'short_description' => true, | |
| 125 | + 'description' => true, | |
| 126 | + 'ratings' => true, | |
| 127 | + 'downloaded' => true, | |
| 128 | + ), | |
| 129 | + ) | |
| 130 | + ); | |
| 149 | 131 | |
| 150 | - if ( false === $api || is_wp_error( $api ) ) { | |
| 151 | - $api = plugins_api( | |
| 152 | - 'plugin_information', | |
| 153 | - array( | |
| 154 | - 'slug' => 'learnpress', | |
| 155 | - 'fields' => array( | |
| 156 | - 'active_installs' => true, | |
| 157 | - 'short_description' => true, | |
| 158 | - 'description' => true, | |
| 159 | - 'ratings' => true, | |
| 160 | - 'downloaded' => true, | |
| 161 | - ), | |
| 162 | - ) | |
| 163 | - ); | |
| 132 | + $section = [ | |
| 133 | + 'wrap' => '<div class="lp-plugin-status-wrap">', | |
| 134 | + 'banner' => sprintf( | |
| 135 | + '<image class="lp-plugin-banner" src="%s" style="%s" />', | |
| 136 | + $api->banners ? ( $api->banners['low'] ?? '' ) : '', | |
| 137 | + 'max-width:100%;height:auto;' | |
| 138 | + ), | |
| 139 | + 'active_installs' => sprintf( | |
| 140 | + '<div class="lp-plugin-active-installs">%s: <strong>%s</strong></div>', | |
| 141 | + esc_html__( 'Active Installations', 'learnpress' ), | |
| 142 | + number_format_i18n( $api->active_installs ) | |
| 143 | + ), | |
| 144 | + 'downloaded' => sprintf( | |
| 145 | + '<div class="lp-plugin-downloaded">%s: <strong>%s</strong></div>', | |
| 146 | + esc_html__( 'Total Downloads', 'learnpress' ), | |
| 147 | + number_format_i18n( $api->downloaded ) | |
| 148 | + ), | |
| 149 | + 'ratings' => sprintf( | |
| 150 | + '<div class="lp-plugin-ratings">%s <strong>%s</strong></div>', | |
| 151 | + esc_html__( 'Ratings 5 stars is:', 'learnpress' ), | |
| 152 | + $api->ratings[5] ?? '0' | |
| 153 | + ), | |
| 154 | + 'requires' => sprintf( | |
| 155 | + '<div class="lp-plugin-requires">%s %s</div>', | |
| 156 | + esc_html__( 'Required WordPress version:', 'learnpress' ), | |
| 157 | + $api->requires ?? esc_html__( 'N/A', 'learnpress' ) | |
| 158 | + ), | |
| 159 | + 'requires_php' => sprintf( | |
| 160 | + '<div class="lp-plugin-requires_php">%s %s</div>', | |
| 161 | + esc_html__( 'Required PHP version:', 'learnpress' ), | |
| 162 | + $api->requires_php ?? esc_html__( 'N/A', 'learnpress' ) | |
| 163 | + ), | |
| 164 | + 'tested' => sprintf( | |
| 165 | + '<div class="lp-plugin-tested">%s %s</div>', | |
| 166 | + esc_html__( 'Tested with WordPress version:', 'learnpress' ), | |
| 167 | + $api->tested ?? esc_html__( 'N/A', 'learnpress' ) | |
| 168 | + ), | |
| 169 | + 'last_updated' => sprintf( | |
| 170 | + '<div class="lp-plugin-last_updated">%s %s</div>', | |
| 171 | + esc_html__( 'Latest updated:', 'learnpress' ), | |
| 172 | + $api->last_updated ?? esc_html__( 'N/A', 'learnpress' ) | |
| 173 | + ), | |
| 174 | + 'wrap-end' => '</div>', | |
| 175 | + ]; | |
| 164 | 176 | |
| 165 | - if ( ! is_wp_error( $api ) ) { | |
| 166 | - set_transient( 'lp_plugin_status', $api, 12 * HOUR_IN_SECONDS ); | |
| 167 | - } | |
| 168 | - } | |
| 169 | - | |
| 170 | - return $api; | |
| 171 | - } | |
| 172 | - | |
| 173 | - /** | |
| 174 | - * Plugin status widget | |
| 175 | - */ | |
| 176 | - public function plugin_status() { | |
| 177 | - $plugin_data = $this->_get_data(); | |
| 178 | - | |
| 179 | - if ( ! $plugin_data || is_wp_error( $plugin_data ) ) { | |
| 180 | - learn_press_admin_view( 'dashboard/plugin-status/html-no-data' ); | |
| 181 | - } else { | |
| 182 | - learn_press_admin_view( 'dashboard/plugin-status/html-results', array( 'plugin_data' => $plugin_data ) ); | |
| 183 | - } | |
| 177 | + $content = new stdClass(); | |
| 178 | + $content->content = Template::combine_components( $section ); | |
| 179 | + return $content; | |
| 184 | 180 | } |
| 185 | 181 | } |
| 186 | 182 | } |
| 187 | 183 | new LP_Admin_Dashboard(); |