Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/tools/exporters/SubscriptionExporter.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Subscription Exporter.
4 + *
5 + * @package TutorPro\Tools
6 + * @author Themeum<support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.8.0
9 + */
10 +
11 + namespace TutorPro\Tools;
12 +
13 + use Tutor\Helpers\QueryHelper;
14 + use TutorPro\Subscription\Models\SubscriptionModel;
15 +
16 + if ( ! defined( 'ABSPATH' ) ) {
17 + exit;
18 + }
19 +
20 + /**
21 + * Handle subscription exporting
22 + *
23 + * @since 3.8.0
24 + */
25 + class SubscriptionExporter {
26 +
27 + /**
28 + * List of sub-files relevant for membership plans.
29 + *
30 + * @since 3.8.1
31 + *
32 + * @var array
33 + */
34 + private static array $membership_sub_files = array(
35 + Exporter::PLANS,
36 + Exporter::ORDERS,
37 + Exporter::SUBSCRIPTIONS,
38 + );
39 +
40 + /**
41 + * Export subscription data for the given plan IDs.
42 + *
43 + * @since 3.8.1
44 + *
45 + * @param array $plan_ids Array of plan IDs.
46 + *
47 + * @return array Exported subscription data.
48 + */
49 + public static function export( $plan_ids ) {
50 + return self::get_subscription_data( $plan_ids );
51 + }
52 +
53 + /**
54 + * Retrieve subscription data for the given plan IDs.
55 + *
56 + * @since 3.8.1
57 + *
58 + * @param array $plan_ids Array of plan IDs.
59 + *
60 + * @return array Array of subscription data.
61 + */
62 + private static function get_subscription_data( array $plan_ids ): array {
63 + global $wpdb;
64 +
65 + $subscription_schema = QueryHelper::get_table_schema( $wpdb->tutor_subscriptions );
66 + $total_columns = count( $subscription_schema );
67 +
68 + $result = array();
69 +
70 + foreach ( $plan_ids as $plan_id ) {
71 +
72 + $subscriptions = SubscriptionModel::get_subscriptions_by_plan_id( $plan_id );
73 + $subscribed_users = array();
74 + $last_key = 0;
75 +
76 + foreach ( $subscriptions as $sub ) {
77 + $sub_id = $sub['id'];
78 +
79 + if ( empty( $subscribed_users ) || ! in_array( $sub_id, $subscribed_users[ $last_key ] ) ) {
80 + $subscribed_users[] = array_slice( (array) $sub, 0, $total_columns );
81 + }
82 +
83 + $last_key = array_key_last( $subscribed_users );
84 +
85 + if ( ! empty( $sub['subscription_id'] ) ) {
86 + $subscribed_users[ $last_key ]['meta'][] = array_slice( (array) $sub, $total_columns );
87 + }
88 + }
89 +
90 + if ( ! empty( $subscribed_users ) ) {
91 + $result = array_merge( $result, $subscribed_users );
92 + }
93 + }
94 +
95 + return $result;
96 + }
97 +
98 + /**
99 + * Get the list of membership sub-files.
100 + *
101 + * @since 3.8.1
102 + *
103 + * @return array
104 + */
105 + public function get_sub_files(): array {
106 +
107 + return self::$membership_sub_files;
108 + }
109 +
110 + /**
111 + * Check if a given sub-file belongs to membership plans.
112 + *
113 + * @since 3.8.1
114 + *
115 + * @param string $content_type The type of content.
116 + * @param string $sub_file The sub-file key.
117 + * @return bool True if the sub-file is for membership plans, false otherwise.
118 + */
119 + public static function is_sub_file( string $content_type, string $sub_file ) {
120 +
121 + return Exporter::TYPE_MEMBERSHIP_PLANS === $content_type && in_array( $sub_file, self::$membership_sub_files );
122 + }
123 + }
124 +