Plugin Directory

source: custom-codes/trunk/lib/release-codes.php

Last change on this file was 3353134, checked in by bilaltas, 4 months ago

2.3.92 deployment

File size: 12.9 KB
Line 
1<?php
2
3/**
4 *
5 * Release the codes.
6 *
7 * @since   2.0.0
8 * @package Custom_Codes
9 */
10defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
11// GET ALL THE CODES.
12$codes_posts = get_posts( array(
13    'post_type'      => 'custom-code',
14    'posts_per_page' => -1,
15    'orderby'        => 'menu_order date',
16    'order'          => 'ASC',
17    'post_status'    => 'publish',
18) );
19// Safe Mode. Do not release codes.
20if ( isset( $_GET['codes_safemode'] ) ) {
21    // phpcs:ignore
22    return;
23}
24/**
25 * Location conditions of hooks.
26 *
27 * @param int    $post_ID Post ID of code.
28 * @param string $location Location of code.
29 */
30function codes_location_conditions(  $post_ID, $location  ) {
31    $condition = false;
32    // Everywhere.
33    if ( 'everywhere' === $location ) {
34        $condition = true;
35    }
36    // Frontend.
37    if ( 'frontend' === $location ) {
38        $condition = !is_admin();
39    }
40    // Backend.
41    if ( 'backend' === $location ) {
42        $restrict_roles = get_post_meta( $post_ID, '_codes_adminroles', true );
43        $restrict_roles = ( is_array( $restrict_roles ) ? $restrict_roles : array() );
44        $user = wp_get_current_user();
45        $condition = is_admin() && (empty( $restrict_roles ) || !empty( $restrict_roles ) && array_intersect( $restrict_roles, $user->roles ));
46    }
47    // Login.
48    if ( 'login' === $location ) {
49        $condition = codes_is_login_page();
50    }
51    return $condition;
52}
53
54/**
55 * HTML, CSS, JS release.
56 */
57function codes_release() {
58    global 
59        $codes_posts,
60        $codes_langs,
61        $codes_lang_groups,
62        $wp_filesystem
63    ;
64    // Early exit if posts are not ready.
65    if ( !is_array( $codes_posts ) ) {
66        return;
67    }
68    $pro_frontend_locations = array(
69        'page',
70        'post',
71        'post_type',
72        'term',
73        'taxonomy',
74        'template'
75    );
76    foreach ( $codes_posts as $code_post ) {
77        $post_ID = $code_post->ID;
78        $language = get_post_meta( $post_ID, '_codes_language', true );
79        $language_key = array_search( $language, array_column( $codes_langs, 'id' ), true );
80        $language_data = $codes_langs[$language_key];
81        $language_extension = $language_data->id;
82        $language_editors = $language_data->editors;
83        $language_compileable = $language_data->output;
84        $language_group = $language_data->group;
85        $language_group_key = array_search( $language_group, array_column( $codes_lang_groups, 'id' ), true );
86        $language_group_data = $codes_lang_groups[$language_group_key];
87        $language_group_extension = $language_group_data->extension;
88        $save_count = get_post_meta( $post_ID, '_codes_savecount', true );
89        $location = get_post_meta( $post_ID, '_codes_location', true );
90        // LANGUAGE GROUP BASED OUTPUTS.
91        if ( 'css' === $language_group_extension ) {
92            // CSS Outputs.
93            $file_name = "{$post_ID}-{$language_extension}-output";
94            $file_full_name = "{$file_name}.{$language_group_extension}";
95            $file_directory = CODES_FOLDER_DIR . $file_full_name;
96            if ( file_exists( $file_directory ) ) {
97                // CSS actions.
98                $css_actions = array(
99                    'wp_enqueue_scripts'    => array('everywhere', 'frontend'),
100                    'admin_enqueue_scripts' => array('everywhere', 'backend'),
101                    'login_enqueue_scripts' => array('everywhere', 'login'),
102                );
103                // Hooks.
104                foreach ( $css_actions as $hook => $run_on ) {
105                    if ( !in_array( $location, $run_on, true ) ) {
106                        continue;
107                    }
108                    add_action( $hook, function () use(
109                        $file_name,
110                        $file_full_name,
111                        $save_count,
112                        $post_ID,
113                        $location
114                    ) {
115                        if ( codes_location_conditions( $post_ID, $location ) ) {
116                            wp_enqueue_style(
117                                "codes-{$file_name}",
118                                CODES_FOLDER_URL . $file_full_name,
119                                array(),
120                                $save_count
121                            );
122                        }
123                    }, 99999 );
124                }
125            }
126        } elseif ( 'js' === $language_group_extension ) {
127            // JS Outputs.
128            // Loop each editor.
129            foreach ( $language_editors as $editor ) {
130                $editor_id = $editor->id;
131                $editor_name = "{$post_ID}-{$editor_id}";
132                if ( 'individual' === $language_compileable ) {
133                    $editor_name .= '-output';
134                } elseif ( true === $language_compileable ) {
135                    $editor_name = "{$post_ID}-{$language_extension}-output";
136                }
137                $editor_file_name = "{$editor_name}.{$language_group_extension}";
138                $editor_location = str_replace( $language_extension . '-', '', $editor_id );
139                $file_directory = CODES_FOLDER_DIR . $editor_file_name;
140                $editor_url = CODES_FOLDER_URL . $editor_file_name;
141                if ( file_exists( $file_directory ) ) {
142                    // JS Actions.
143                    $js_actions = array(
144                        'wp_body_open'          => array('everywhere', 'frontend'),
145                        'wp_enqueue_scripts'    => array('everywhere', 'frontend'),
146                        'admin_enqueue_scripts' => array('everywhere', 'backend'),
147                        'login_enqueue_scripts' => array('everywhere', 'login'),
148                    );
149                    // Hooks.
150                    foreach ( $js_actions as $hook => $run_on ) {
151                        if ( !in_array( $location, $run_on, true ) ) {
152                            continue;
153                        }
154                        add_action( $hook, function () use(
155                            $editor_file_name,
156                            $editor_url,
157                            $save_count,
158                            $editor_name,
159                            $post_ID,
160                            $location,
161                            $editor_location
162                        ) {
163                            if ( codes_location_conditions( $post_ID, $location ) ) {
164                                if ( 'body-opening' === $editor_location ) {
165                                    echo '<script type="text/javascript" src="' . esc_url( "{$editor_url}?ver={$save_count}" ) . '" id="' . esc_attr( "codes-{$editor_name}" ) . '"></script>';
166                                    // phpcs:ignore
167                                } else {
168                                    wp_enqueue_script(
169                                        "codes-{$editor_name}",
170                                        $editor_url,
171                                        array('jquery'),
172                                        $save_count,
173                                        'body-closing' === $editor_location
174                                    );
175                                }
176                            }
177                        }, 99999 );
178                    }
179                }
180            }
181        } elseif ( 'html' === $language_group_extension ) {
182            // HTML Outputs.
183            // Loop each editor.
184            foreach ( $language_editors as $editor ) {
185                $editor_id = $editor->id;
186                $editor_name = "{$post_ID}-{$editor_id}";
187                if ( 'individual' === $language_compileable ) {
188                    $editor_name .= '-output';
189                } elseif ( true === $language_compileable ) {
190                    $editor_name = "{$post_ID}-{$language_extension}-output";
191                }
192                $editor_file_name = "{$editor_name}.{$language_group_extension}";
193                $editor_location = str_replace( $language_extension . '-', '', $editor_id );
194                $file_directory = CODES_FOLDER_DIR . $editor_file_name;
195                if ( file_exists( $file_directory ) ) {
196                    // HTML Actions.
197                    $html_actions = array(
198                        'wp_body_open' => array('everywhere', 'frontend'),
199                        'wp_head'      => array('everywhere', 'frontend'),
200                        'wp_footer'    => array('everywhere', 'frontend'),
201                        'admin_head'   => array('everywhere', 'backend'),
202                        'admin_footer' => array('everywhere', 'backend'),
203                        'login_head'   => array('everywhere', 'login'),
204                        'login_footer' => array('everywhere', 'login'),
205                    );
206                    // Hooks.
207                    foreach ( $html_actions as $hook => $run_on ) {
208                        if ( !in_array( $location, $run_on, true ) ) {
209                            continue;
210                        }
211                        // Editor locations.
212                        if ( 'head' === $editor_location && 'wp_head' !== $hook && 'admin_head' !== $hook && 'login_head' !== $hook || 'body-opening' === $editor_location && 'wp_body_open' !== $hook && 'admin_body_open' !== $hook && 'login_body_open' !== $hook || 'body-closing' === $editor_location && 'wp_footer' !== $hook && 'admin_footer' !== $hook && 'login_footer' !== $hook ) {
213                            continue;
214                        }
215                        add_action( $hook, function () use(
216                            $file_directory,
217                            $wp_filesystem,
218                            $post_ID,
219                            $location,
220                            $editor_location
221                        ) {
222                            if ( codes_location_conditions( $post_ID, $location ) ) {
223                                echo $wp_filesystem->get_contents( $file_directory );
224                                // phpcs:ignore
225                            }
226                        }, 99999 );
227                    }
228                }
229            }
230        }
231    }
232    // Codes Loop
233}
234
235add_action( 'init', 'codes_release', 99 );
236// In some cases like custom login screens, it does not enqueue the styles.
237// PHP RELEASE.
238foreach ( $codes_posts as $code_post ) {
239    $code_post_id = $code_post->ID;
240    $language = get_post_meta( $code_post_id, '_codes_language', true );
241    if ( 'php' !== $language || isset( $_GET['custom-codes-saving'] ) ) {
242        // phpcs:ignore
243        continue;
244    }
245    $language_key = array_search( $language, array_column( $codes_langs, 'id' ), true );
246    $language_data = $codes_langs[$language_key];
247    $language_extension = $language_data->id;
248    $language_editors = $language_data->editors;
249    $language_compileable = $language_data->output;
250    $language_group = $language_data->group;
251    $language_group_key = array_search( $language_group, array_column( $codes_lang_groups, 'id' ), true );
252    $language_group_data = $codes_lang_groups[$language_group_key];
253    $language_group_extension = $language_group_data->extension;
254    $save_count = get_post_meta( $code_post_id, '_codes_savecount', true );
255    $location = get_post_meta( $code_post_id, '_codes_location', true );
256    // Loop each editor.
257    foreach ( $language_editors as $editor ) {
258        $editor_id = $editor->id;
259        $editor_name = "{$code_post_id}-{$editor_id}";
260        $editor_file_name = "{$editor_name}.{$language_group_extension}";
261        $editor_location = str_replace( $language_extension . '-', '', $editor_id );
262        $file_directory = CODES_FOLDER_DIR . $editor_file_name;
263        if ( file_exists( $file_directory ) && 'default' === $editor_location ) {
264            if ( 'frontend' === $location && !is_admin() || 'backend' === $location && is_admin() && empty( $restrict_roles ) || 'login' === $location && codes_is_login_page() || 'everywhere' === $location ) {
265                try {
266                    require $file_directory;
267                } catch ( Exception $e ) {
268                    if ( CODES_DEBUG ) {
269                        wp_die( 'CodeKit - Caught exception on code #' . esc_html( $editor_name ) . ': ' . esc_html( $e->getMessage() ) . "\n" );
270                    }
271                }
272            } else {
273                // Or, run it in an action.
274                add_action( 'wp', function () use(
275                    $file_directory,
276                    $editor_name,
277                    $code_post_id,
278                    $location
279                ) {
280                    if ( codes_location_conditions( $code_post_id, $location ) ) {
281                        try {
282                            require $file_directory;
283                        } catch ( Exception $e ) {
284                            if ( CODES_DEBUG ) {
285                                wp_die( 'CodeKit - Caught exception on code #' . esc_html( $editor_name ) . ': ' . esc_html( $e->getMessage() ) . "\n" );
286                            }
287                        }
288                    }
289                } );
290            }
291        }
292    }
293}
Note: See TracBrowser for help on using the repository browser.