-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.php
186 lines (132 loc) · 6.17 KB
/
1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/* Template Name: posts receive Template */
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: https://domain.net");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: Content-Type");
// // Check if the current URL matches the desired page URL
// if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'https://www.domain.net/post-list-frpm-folder/') !== false) {
// // Check if this is a POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_once('/www/wwwroot/test.domain.net/wp-load.php');
require_once('/www/wwwroot/test.domain.net/wp-blog-header.php');
/*
https://domain.net/wp-content/aldoros/
التربية الاخلاقية للصف الرابع المنهاج الاماراتي/حل درس ا
لسعادة مفتاح الحياة للصف الرابع المنهاج الاماراتي.webp
*/
function set_featured_image_from_external_url($url, $post_id){
// Add Featured Image to Post
$image_url = $url; // Define the image URL here
$image_name = basename($image_url);
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
}
if (function_exists('wp_insert_post') && function_exists('term_exists')) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the values from the form POST
$fileLink = $_POST['file_link'];
$post_title = isset($_POST['post_title']) ? sanitize_text_field($_POST['post_title']) : '';
$post_content = isset($_POST['post_content']) ? wp_kses_post($_POST['post_content']) : '';
$post_content = wp_kses_post($_POST['post_content']) . '';
$post_category = isset($_POST['post_category']) ? sanitize_text_field($_POST['post_category']) : '';
$folderName = isset($_POST['post_folderName']) ? sanitize_text_field($_POST['post_folderName']) : '';
// echo $fileLink;
// echo file_get_contents($fileLink); // Get image data
if (!empty($post_title) && !empty($post_content)) {
// Custom database query to check if post title exists
global $wpdb;
$post_exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s", $post_title));
$category = get_term_by('name', $post_category, 'category');
$category_id = $category->term_id;
if (!$post_exists) {
// Continue with post creation
$post_data = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'pending', // Set post status to "draft"
'post_category' => array($category_id), // Assign the category
// 'tags_input' => array($post_category), // Assign the tag
);
// Insert the post
$post_id = wp_insert_post($post_data);
$attach_id = set_featured_image_from_external_url( $fileLink, $post_id );
update_post_meta($post_id, 'rank_math_focus_keyword', strtolower(get_the_title($post_id)));
if (!is_wp_error($post_id)) {
// Post created successfully
$response = array(
'status' => 'success',
'message2' => 'Image uploaded successfully. Attachment ID: ' . $attach_id,
'message' => 'Draft post created successfully. Post ID: ' . $post_id,
);
} else {
// Error creating post
$response = array(
'status' => 'error',
'message' => 'Error creating draft post: ' . $post_id->get_error_message(),
);
}
} else {
// Post title already exists
$response = array(
'status' => 'already exists',
'message' => 'A post with the same title already exists.',
);
}
} else {
// Handle missing or empty fields
$response = array(
'status' => 'error',
'message' => 'Missing or empty fields in the POST request.',
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
} else {
// WordPress functions are not available
$response = array(
'status' => 'error',
'message' => 'WordPress functions are not available.',
);
// Return the response as JSON
header('Content-Type: application/json');
echo json_encode($response);
}
}
// } else {
// // If the page URL doesn't match, return a 403 Forbidden status
// header('HTTP/1.0 403 Forbidden');
// exit('Access is forbidden for this page.');
// }