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

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Bundle Exporter
4 + *
5 + * @package TutorPro\Tools
6 + * @author Themeum<support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.6.0
9 + */
10 +
11 + namespace TutorPro\Tools;
12 +
13 + if ( ! defined( 'ABSPATH' ) ) {
14 + exit;
15 + }
16 +
17 + /**
18 + * Handle a bundle exporting
19 + *
20 + * @since 3.6.0
21 + */
22 + class BundleExporter {
23 +
24 + /**
25 + * List of all sub-file types related to a course-bundle export.
26 + *
27 + * @since 3.8.1
28 + *
29 + * @var string[]
30 + */
31 + private static array $bundle_sub_files = array(
32 + Exporter::BUNDLE,
33 + Exporter::ORDERS,
34 + Exporter::COURSES,
35 + );
36 +
37 + /**
38 + * Export a bundle as array
39 + *
40 + * @since 3.6.0
41 + *
42 + * @throws \Exception Throws exception if course id is invalid.
43 + *
44 + * @param integer $bundle_id Bundle id.
45 + *
46 + * @return array Export the entire bundle as an array
47 + */
48 + public static function export( int $bundle_id ) {
49 +
50 + // Bundle Post.
51 + $bundle_data = get_post( $bundle_id );
52 + if ( ! is_a( $bundle_data, 'WP_Post' ) ) {
53 + throw new \Exception( esc_html__( 'Invalid bundle id detected', 'tutor-pro' ) );
54 + }
55 +
56 + if ( tutor()->bundle_post_type !== $bundle_data->post_type ) {
57 + throw new \Exception( esc_html__( 'Invalid bundle', 'tutor-pro' ) );
58 + }
59 +
60 + // Thumbnail.
61 + $bundle_data->thumbnail_url = get_the_post_thumbnail_url( $bundle_id, 'full' );
62 +
63 + // Post Meta.
64 + $bundle_data->meta = get_post_meta( $bundle_id );
65 +
66 + return $bundle_data;
67 + }
68 +
69 + /**
70 + * Get the list of bundle sub-files to export.
71 + *
72 + * @since 3.8.1
73 + *
74 + * @param bool $keep_user_data Whether to include all sub-files.
75 + * @return string[] List of sub-file identifiers.
76 + */
77 + public static function get_sub_files( $keep_user_data ) {
78 +
79 + if ( ! $keep_user_data ) {
80 + return array( Exporter::BUNDLE, Exporter::COURSES );
81 + }
82 +
83 + if ( tutor_utils()->is_addon_enabled( 'subscription' ) ) {
84 +
85 + // Need to add plans and subscription before order.
86 +
87 + self::$bundle_sub_files = Helper::insert_items_before_target( Exporter::ORDERS, self::$bundle_sub_files, array( Exporter::PLANS, Exporter::SUBSCRIPTIONS ) );
88 + }
89 +
90 + if ( tutor_utils()->is_addon_enabled( TUTOR_ENROLLMENTS()->basename ) ) {
91 +
92 + // Need to add enrollments before Courses.
93 + self::$bundle_sub_files = Helper::insert_items_before_target( Exporter::COURSES, self::$bundle_sub_files, array( Exporter::ENROLLMENTS ) );
94 + }
95 +
96 + return self::$bundle_sub_files;
97 + }
98 +
99 + /**
100 + * Check if a given file is considered a bundle sub-file.
101 + *
102 + * @since 3.8.1
103 + *
104 + * @param string $content_type The type of content (e.g., course, bundle).
105 + * @param string $sub_file The sub-file identifier to check.
106 + * @return bool True if the file is a course sub-file, false otherwise.
107 + */
108 + public static function is_sub_file( string $content_type, string $sub_file ) {
109 +
110 + return tutor()->bundle_post_type === $content_type && in_array( $sub_file, self::$bundle_sub_files );
111 + }
112 +
113 + /**
114 + * Get the list of exportable content type keys, excluding attachments.
115 + *
116 + * @since 3.8.1
117 + *
118 + * @return string[] Array of content type keys that can be exported.
119 + */
120 + public static function get_content_types() {
121 +
122 + $content_types = array();
123 + $exporter = tutor_pro_tools()->exporter;
124 +
125 + foreach ( $exporter->get_exportable_sub_contents() as $sub_content ) {
126 + $content_types[ $sub_content['key'] ] = $sub_content['label'];
127 + }
128 + unset( $content_types[ $exporter::TYPE_ATTACHMENTS ] );
129 +
130 + return array_keys( $content_types );
131 + }
132 +
133 + /**
134 + * Check if the given content key represents a bundle.
135 + *
136 + * @since 3.8.1
137 + *
138 + * @param string $key Content key.
139 + * @return bool True if the key matches BundleExporter::BUNDLE, false otherwise.
140 + */
141 + public static function is_bundle( string $key ): bool {
142 + return Exporter::BUNDLE === $key;
143 + }
144 + }
145 +