get_campaign_stats(); $posts = $this->get_blog_posts(); $is_pro = Disco::is_pro(); $has_campaigns = $stats['total'] > 0; $new_campaign_url = admin_url( 'admin.php?page=disco-create-discount#/disco' ); $campaigns_url = admin_url( 'admin.php?page=disco-create-discount' ); $settings_url = admin_url( 'admin.php?page=disco-create-discount#/settings' ); $pro_url = 'https://discoplugin.com/?utm_source=wp_wedget&utm_medium=free-pro&utm_campaign=free-pro&utm_id=1#pricing'; $blog_url = 'https://discoplugin.com/blog/'; $docs_url = 'https://discoplugin.com/docs/'; $support_url = 'https://wordpress.org/support/plugin/disco/'; $review_url = 'https://wordpress.org/support/plugin/disco/reviews/#new-post'; ?>
get_results( //phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT status, data FROM {$wpdb->prefix}disco_campaigns", OBJECT ); $total = 0; $active = 0; $expiring_soon = 0; $now = current_time( 'timestamp' ); //phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested $seven_days = $now + ( 7 * DAY_IN_SECONDS ); foreach ( $rows as $row ) { $total++; if ( '1' === $row->status ) { $active++; } if ( ! empty( $row->data ) ) { $data = json_decode( $row->data, true ); if ( ! empty( $data['discount_valid_to'] ) ) { $expires = strtotime( $data['discount_valid_to'] ); if ( $expires && $expires >= $now && $expires <= $seven_days ) { $expiring_soon++; } } } } return array( 'total' => $total, 'active' => $active, 'expiring_soon' => $expiring_soon, ); } /** * Fetch the latest 3 blog posts from discoplugin.com with transient caching. * * @return array Array of posts with 'title', 'link', 'category', and 'is_new' keys. */ private function get_blog_posts() { $cache_key = 'disco_blog_posts_cache'; $cached = get_transient( $cache_key ); if ( false !== $cached ) { return $cached; } $response = wp_remote_get( 'https://discoplugin.com/wp-json/wp/v2/posts?_embed&per_page=3&orderby=date&order=desc', array( 'timeout' => 8 ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return array(); } $body = wp_remote_retrieve_body( $response ); $items = json_decode( $body, true ); if ( ! is_array( $items ) ) { return array(); } $posts = array(); $two_weeks = 14 * DAY_IN_SECONDS; foreach ( $items as $item ) { $title = isset( $item['title']['rendered'] ) ? wp_strip_all_tags( $item['title']['rendered'] ) : ''; $link = isset( $item['link'] ) ? esc_url_raw( $item['link'] ) : ''; if ( empty( $title ) || empty( $link ) ) { continue; } // Category from embedded terms. $category = ''; if ( ! empty( $item['_embedded']['wp:term'] ) ) { foreach ( $item['_embedded']['wp:term'] as $term_group ) { foreach ( $term_group as $term ) { if ( isset( $term['taxonomy'] ) && 'category' === $term['taxonomy'] ) { $category = sanitize_text_field( $term['name'] ); break 2; } } } } // "NEW" badge if published within the last 14 days. $is_new = false; if ( ! empty( $item['date_gmt'] ) ) { $published = strtotime( $item['date_gmt'] ); $is_new = $published && ( time() - $published ) < $two_weeks; } $posts[] = array( 'title' => $title, 'link' => $link, 'category' => $category, 'is_new' => $is_new, ); } // Cache for 6 hours. set_transient( $cache_key, $posts, 6 * HOUR_IN_SECONDS ); return $posts; } }