PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.7
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.7
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.6.7, at includes/class-metasync-common.php

247 lines 6.7 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 $path = isset($parse_url['path']) ? $parse_url['path'] : '';
65 return end(array_diff(explode('/', $path), array('')));
66 }
67
68 /**
69 * Get file name from a URL.
70 * @param $url Valid URL as string.
71 */
72 public function get_file_name_by_url($url)
73 {
74 if (stripos($url, "https://cdn.midjourney.com/") !== false) {
75 return pathinfo(str_replace("/", "_", parse_url($url, PHP_URL_PATH) ?? ''), PATHINFO_FILENAME);
76 } elseif (stripos($url, "https://drive.google.com/") !== false) {
77 $parse_url = wp_parse_url($url);
78 $args = [];
79 wp_parse_str($parse_url['query'], $args);
80 return $args['id'];
81 } else {
82 return pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);
83 }
84 }
85
86 public function allowedDownloadSources($url)
87 {
88 return false;
89 $allowed_sources = array(
90 "https://storage.googleapis.com",
91 "https://drive.google.com"
92 );
93 foreach ($allowed_sources as $source) {
94 if (stripos($url, $source) !== false)
95 return true;
96 }
97 return false;
98 }
99
100 public function get_media_id_from_url($url) {
101 global $wpdb;
102
103 // Search for any attachment matching the URL
104 $query = $wpdb->prepare("
105 SELECT ID
106 FROM $wpdb->posts
107 WHERE post_type = 'attachment'
108 AND guid = %s
109 LIMIT 1
110 ", $url);
111
112 $attachment_id = $wpdb->get_var($query);
113
114 // Return the attachment ID if found, otherwise return false
115 return $attachment_id ? intval($attachment_id) : false;
116 }
117 /**
118 * Upload image to media library if URL is valid.
119 * @param $url Valid URL.
120 * add title to the image default value value is empty string
121 */
122 public function upload_image_by_url($url,$alt='',$title_text='')
123 {
124 require_once(ABSPATH . "/wp-load.php");
125 require_once(ABSPATH . "/wp-admin/includes/image.php");
126 require_once(ABSPATH . "/wp-admin/includes/file.php");
127 require_once(ABSPATH . "/wp-admin/includes/media.php");
128
129 $tmp = download_url($url);
130 if (is_wp_error($tmp)){
131 $attachment_id = $this->get_media_id_from_url($url);
132 error_log($attachment_id);
133 return $attachment_id;
134 }
135
136 $filename = $this->get_file_name_by_url($url);
137 // $filename = pathinfo($url, PATHINFO_FILENAME);
138 // eliminating query params from file name
139 $filename = explode("?", $filename)[0];
140 $extension = pathinfo($url, PATHINFO_EXTENSION);
141
142 if (!$extension || strlen($extension) > 4) {
143 $mime = mime_content_type($tmp);
144 $mime = is_string($mime) ? sanitize_mime_type($mime) : false;
145
146 $mime_extensions = array(
147 'image/jpe' => 'jpe',
148 'image/jpg' => 'jpg',
149 'image/jpeg' => 'jpeg',
150 'image/gif' => 'gif',
151 'image/png' => 'png',
152 'image/webp' => 'webp'
153 );
154
155 if (isset($mime_extensions[$mime])) {
156 $extension = $mime_extensions[$mime];
157 } else {
158 // Safely delete temporary file if it exists
159 if (file_exists($tmp)) {
160 unlink($tmp);
161 }
162 return false;
163 }
164 }
165
166 $args = array(
167 'name' => "$filename.$extension",
168 /*
169 Generate a sanitized post_name by replacing double dashes with a single dash
170 and appending the file extension with a hyphen
171 */
172 'post_name' => str_replace('--', '-', $filename ?? '') ."-".$extension,
173 'tmp_name' => $tmp,
174 );
175
176 $get_attachment = $this->get_attachment_by_name($args['post_name']);
177
178
179 # remove if null logic check on 11 march 2025 for issue 264 and merge request 320
180 if (empty($get_attachment)) {
181 $attachment_id = media_handle_sideload($args, 0, $args['name']);
182 update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt);
183 // check if the title is empty or not if it's has title update the title
184 if($title_text !== ''){
185 wp_update_post(
186 array (
187 'ID' => $attachment_id,
188 'post_title' => $title_text
189 )
190 );
191 }
192 $attach_data = wp_generate_attachment_metadata(
193 $attachment_id,
194 wp_get_original_image_path($attachment_id)
195 );
196 wp_update_attachment_metadata($attachment_id, $attach_data);
197
198 // Safely delete temporary file if it exists
199 if (file_exists($tmp)) {
200 unlink($tmp);
201 }
202
203 if (is_wp_error($attachment_id)) return false;
204 return $attachment_id;
205 } else {
206 // check if the title attribute is set on the image tag and then update the title
207 if($title_text !== ''){
208 wp_update_post(
209 array (
210 'ID' => $get_attachment->ID,
211 'post_title' => $title_text
212 )
213 );
214 }
215 // update the alt attribute in the image
216 update_post_meta($get_attachment->ID, '_wp_attachment_image_alt', $alt);
217 return $get_attachment->ID;
218 }
219 }
220 /**
221 * Check if Gutenberg is enabled as the default page editor
222 */
223 protected function is_gutenberg_enabled() {
224 $current_screen = get_current_screen();
225 return method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor();
226 }
227 /**
228 * Check if Elementor is active
229 */
230 protected function is_elementor_active() {
231 return class_exists('Elementor\Plugin') && \Elementor\Plugin::$instance->preview->is_preview_mode();
232 }
233 /**
234 * Check if Gutenberg is enabled or Elementor is active
235 */
236 public static function check_default_page_editor() {
237 $instance = new self();
238 if ($instance->is_gutenberg_enabled()) {
239 return 'gutenberg';
240 } elseif ($instance->is_elementor_active()) {
241 return 'elementor';
242 } else {
243 return 'neither';
244 }
245 }
246 }
247