| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) |
| 3 |
exit; |
| 4 |
|
| 5 |
if(!function_exists('xyz_ihs_plugin_get_version')) |
| 6 |
{ |
| 7 |
function xyz_ihs_plugin_get_version() |
| 8 |
{ |
| 9 |
if ( ! function_exists( 'get_plugins' ) ) |
| 10 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 11 |
$plugin_folder = get_plugins( '/' . plugin_basename( dirname( XYZ_INSERT_HTML_PLUGIN_FILE ) ) ); |
| 12 |
return $plugin_folder['insert-html-snippet.php']['Version']; |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
if(!function_exists('xyz_ihs_run_upgrade_routines')) |
| 17 |
{ |
| 18 |
function xyz_ihs_run_upgrade_routines() { |
| 19 |
global $wpdb; |
| 20 |
if (is_multisite()) { |
| 21 |
$blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 22 |
foreach ($blog_ids as $blog_id) { |
| 23 |
switch_to_blog($blog_id); |
| 24 |
xyz_ihs_install(); |
| 25 |
restore_current_blog(); |
| 26 |
} |
| 27 |
} else { |
| 28 |
xyz_ihs_install(); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
if(!function_exists('xyz_trim_deep')) |
| 34 |
{ |
| 35 |
|
| 36 |
function xyz_trim_deep($value) { |
| 37 |
if ( is_array($value) ) { |
| 38 |
$value = array_map('xyz_trim_deep', $value); |
| 39 |
} elseif ( is_object($value) ) { |
| 40 |
|
| 41 |
foreach (get_object_vars($value) as $key => $data) { |
| 42 |
$value->{$key} = xyz_trim_deep( $data ); |
| 43 |
} |
| 44 |
|
| 45 |
} elseif (is_string($value)) { |
| 46 |
$value = trim($value); |
| 47 |
} |
| 48 |
|
| 49 |
return $value; |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
if(!function_exists('xyz_ihs_links')){ |
| 56 |
function xyz_ihs_links($links, $file) { |
| 57 |
$base = plugin_basename(XYZ_INSERT_HTML_PLUGIN_FILE); |
| 58 |
if ($file == $base) { |
| 59 |
|
| 60 |
$links[] = '<a href="https://xyzscripts.com/support/" class="xyz_ihs_support" title="Support"></a>'; |
| 61 |
$links[] = '<a href="https://twitter.com/xyzscripts" class="xyz_ihs_twitt" title="Follow us on Twitter"></a>'; |
| 62 |
$links[] = '<a href="https://www.facebook.com/xyzscripts" class="xyz_ihs_fbook" title="Like us on Facebook"></a>'; |
| 63 |
$links[] = '<a href="https://www.instagram.com/xyz_scripts/" class="xyz_ihs_insta" title="Follow us on Instagram+"></a>'; |
| 64 |
$links[] = '<a href="https://www.linkedin.com/company/xyzscripts" class="xyz_ihs_linkedin" title="Follow us on LinkedIn"></a>'; |
| 65 |
} |
| 66 |
return $links; |
| 67 |
} |
| 68 |
} |
| 69 |
add_filter( 'plugin_row_meta','xyz_ihs_links',10,2); |
| 70 |
if(!function_exists('xyz_ihs_get_insertion_location_label')){ |
| 71 |
function xyz_ihs_get_insertion_location_label($value) { |
| 72 |
$map = array_flip(XYZ_IHS_INSERTION_LOCATION); |
| 73 |
|
| 74 |
if (!isset($map[$value])) { |
| 75 |
return ''; |
| 76 |
} |
| 77 |
// Convert constant-style key to readable text |
| 78 |
return ucwords(strtolower(str_replace('_', ' ', $map[$value]))); |
| 79 |
} |
| 80 |
} |
| 81 |
if(!function_exists('xyz_ihs_get_insertion_location_type_label')){ |
| 82 |
function xyz_ihs_get_insertion_location_type_label($value) { |
| 83 |
$map = array_flip(XYZ_IHS_INSERTION_LOCATION_TYPE); |
| 84 |
|
| 85 |
if (!isset($map[$value])) { |
| 86 |
return ''; |
| 87 |
} |
| 88 |
|
| 89 |
return ucwords(strtolower(str_replace('_', ' ', $map[$value]))); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if(!function_exists('xyz_ihs_update_usage_for_post')){ |
| 94 |
// --- Tracking Logic --- |
| 95 |
function xyz_ihs_update_usage_for_post($post_id, $content, $post_type = 'post') { |
| 96 |
global $wpdb; |
| 97 |
$table_name = $wpdb->prefix . 'xyz_ihs_usage'; |
| 98 |
|
| 99 |
if (strpos($content, '[xyz-ihs') === false) { |
| 100 |
$wpdb->delete($table_name, ['post_id' => $post_id]); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$wpdb->delete($table_name, ['post_id' => $post_id]); |
| 105 |
|
| 106 |
preg_match_all('/\[xyz-ihs([^\]]*)\]/', $content, $shortcodes); |
| 107 |
|
| 108 |
$titles = []; |
| 109 |
|
| 110 |
if (!empty($shortcodes[1])) { |
| 111 |
foreach ($shortcodes[1] as $attr_string) { |
| 112 |
$atts = shortcode_parse_atts($attr_string); |
| 113 |
// PHP 8.3 Compliance Fix: Ensure $atts is an array before checking the 'snippet' key |
| 114 |
//if (!empty($atts['snippet'])) |
| 115 |
if (is_array($atts) && !empty($atts['snippet'])) |
| 116 |
{ |
| 117 |
$titles[] = $atts['snippet']; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$unique_snippets = array_unique($titles); |
| 123 |
|
| 124 |
foreach ($unique_snippets as $title) { |
| 125 |
$s_id = $wpdb->get_var($wpdb->prepare( |
| 126 |
"SELECT id FROM {$wpdb->prefix}xyz_ihs_short_code WHERE title = %s", |
| 127 |
$title |
| 128 |
)); |
| 129 |
|
| 130 |
if ($s_id) { |
| 131 |
$wpdb->insert($table_name, [ |
| 132 |
'post_id' => $post_id, |
| 133 |
'snippet_id' => $s_id, |
| 134 |
'post_type' => $post_type |
| 135 |
]); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
?> |