class-activation.php
2 years ago
class-admin-interface.php
2 years ago
class-common-methods.php
2 years ago
class-content-management.php
2 years ago
class-custom-code.php
2 years ago
class-deactivation.php
2 years ago
class-disable-components.php
2 years ago
class-login-logout.php
2 years ago
class-optimizations.php
2 years ago
class-security.php
2 years ago
class-settings-fields-render.php
2 years ago
class-settings-sanitization.php
2 years ago
class-settings-sections-fields.php
2 years ago
class-utilities.php
2 years ago
class-custom-code.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class related to Custom Code features |
| 7 | * |
| 8 | * @since 3.6.0 |
| 9 | */ |
| 10 | class Custom_Code |
| 11 | { |
| 12 | /** |
| 13 | * Enqueue custom admin CSS |
| 14 | * Consider using https://github.com/Cerdic/CSSTidy in the future |
| 15 | * |
| 16 | * @since 2.3.0 |
| 17 | */ |
| 18 | public function custom_admin_css() |
| 19 | { |
| 20 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 21 | $custom_admin_css = $options['custom_admin_css']; |
| 22 | ?> |
| 23 | <style type="text/css"> |
| 24 | <?php |
| 25 | echo $custom_admin_css ; |
| 26 | ?> |
| 27 | </style> |
| 28 | <?php |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Enqueue custom frontend CSS |
| 33 | * Consider using https://github.com/Cerdic/CSSTidy in the future |
| 34 | * |
| 35 | * @since 2.3.0 |
| 36 | */ |
| 37 | public function custom_frontend_css() |
| 38 | { |
| 39 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 40 | $custom_frontend_css = $options['custom_frontend_css']; |
| 41 | ?> |
| 42 | <style type="text/css"> |
| 43 | <?php |
| 44 | echo $custom_frontend_css ; |
| 45 | ?> |
| 46 | </style> |
| 47 | <?php |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Add Custom Body Class meta box for enabled post types |
| 52 | * |
| 53 | * @since 3.9.0 |
| 54 | */ |
| 55 | public function add_custom_body_class_meta_box( $post_type, $post ) |
| 56 | { |
| 57 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 58 | $enable_custom_body_class_for = $options['enable_custom_body_class_for']; |
| 59 | foreach ( $enable_custom_body_class_for as $post_type_slug => $is_custom_body_class_enabled ) { |
| 60 | if ( get_post_type() == $post_type_slug && $is_custom_body_class_enabled ) { |
| 61 | // Skip adding meta box for post types where Gutenberg is enabled |
| 62 | // if ( |
| 63 | // function_exists( 'use_block_editor_for_post_type' ) |
| 64 | // && use_block_editor_for_post_type( $post_type_slug ) |
| 65 | // ) { |
| 66 | // continue; // go to the beginning of next iteration |
| 67 | // } |
| 68 | add_meta_box( |
| 69 | 'asenha-custom-body-class', |
| 70 | // ID of meta box |
| 71 | 'Custom <body> Class', |
| 72 | // Title of meta box |
| 73 | [ $this, 'output_custom_body_class_meta_box' ], |
| 74 | // Callback function |
| 75 | $post_type_slug, |
| 76 | // The screen on which the meta box should be output to |
| 77 | 'normal', |
| 78 | // context |
| 79 | 'high' |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Render External Permalink meta box |
| 87 | * |
| 88 | * @since 3.9.0 |
| 89 | */ |
| 90 | public function output_custom_body_class_meta_box( $post ) |
| 91 | { |
| 92 | ?> |
| 93 | <div class="custom-body-class-input"> |
| 94 | <input name="<?php |
| 95 | echo esc_attr( 'custom_body_class' ) ; |
| 96 | ?>" class="large-text" id="<?php |
| 97 | echo esc_attr( 'custom_body_class' ) ; |
| 98 | ?>" type="text" value="<?php |
| 99 | echo esc_attr( get_post_meta( $post->ID, '_custom_body_class', true ) ) ; |
| 100 | ?>" placeholder="e.g. light-theme new-year-promo" /> |
| 101 | <div class="custom-body-class-input-description">Use blank space to separate multiple classes, e.g. first-class second-class</div> |
| 102 | <?php |
| 103 | wp_nonce_field( |
| 104 | 'custom_body_class_' . $post->ID, |
| 105 | 'custom_body_class_nonce', |
| 106 | false, |
| 107 | true |
| 108 | ); |
| 109 | ?> |
| 110 | </div> |
| 111 | <?php |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Save custom body class input |
| 116 | * |
| 117 | * @since 3.9.0 |
| 118 | */ |
| 119 | public function save_custom_body_class( $post_id ) |
| 120 | { |
| 121 | // Only proceed if nonce is verified |
| 122 | |
| 123 | if ( isset( $_POST['custom_body_class_nonce'] ) && wp_verify_nonce( $_POST['custom_body_class_nonce'], 'custom_body_class_' . $post_id ) ) { |
| 124 | // Get the value of external permalink from input field |
| 125 | $custom_body_class = ( isset( $_POST['custom_body_class'] ) ? sanitize_text_field( trim( $_POST['custom_body_class'] ) ) : '' ); |
| 126 | // Update or delete external permalink post meta |
| 127 | |
| 128 | if ( !empty($custom_body_class) ) { |
| 129 | update_post_meta( $post_id, '_custom_body_class', $custom_body_class ); |
| 130 | } else { |
| 131 | delete_post_meta( $post_id, '_custom_body_class' ); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Append custom body classes to the frontend <body> tag |
| 140 | * |
| 141 | * @since 4.4.0 |
| 142 | */ |
| 143 | public function append_custom_body_class( $classes ) |
| 144 | { |
| 145 | // Only add custom body classes to the singular view of enabled post types |
| 146 | |
| 147 | if ( is_singular() ) { |
| 148 | global $post ; |
| 149 | $custom_body_classes = get_post_meta( $post->ID, '_custom_body_class', true ); |
| 150 | |
| 151 | if ( !empty($custom_body_classes) ) { |
| 152 | $custom_body_classes = explode( ' ', $custom_body_classes ); |
| 153 | foreach ( $custom_body_classes as $custom_body_class ) { |
| 154 | $classes[] = sanitize_html_class( $custom_body_class ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | return $classes; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Show content of ads.txt saved to options |
| 165 | * |
| 166 | * @since 3.2.0 |
| 167 | */ |
| 168 | public function show_ads_appads_txt_content() |
| 169 | { |
| 170 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 171 | $request = ( isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false ); |
| 172 | |
| 173 | if ( '/ads.txt' === $request ) { |
| 174 | $ads_txt_content = ( array_key_exists( 'ads_txt_content', $options ) ? $options['ads_txt_content'] : '' ); |
| 175 | header( 'Content-Type: text/plain' ); |
| 176 | echo esc_textarea( $ads_txt_content ) ; |
| 177 | die; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | if ( '/app-ads.txt' === $request ) { |
| 182 | $app_ads_txt_content = ( array_key_exists( 'app_ads_txt_content', $options ) ? $options['app_ads_txt_content'] : '' ); |
| 183 | header( 'Content-Type: text/plain' ); |
| 184 | echo esc_textarea( $app_ads_txt_content ) ; |
| 185 | die; |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Maybe show custom robots.txt content |
| 192 | * |
| 193 | * @since 3.5.0 |
| 194 | */ |
| 195 | public function maybe_show_custom_robots_txt_content( $output, $public ) |
| 196 | { |
| 197 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 198 | if ( array_key_exists( 'robots_txt_content', $options ) && !empty($options['robots_txt_content']) ) { |
| 199 | $output = wp_strip_all_tags( $options['robots_txt_content'] ); |
| 200 | } |
| 201 | return $output; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Insert code before </head> tag |
| 206 | * |
| 207 | * @since 3.3.0 |
| 208 | */ |
| 209 | public function insert_head_code() |
| 210 | { |
| 211 | $this->insert_code( 'head' ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Insert code after <body> tag |
| 216 | * |
| 217 | * @since 3.3.0 |
| 218 | */ |
| 219 | public function insert_body_code() |
| 220 | { |
| 221 | $this->insert_code( 'body' ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Insert code in footer section before </body> tag |
| 226 | * |
| 227 | * @since 3.3.0 |
| 228 | */ |
| 229 | public function insert_footer_code() |
| 230 | { |
| 231 | $this->insert_code( 'footer' ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Insert code |
| 236 | * |
| 237 | * @since 3.3.0 |
| 238 | */ |
| 239 | public function insert_code( $location ) |
| 240 | { |
| 241 | // Do not insert code in admin, feed, robots or trackbacks |
| 242 | if ( is_admin() || is_feed() || is_robots() || is_trackback() ) { |
| 243 | return; |
| 244 | } |
| 245 | // Get option that stores the code |
| 246 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 247 | if ( 'head' == $location ) { |
| 248 | $code = ( array_key_exists( 'head_code', $options ) ? $options['head_code'] : '' ); |
| 249 | } |
| 250 | if ( 'body' == $location ) { |
| 251 | $code = ( array_key_exists( 'body_code', $options ) ? $options['body_code'] : '' ); |
| 252 | } |
| 253 | if ( 'footer' == $location ) { |
| 254 | $code = ( array_key_exists( 'footer_code', $options ) ? $options['footer_code'] : '' ); |
| 255 | } |
| 256 | echo wp_unslash( $code ) . PHP_EOL ; |
| 257 | } |
| 258 | |
| 259 | } |