| @@ -1,59 +1,291 @@ | ||
| 1 | -<?php | |
| 1 | +<?php | |
| 2 | +if ( ! defined( 'ABSPATH' ) ) | |
| 3 | + exit; | |
| 2 | 4 | global $wpdb; |
| 5 | +add_shortcode('xyz-ips','xyz_ips_display_content'); | |
| 6 | +include_once 'admin/constants.php'; | |
| 3 | 7 | |
| 4 | -add_shortcode('xyz-ips','xyz_ips_display_content'); | |
| 5 | 8 | |
| 6 | -function xyz_ips_display_content($xyz_snippet_name){ | |
| 7 | - global $wpdb; | |
| 9 | +/* customization starts: Execute on demand and run now */ | |
| 8 | 10 | |
| 9 | - if(is_array($xyz_snippet_name)){ | |
| 10 | - $snippet_name = $xyz_snippet_name['snippet']; | |
| 11 | +$table_name = $wpdb->prefix . 'xyz_ips_short_code'; | |
| 12 | +$snippets = $wpdb->get_results($wpdb->prepare( "SELECT * FROM {$table_name} WHERE insertionMethod = %d AND status = %d",1, 1 )); | |
| 13 | +foreach ($snippets as $snippet) { | |
| 14 | + | |
| 15 | + switch ($snippet->insertionLocation) { | |
| 11 | 16 | |
| 12 | - $query = $wpdb->get_results($wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."xyz_ips_short_code WHERE title=%s" ,$snippet_name)); | |
| 13 | - | |
| 14 | - if(count($query)>0){ | |
| 17 | + | |
| 18 | + case XYZ_IPS_INSERTION_LOCATION['ADMIN_RUN_ON_HEADER']: | |
| 19 | + if (is_admin()) { | |
| 20 | + add_action('admin_head', function() use ($snippet) { | |
| 21 | + echo xyz_execute_ips_snippet($snippet); | |
| 22 | + }, 10); | |
| 23 | + } | |
| 15 | 24 | |
| 16 | - foreach ($query as $sippetdetails){ | |
| 17 | - if($sippetdetails->status==1){ | |
| 18 | - | |
| 19 | - if(is_numeric(ini_get('output_buffering'))){ | |
| 20 | - $tmp=ob_get_contents(); | |
| 21 | - ob_clean(); | |
| 22 | - ob_start(); | |
| 23 | - $content_to_eval=$sippetdetails->content; | |
| 24 | - if(substr($content_to_eval, 0,5)=='<?php') | |
| 25 | - $content_to_eval='?>'.$content_to_eval; | |
| 26 | - eval($content_to_eval); | |
| 27 | - $xyz_em_content = ob_get_contents(); | |
| 28 | - ob_clean(); | |
| 29 | - echo $tmp; | |
| 30 | - return $xyz_em_content; | |
| 31 | - }else{ | |
| 32 | - eval($sippetdetails->content); | |
| 33 | - } | |
| 34 | - | |
| 35 | - | |
| 36 | - | |
| 37 | - }else{ | |
| 38 | - return ''; | |
| 39 | - } | |
| 40 | - break; | |
| 25 | + break; | |
| 26 | + case XYZ_IPS_INSERTION_LOCATION['ADMIN_RUN_ON_FOOTER']: | |
| 27 | + if (is_admin()) { | |
| 28 | + add_action('admin_footer', function() use ($snippet) { | |
| 29 | + echo xyz_execute_ips_snippet($snippet); | |
| 30 | + }, 10); | |
| 41 | 31 | } |
| 42 | 32 | |
| 43 | - }else{ | |
| 33 | + break; | |
| 44 | 34 | |
| 45 | - return ''; | |
| 46 | -/* return "<div style='padding:20px; font-size:16px; color:#FA5A6A; width:93%;text-align:center;background:lightyellow;border:1px solid #3FAFE3; margin:20px 0 20px 0'> | |
| 47 | 35 | |
| 48 | - Please use a valid short code to call snippet. | |
| 36 | + | |
| 37 | + case XYZ_IPS_INSERTION_LOCATION['FRONTEND_RUN_ON_HEADER']: | |
| 38 | + if (!is_admin()) { | |
| 39 | + add_action('wp_head', function() use ($snippet) { | |
| 40 | + echo xyz_execute_ips_snippet($snippet); | |
| 41 | + }, 10); | |
| 42 | + } | |
| 49 | 43 | |
| 44 | + break; | |
| 45 | + | |
| 46 | + case XYZ_IPS_INSERTION_LOCATION['FRONTEND_RUN_ON_FOOTER']: | |
| 47 | + if (!is_admin()) { | |
| 50 | 48 | |
| 51 | - </div>"; | |
| 52 | -*/ | |
| 53 | - } | |
| 49 | + | |
| 50 | + add_action('wp_footer', function() use ($snippet) { | |
| 51 | + echo xyz_execute_ips_snippet($snippet); | |
| 52 | + | |
| 53 | + }, 10); | |
| 54 | + } | |
| 55 | + | |
| 56 | + break; | |
| 54 | 57 | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 55 | 63 | } |
| 56 | 64 | } |
| 57 | 65 | |
| 58 | 66 | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | +function xyz_execute_ips_snippet($sippetdetails) | |
| 71 | +{ | |
| 72 | + | |
| 73 | + | |
| 74 | + if($sippetdetails->status==1){ | |
| 75 | + /* if(is_numeric(ini_get('output_buffering'))){ | |
| 76 | + $tmp=ob_get_contents(); | |
| 77 | + if(strlen($tmp)>0) | |
| 78 | + ob_clean();*/ | |
| 79 | + ob_start(); | |
| 80 | + $content_to_eval=$sippetdetails->content; | |
| 81 | + | |
| 82 | +/***** to handle old codes : start *****/ | |
| 83 | + | |
| 84 | +if(get_option('xyz_ips_auto_insert')==1){ | |
| 85 | +$content_to_eval =xyz_ips_prepare_content_to_eval($content_to_eval); | |
| 86 | +} | |
| 87 | + | |
| 88 | +/***** to handle old codes : end *****/ | |
| 89 | +else{ | |
| 90 | +if(substr(trim($content_to_eval), 0,5)=='<?php') | |
| 91 | +$content_to_eval='?>'.$content_to_eval; | |
| 92 | +} | |
| 93 | + | |
| 94 | + | |
| 95 | +$exception_occur=0; | |
| 96 | +$exception_msg=""; | |
| 97 | + | |
| 98 | +try { | |
| 99 | + | |
| 100 | + | |
| 101 | +eval($content_to_eval); | |
| 102 | +} catch (Throwable $e) { // For PHP 7 and later | |
| 103 | +$exception_occur = 1; | |
| 104 | +$exception_msg = $e->getMessage(); | |
| 105 | +} | |
| 106 | +catch (Exception $e) { // For PHP 5 | |
| 107 | +$exception_occur = 1; | |
| 108 | +$exception_msg = $e->getMessage(); | |
| 109 | +} | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + if($exception_occur==1) { | |
| 114 | + | |
| 115 | + // global $post; | |
| 116 | + // $post_slug = $post->post_name; | |
| 117 | + | |
| 118 | + //if($post_slug!="xyz-ics-preview-page" && !is_customize_preview()) | |
| 119 | + { | |
| 120 | + | |
| 121 | + if(get_option('xyz_ips_exception_email')!="0" && get_option('xyz_ips_exception_email')!="" && get_option('xyz_ips_auto_exception')==1) | |
| 122 | + { | |
| 123 | + $email=get_option('xyz_ips_exception_email'); | |
| 124 | + $headers= "Content-type: text/html"; | |
| 125 | + $subject="Exception Report"; | |
| 126 | + $message="Hi,<br>An exception occurred while running one of the snippet.The snippet name is ".$snippet_name; | |
| 127 | + $message.=".<br>Exception details are given below : <br>"; | |
| 128 | + $message.=$exception_msg; | |
| 129 | + wp_mail($email, $subject, $message,$headers); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + $xyz_em_content = ob_get_contents(); | |
| 135 | + // ob_clean(); | |
| 136 | + ob_end_clean(); | |
| 137 | + return $xyz_em_content; | |
| 138 | + /* } | |
| 139 | + else{ | |
| 140 | + eval($sippetdetails->content); | |
| 141 | + }*/ | |
| 142 | + } | |
| 143 | + else{ | |
| 144 | + return ''; | |
| 145 | + } | |
| 146 | + | |
| 147 | + | |
| 148 | +} | |
| 149 | +/* customization ends */ | |
| 150 | +function xyz_ips_display_content($xyz_snippet_name){ | |
| 151 | + global $wpdb; | |
| 152 | + $xyz_ips_exec_in_editor = get_option('xyz_ips_exec_in_editor'); | |
| 153 | + if ( $xyz_ips_exec_in_editor ) { | |
| 154 | + // Page Builder checks (Elementor, WPBakery, Divi, Beaver Builder) | |
| 155 | + if ( wp_doing_ajax() ) { | |
| 156 | + $builder_actions = ['elementor_preview', 'wpb_pb_preview', 'et_pb_preview']; | |
| 157 | + // Check for Elementor, WPBakery, Divi actions | |
| 158 | + if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], $builder_actions, true ) ) { | |
| 159 | + // Allow shortcode execution in page builder previews | |
| 160 | + } | |
| 161 | + // Beaver Builder detection using URL parameters | |
| 162 | + if ( isset( $_REQUEST['fl_builder'] ) && isset( $_REQUEST['fl_builder'] ) ) { | |
| 163 | + // Allow execution for Beaver Builder preview | |
| 164 | + } | |
| 165 | + } | |
| 166 | + // Classic Editor or Gutenberg Editor check (editing posts) | |
| 167 | + if ( is_admin() && isset( $_GET['post'] ) && 'edit' === $_GET['action'] ) { | |
| 168 | + // Allow shortcode execution in Classic Editor or Gutenberg (when editing posts) | |
| 169 | + } | |
| 170 | + } elseif ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { | |
| 171 | + return ''; // Do not execute shortcode in other admin areas or REST API requests | |
| 172 | + } | |
| 173 | + if(is_array($xyz_snippet_name)&& isset($xyz_snippet_name['snippet'])){ | |
| 174 | + $snippet_name = $xyz_snippet_name['snippet']; | |
| 175 | + $query = $wpdb->get_results($wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."xyz_ips_short_code WHERE title=%s" ,$snippet_name)); | |
| 176 | + if(!empty($query))//if(count($query)>0) | |
| 177 | + { | |
| 178 | + foreach ($query as $sippetdetails){ | |
| 179 | + if($sippetdetails->status==1){ | |
| 180 | + /* if(is_numeric(ini_get('output_buffering'))){ | |
| 181 | + $tmp=ob_get_contents(); | |
| 182 | + if(strlen($tmp)>0) | |
| 183 | + ob_clean();*/ | |
| 184 | + ob_start(); | |
| 185 | + $content_to_eval=$sippetdetails->content; | |
| 186 | + | |
| 187 | +/***** to handle old codes : start *****/ | |
| 188 | + | |
| 189 | +if(get_option('xyz_ips_auto_insert')==1){ | |
| 190 | + $xyz_ips_content_start='<?php'; | |
| 191 | + $new_line="\r\n"; | |
| 192 | + $xyz_ips_content_end='?>'; | |
| 193 | + | |
| 194 | + if (stripos($content_to_eval, '<?php') !== false) | |
| 195 | + $tag_start_position=stripos($content_to_eval,'<?php'); | |
| 196 | + else | |
| 197 | + $tag_start_position="-1"; | |
| 198 | + | |
| 199 | + if (stripos($content_to_eval, '?>') !== false) | |
| 200 | + $tag_end_position=stripos($content_to_eval,'?>'); | |
| 201 | + else | |
| 202 | + $tag_end_position="-1"; | |
| 203 | + | |
| 204 | + if(stripos($content_to_eval, '<?php') === false && stripos($content_to_eval, '?>') === false) | |
| 205 | + { | |
| 206 | + $content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval; | |
| 207 | + } | |
| 208 | + else if(stripos($content_to_eval, '<?php') !== false) | |
| 209 | + { | |
| 210 | + if($tag_start_position>=0 && $tag_end_position>=0 && $tag_start_position>$tag_end_position) | |
| 211 | + { | |
| 212 | + $content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval; | |
| 213 | + } | |
| 214 | + } | |
| 215 | + else if(stripos($content_to_eval, '<?php') === false) | |
| 216 | + { | |
| 217 | + if (stripos($content_to_eval, '?>') !== false) | |
| 218 | + { | |
| 219 | + $content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval; | |
| 220 | + } | |
| 221 | + } | |
| 222 | + $content_to_eval='?>'.$content_to_eval; | |
| 223 | +} | |
| 224 | + | |
| 225 | +/***** to handle old codes : end *****/ | |
| 226 | +else{ | |
| 227 | + if(substr(trim($content_to_eval), 0,5)=='<?php') | |
| 228 | + $content_to_eval='?>'.$content_to_eval; | |
| 229 | +} | |
| 230 | + | |
| 231 | + | |
| 232 | +$exception_occur=0; | |
| 233 | +$exception_msg=""; | |
| 234 | + | |
| 235 | + try { | |
| 236 | + | |
| 237 | + | |
| 238 | + eval($content_to_eval); | |
| 239 | + } catch (Throwable $e) { // For PHP 7 and later | |
| 240 | + $exception_occur = 1; | |
| 241 | + $exception_msg = $e->getMessage(); | |
| 242 | + } | |
| 243 | + catch (Exception $e) { // For PHP 5 | |
| 244 | + $exception_occur = 1; | |
| 245 | + $exception_msg = $e->getMessage(); | |
| 246 | + } | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + if($exception_occur==1) { | |
| 251 | + | |
| 252 | + // global $post; | |
| 253 | + // $post_slug = $post->post_name; | |
| 254 | + | |
| 255 | + //if($post_slug!="xyz-ics-preview-page" && !is_customize_preview()) | |
| 256 | + { | |
| 257 | + | |
| 258 | + if(get_option('xyz_ips_exception_email')!="0" && get_option('xyz_ips_exception_email')!="" && get_option('xyz_ips_auto_exception')==1) | |
| 259 | + { | |
| 260 | + $email=get_option('xyz_ips_exception_email'); | |
| 261 | + $headers= "Content-type: text/html"; | |
| 262 | + $subject="Exception Report"; | |
| 263 | + $message="Hi,<br>An exception occurred while running one of the snippet.The snippet name is ".$snippet_name; | |
| 264 | + $message.=".<br>Exception details are given below : <br>"; | |
| 265 | + $message.=$exception_msg; | |
| 266 | + wp_mail($email, $subject, $message,$headers); | |
| 267 | + } | |
| 268 | + } | |
| 269 | + } | |
| 270 | + | |
| 271 | + $xyz_em_content = ob_get_contents(); | |
| 272 | + // ob_clean(); | |
| 273 | + ob_end_clean(); | |
| 274 | + return $xyz_em_content; | |
| 275 | + /* } | |
| 276 | + else{ | |
| 277 | + eval($sippetdetails->content); | |
| 278 | + }*/ | |
| 279 | + } | |
| 280 | + else{ | |
| 281 | + return ''; | |
| 282 | + } | |
| 283 | + break; | |
| 284 | + } | |
| 285 | + } | |
| 286 | + else{ | |
| 287 | + return ''; | |
| 288 | + } | |
| 289 | + } | |
| 290 | +} | |
| 59 | 291 | add_filter('widget_text', 'do_shortcode'); // to run shortcodes in text widgets |