|
7 | 7 | * Description: Freemius Toolkit |
8 | 8 | * Requires at least: 6.6 |
9 | 9 | * Requires PHP: 7.4 |
10 | | - * Version: 0.2.2 |
| 10 | + * Version: 0.3.0 |
11 | 11 | * Author: Freemius |
12 | 12 | * Author URI: https://freemius.com |
13 | 13 | * License: MIT |
|
17 | 17 | exit; // Exit if accessed directly. |
18 | 18 | } |
19 | 19 |
|
20 | | -require_once __DIR__ . '/vendor/autoload.php'; |
21 | | - |
22 | | -\add_action( 'enqueue_block_assets', __NAMESPACE__ . '\block_script_styles', 1 ); |
23 | | -function block_script_styles() { |
24 | | - |
25 | | - if ( ! is_admin() ) { |
26 | | - return; |
27 | | - } |
28 | | - |
29 | | - $plugin_dir = \plugin_dir_path( __FILE__ ); |
30 | | - $plugin_url = \plugin_dir_url( __FILE__ ); |
31 | | - |
32 | | - // load from assets.php |
33 | | - $freemius_dependencies = include $plugin_dir . 'build/freemius-button/editor.asset.php'; |
34 | | - |
35 | | - \wp_enqueue_code_editor( array( 'type' => 'application/javascript' ) ); |
36 | | - |
37 | | - // Freemius Button Block |
38 | | - \wp_enqueue_script( 'freemius-button', $plugin_url . 'build/freemius-button/editor.js', $freemius_dependencies['dependencies'], $freemius_dependencies['version'], true ); |
39 | | - \wp_enqueue_style( 'freemius-button', $plugin_url . 'build/freemius-button/editor.css', array(), $freemius_dependencies['version'] ); |
40 | | - |
41 | | - // TODO: load this via API in the editor.js |
42 | | - \wp_add_inline_script( 'freemius-button', 'const freemius_button_schema = ' . wp_json_encode( get_schema() ) . '', true ); |
43 | | -} |
44 | | - |
45 | | - |
46 | | - |
47 | | -\add_filter( 'render_block_core/button', __NAMESPACE__ . '\render_button', 10, 3 ); |
48 | | -function render_button( $block_content, $block, $instance ) { |
49 | | - |
50 | | - if ( ! isset( $block['attrs'] ) ) { |
51 | | - return $block_content; |
52 | | - } |
53 | | - if ( ! isset( $block['attrs']['freemius_enabled'] ) || $block['attrs']['freemius_enabled'] === false ) { |
54 | | - return $block_content; |
55 | | - } |
56 | | - |
57 | | - // merge the data from the site, the page and the block |
58 | | - $site_data = \get_option( 'freemius_button', array() ); |
59 | | - $page_data = \get_post_meta( get_the_ID(), 'freemius_button', true ); |
60 | | - $plugin_data = isset( $block['attrs']['freemius'] ) ? $block['attrs']['freemius'] : array(); |
61 | | - |
62 | | - $data = array_merge( (array) $site_data, (array) $page_data, (array) $plugin_data ); |
63 | | - |
64 | | - /** |
65 | | - * Filter the data that will be passed to the Freemius checkout. |
66 | | - * |
67 | | - * @param array $data The data that will be passed to the Freemius checkout. |
68 | | - */ |
69 | | - $data = \apply_filters( 'freemius_button_data', $data ); |
70 | | - |
71 | | - $extra = ''; |
72 | | - $extra .= '<script type="application/json" class="freemius-button-data">' . wp_json_encode( $data ) . '</script>'; |
73 | | - |
74 | | - $plugin_dir = \plugin_dir_path( __FILE__ ); |
75 | | - $plugin_url = \plugin_dir_url( __FILE__ ); |
76 | | - |
77 | | - \wp_enqueue_script( 'freemius-button-checkout', 'https://checkout.freemius.com/js/v1/', array(), 'v1', true ); |
78 | | - |
79 | | - // load from assets.php |
80 | | - $dependecied = include $plugin_dir . 'build/freemius-button/view.asset.php'; |
81 | | - \wp_enqueue_script( 'freemius-button-frontend', $plugin_url . 'build/freemius-button/view.js', $dependecied['dependencies'], $dependecied['version'], true ); |
82 | | - \wp_enqueue_style( 'freemius-button-frontend', $plugin_url . 'build/freemius-button/view.css', array(), $dependecied['version'] ); |
| 20 | +define( 'FREEMIUS_PLUGIN_DIR', __DIR__ ); |
| 21 | +define( 'FREEMIUS_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); |
83 | 22 |
|
84 | | - return $extra . $block_content; |
85 | | -} |
86 | | - |
87 | | - |
88 | | -// register custom post meta to store the button data |
89 | | -\add_action( 'init', __NAMESPACE__ . '\register_post_meta' ); |
90 | | -function register_post_meta() { |
91 | | - |
92 | | - \register_post_meta( |
93 | | - '', // registered for all post types |
94 | | - 'freemius_button', |
95 | | - array( |
96 | | - 'single' => true, |
97 | | - 'type' => 'object', |
98 | | - 'sanitize_callback' => __NAMESPACE__ . '\sanitize_schema', |
99 | | - 'default' => array(), |
100 | | - 'show_in_rest' => array( |
101 | | - 'schema' => array( |
102 | | - 'type' => 'object', |
103 | | - 'properties' => get_schema(), |
104 | | - 'additionalProperties' => false, |
105 | | - |
106 | | - ), |
107 | | - |
108 | | - ), |
109 | | - ) |
110 | | - ); |
111 | | -} |
112 | | - |
113 | | -// register a setting to store the button data |
114 | | -\add_action( 'init', __NAMESPACE__ . '\register_my_setting' ); |
115 | | -\add_action( 'rest_api_init', __NAMESPACE__ . '\register_my_setting' ); |
116 | | - |
117 | | - |
118 | | -function register_my_setting() { |
| 23 | +require_once __DIR__ . '/vendor/autoload.php'; |
119 | 24 |
|
120 | | - \register_setting( |
121 | | - 'options', |
122 | | - 'freemius_button', |
123 | | - array( |
124 | | - 'single' => true, |
125 | | - 'label' => 'Freemius Button', |
126 | | - 'type' => 'object', |
127 | | - 'sanitize_callback' => __NAMESPACE__ . '\sanitize_schema', |
128 | | - 'show_in_rest' => array( |
129 | | - 'schema' => array( |
130 | | - 'type' => 'object', |
131 | | - 'properties' => get_schema(), |
132 | | - 'additionalProperties' => false, |
| 25 | +// Autoload classes |
| 26 | +spl_autoload_register( |
| 27 | + function ( $class ) { |
| 28 | + // Project-specific namespace prefix |
| 29 | + $prefix = 'Freemius\\'; |
133 | 30 |
|
134 | | - ), |
| 31 | + // Base directory for the namespace prefix |
| 32 | + $base_dir = FREEMIUS_PLUGIN_DIR . '/includes/'; |
135 | 33 |
|
136 | | - ), |
137 | | - ) |
138 | | - ); |
139 | | -} |
| 34 | + // Check if the class uses the namespace prefix |
| 35 | + $len = strlen( $prefix ); |
| 36 | + if ( strncmp( $prefix, $class, $len ) !== 0 ) { |
| 37 | + return; |
| 38 | + } |
140 | 39 |
|
| 40 | + // Get the relative class name |
| 41 | + $relative_class = substr( $class, $len ); |
141 | 42 |
|
142 | | -function sanitize_schema( $settings ) { |
| 43 | + // Replace namespace separators with directory separators |
| 44 | + $file = $base_dir . 'class-freemius-' . strtolower( str_replace( '_', '-', $relative_class ) ) . '.php'; |
143 | 45 |
|
144 | | - foreach ( $settings as $key => $value ) { |
145 | | - if ( $settings[ $key ] === '' ) { |
146 | | - unset( $settings[ $key ] ); |
| 46 | + // If the file exists, require it |
| 47 | + if ( file_exists( $file ) ) { |
| 48 | + require $file; |
147 | 49 | } |
148 | 50 | } |
149 | | - |
150 | | - return $settings; |
| 51 | +); |
| 52 | + |
| 53 | +function init() { |
| 54 | + Button::get_instance(); |
| 55 | + Scope::get_instance(); |
| 56 | + Settings::get_instance(); |
| 57 | + Api::get_instance(); |
| 58 | + Blocks::get_instance(); |
151 | 59 | } |
| 60 | +\add_action( 'plugins_loaded', __NAMESPACE__ . '\\init' ); |
152 | 61 |
|
153 | | -function get_schema() { |
154 | | - |
155 | | - $plugin_dir = \plugin_dir_path( __FILE__ ); |
156 | | - $schema = include $plugin_dir . 'includes/schema.php'; |
157 | 62 |
|
158 | | - return $schema; |
| 63 | +/** |
| 64 | + * Plugin activation hook |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | +function activate() { |
159 | 69 | } |
| 70 | +register_activation_hook( __FILE__, __NAMESPACE__ . '\\activate' ); |
160 | 71 |
|
161 | | -\add_filter( 'render_block_core/group', __NAMESPACE__ . '\render_group', 10, 3 ); |
162 | | -function render_group( $block_content, $block, $instance ) { |
163 | | - if ( ! isset( $block['attrs']['testEnabled'] ) || ! $block['attrs']['testEnabled'] ) { |
164 | | - return $block_content; |
165 | | - } |
166 | | - |
167 | | - $test_text = isset( $block['attrs']['testText'] ) ? $block['attrs']['testText'] : ''; |
168 | | - |
169 | | - // Add data attributes for styling |
170 | | - $block_content = str_replace( |
171 | | - 'class="wp-block-group', |
172 | | - sprintf( |
173 | | - 'class="wp-block-group" data-test-enabled="true" data-test-text="%s"', |
174 | | - esc_attr( $test_text ) |
175 | | - ), |
176 | | - $block_content |
177 | | - ); |
178 | | - |
179 | | - return $block_content; |
| 72 | +/** |
| 73 | + * Plugin deactivation hook |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | +function deactivate() { |
180 | 78 | } |
| 79 | +register_deactivation_hook( __FILE__, __NAMESPACE__ . '\\deactivate' ); |
0 commit comments