PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.5.23
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.5.23
2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 2.6.4 2.6.3 2.5.23 All 138 releases
metasync / includes / class-metasync-common.php

class-metasync-common.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.5.23, at includes/class-metasync-common.php

245 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The database operations for the 404 error monitor.
5 *
6 * @since 1.0.0
7 * @package Metasync
8 * @subpackage Metasync/404-monitor
9 * @author Engineering Team <support@searchatlas.com>
10 */
11 class Metasync_Common
12 {
13 /**
14 * Sanitize a array with urls and text field.
15 * @param $data Pass a Array.
16 */
17 public function sanitize_array($data)
18 {
19 // Ensure $data is always an array from the start
20 if (!is_array($data)) {
21 $data = (array) $data;
22 }
23
24 foreach ($data as $key => $value) {
25 if (is_array($value) && !empty($value)) {
26 $data[$key] = $this->sanitize_array($value);
27 } else {
28 if ($value) {
29 // Ensure $value is a string before processing
30 if (!is_string($value) && !is_numeric($value)) {
31 $value = (string) $value;
32 }
33
34 if (filter_var($value, FILTER_VALIDATE_URL)) {
35 $data[$key] = sanitize_url($value);
36 } else {
37 $data[$key] = sanitize_text_field($value);
38 }
39 }
40 }
41 }
42 return $data;
43 }
44
45 /**
46 * Get post by post name.
47 * @param $attachment_name Attachment name will be string.
48 */
49 public function get_attachment_by_name($attachment_name)
50 {
51 global $wpdb;
52 $post = $wpdb->get_row(
53 $wpdb->prepare(
54 "SELECT * FROM $wpdb->posts WHERE `post_name` = '%s' and `post_type` = 'attachment' LIMIT 1",
55 $attachment_name
56 )
57 );
58 return get_post($post);
59 }
60
61 public function get_permalink_from_url($url)
62 {
63 $parse_url = wp_parse_url($url);
64 return end(array_diff(explode('/', $parse_url['path']), array('')));
65 }
66
67 /**
68 * Get file name from a URL.
69 * @param $url Valid URL as string.
70 */
71 public function get_file_name_by_url($url)
72 {
73 if (stripos($url, "https://cdn.midjourney.com/") !== false) {
74 return pathinfo(str_replace("/", "_", parse_url($url, PHP_URL_PATH) ?? ''), PATHINFO_FILENAME);
75 } elseif (stripos($url, "https://drive.google.com/") !== false) {
76 $parse_url = wp_parse_url($url);
77 $args = [];
78 wp_parse_str($parse_url['query'], $args);
79 return $args['id'];
80 } else {
81 return pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);
82 }
83 }
84
85 public function allowedDownloadSources($url)
86 {
87 return false;
88 $allowed_sources = array(
89 "https://storage.googleapis.com",
90 "https://drive.google.com"
91 );
92 foreach ($allowed_sources as $source) {
93 if (stripos($url, $source) !== false)
94 return true;
95 }
96 return false;
97 }
98
99 public function get_media_id_from_url($url) {
100 global $wpdb;
101
102 // Search for any attachment matching the URL
103 $query = $wpdb->prepare("
104 SELECT ID
105 FROM $wpdb->posts
106 WHERE post_type = 'attachment'
107 AND guid = %s
108 LIMIT 1
109 ", $url);
110
111 $attachment_id = $wpdb->get_var($query);
112
113 // Return the attachment ID if found, otherwise return false
114 return $attachment_id ? intval($attachment_id) : false;
115 }
116 /**
117 * Upload image to media library if URL is valid.
118 * @param $url Valid URL.
119 * add title to the image default value value is empty string
120 */
121 public function upload_image_by_url($url,$alt='',$title_text='')
122 {
123 require_once(ABSPATH . "/wp-load.php");
124 require_once(ABSPATH . "/wp-admin/includes/image.php");
125 require_once(ABSPATH . "/wp-admin/includes/file.php");
126 require_once(ABSPATH . "/wp-admin/includes/media.php");
127
128 $tmp = download_url($url);
129 if (is_wp_error($tmp)){
130 $attachment_id = $this->get_media_id_from_url($url);
131 error_log($attachment_id);
132 return $attachment_id;
133 }
134
135 $filename = $this->get_file_name_by_url($url);
136 // $filename = pathinfo($url, PATHINFO_FILENAME);
137 // eliminating query params from file name
138 $filename = explode("?", $filename)[0];
139 $extension = pathinfo($url, PATHINFO_EXTENSION);
140
141 if (!$extension || strlen($extension) > 4) {
142 $mime = mime_content_type($tmp);
143 $mime = is_string($mime) ? sanitize_mime_type($mime) : false;
144
145 $mime_extensions = array(
146 'image/jpe' => 'jpe',
147 'image/jpg' => 'jpg',
148 'image/jpeg' => 'jpeg',
149 'image/gif' => 'gif',
150 'image/png' => 'png',
151 'image/webp' => 'webp'
152 );
153
154 if (isset($mime_extensions[$mime])) {
155 $extension = $mime_extensions[$mime];
156 } else {
157 // Safely delete temporary file if it exists
158 if (file_exists($tmp)) {
159 unlink($tmp);
160 }
161 return false;
162 }
163 }
164
165 $args = array(
166 'name' => "$filename.$extension",
167 /*
168 Generate a sanitized post_name by replacing double dashes with a single dash
169 and appending the file extension with a hyphen
170 */
171 'post_name' => str_replace('--', '-', $filename ?? '') ."-".$extension,
172 'tmp_name' => $tmp,
173 );
174
175 $get_attachment = $this->get_attachment_by_name($args['post_name']);
176
177
178 # remove if null logic check on 11 march 2025 for issue 264 and merge request 320
179 if (empty($get_attachment)) {
180 $attachment_id = media_handle_sideload($args, 0, $args['name']);
181 update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt);
182 // check if the title is empty or not if it's has title update the title
183 if($title_text !== ''){
184 wp_update_post(
185 array (
186 'ID' => $attachment_id,
187 'post_title' => $title_text
188 )
189 );
190 }
191 $attach_data = wp_generate_attachment_metadata(
192 $attachment_id,
193 wp_get_original_image_path($attachment_id)
194 );
195 wp_update_attachment_metadata($attachment_id, $attach_data);
196
197 // Safely delete temporary file if it exists
198 if (file_exists($tmp)) {
199 unlink($tmp);
200 }
201
202 if (is_wp_error($attachment_id)) return false;
203 return $attachment_id;
204 } else {
205 // check if the title attribute is set on the image tag and then update the title
206 if($title_text !== ''){
207 wp_update_post(
208 array (
209 'ID' => $get_attachment->ID,
210 'post_title' => $title_text
211 )
212 );
213 }
214 // update the alt attribute in the image
215 update_post_meta($get_attachment->ID, '_wp_attachment_image_alt', $alt);
216 return $get_attachment->ID;
217 }
218 }
219 /**
220 * Check if Gutenberg is enabled as the default page editor
221 */
222 protected function is_gutenberg_enabled() {
223 $current_screen = get_current_screen();
224 return method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor();
225 }
226 /**
227 * Check if Elementor is active
228 */
229 protected function is_elementor_active() {
230 return class_exists('Elementor\Plugin') && \Elementor\Plugin::$instance->preview->is_preview_mode();
231 }
232 /**
233 * Check if Gutenberg is enabled or Elementor is active
234 */
235 public static function check_default_page_editor() {
236 if (is_gutenberg_enabled()) {
237 return 'gutenberg';
238 } elseif (is_elementor_active()) {
239 return 'elementor';
240 } else {
241 return 'neither';
242 }
243 }
244 }
245