Skip to content

Commit 3e25cdd

Browse files
authored
Merge pull request #6 from FeXd/autofill
Auto Fill closes #5
2 parents 2e1ae73 + 2e97a07 commit 3e25cdd

File tree

2 files changed

+222
-54
lines changed

2 files changed

+222
-54
lines changed

quick-weblog.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
function fetchArticleData(url, api_key) {
2+
const base_url = 'https://article-extractor2.p.rapidapi.com/article/parse?url=';
3+
const options = {
4+
method: 'GET',
5+
headers: {
6+
'X-RapidAPI-Key': api_key,
7+
'X-RapidAPI-Host': 'article-extractor2.p.rapidapi.com'
8+
}
9+
};
10+
11+
return fetch(base_url + url, options)
12+
.then(response => {
13+
if (response.status !== 200) {
14+
throw new Error(`Request failed with status: ${response.status}`);
15+
}
16+
return response.json();
17+
})
18+
.then(response => {
19+
if (response.error > 0) {
20+
throw new Error(`Request received error: ${response.message}`);
21+
}
22+
return response.data;
23+
})
24+
.catch(err => {
25+
throw new Error(`Error fetching article data: ${err.message}`);
26+
});
27+
}
28+
29+
function updateFormFields(data) {
30+
document.getElementById("quick-weblog-title").value = data?.title;
31+
document.getElementById("quick-weblog-image_url").value = data?.image;
32+
document.getElementById("quick-weblog-image_description").value = "image via " + data?.source;
33+
document.getElementById("quick-weblog-quote").value = data?.description;
34+
}
35+
36+
function fetchAndPopulateFormFields(articleUrl, rapidApiKey) {
37+
if (articleUrl === "") {
38+
updateStatus("You must fill out <strong>Post URL</strong> to use Auto Fill.");
39+
return;
40+
}
41+
42+
if (rapidApiKey === "") {
43+
updateStatus("Your API Key is missing. Please update settings to use Auto Fill.");
44+
return;
45+
}
46+
47+
updateStatus("Attempting to fetch information and Auto Fill...");
48+
disableForm();
49+
50+
fetchArticleData(articleUrl, rapidApiKey)
51+
.then(data => {
52+
updateFormFields(data);
53+
updateStatus("Successfully used Auto Fill to populate form!");
54+
enableForm();
55+
})
56+
.catch(err => {
57+
updateStatus(err);
58+
console.error(err);
59+
enableForm();
60+
});
61+
}
62+
63+
function toggleFormElements(enable) {
64+
const form = document.getElementById("quick-weblog");
65+
const formElements = form.querySelectorAll("input, select, textarea");
66+
67+
formElements.forEach(element => {
68+
element.disabled = !enable;
69+
});
70+
}
71+
72+
function enableForm() {
73+
toggleFormElements(true);
74+
}
75+
76+
function disableForm() {
77+
toggleFormElements(false);
78+
}
79+
80+
function updateStatus(status) {
81+
document.getElementById("quick-weblog-status").innerHTML = status;
82+
}

quick-weblog.php

Lines changed: 140 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,165 +2,251 @@
22

33
/*
44
Plugin Name: Quick Weblog
5-
Plugin URI: https://fexd.github.io/wordpress-plugin-quick-weblog/
5+
Plugin URI: https://github.com/FeXd/wordpress-plugin-quick-weblog
66
Description: Quickly create a simple Post that highlights an existing news article.
7-
Version: 0.0.1
7+
Version: 0.0.2
88
Author: Arlin Schaffel
99
Author URI: https://github.com/FeXd
1010
License: MIT
1111
License URI: https://github.com/FeXd/wordpress-plugin-quick-weblog/blob/main/LICENSE.md
1212
Text Domain: quick-weblog
1313
*/
1414

15-
function quick_weblog_form() {
16-
?>
15+
function quick_weblog_form()
16+
{
17+
18+
$api_key = get_option('quick_weblog_api_key', '');
19+
20+
?>
1721
<style>
1822
#quick-weblog {
1923
padding: 1em 0;
2024
}
25+
2126
#quick-weblog div {
2227
padding: 0.75em 0;
2328
}
29+
2430
#quick-weblog div:nth-last-child(1) {
2531
padding-bottom: 0;
26-
}
32+
}
33+
2734
#quick-weblog div:nth-child(1) {
2835
padding-top: 0;
29-
}
36+
}
37+
3038
#quick-weblog div label {
3139
font-weight: 600;
3240
}
41+
3342
#quick-weblog-description {
3443
max-width: 520px;
3544
}
45+
3646
#quick-weblog div input,
3747
#quick-weblog div textarea,
3848
#quick-weblog div select {
3949
display: block;
4050
max-width: 600px;
4151
width: 99%;
4252
}
53+
4354
#quick-weblog div input[type=submit] {
4455
width: auto;
4556
}
4657
</style>
4758

59+
<script src="<?php echo plugin_dir_url(__FILE__); ?>quick-weblog.js"></script>
60+
61+
<script>
62+
window.addEventListener("DOMContentLoaded", (event) => {
63+
document.getElementById("quick-weblog-auto").addEventListener("click", (click_event) => {
64+
click_event.preventDefault();
65+
fetchAndPopulateFormFields(document.getElementById("quick-weblog-url").value, "<?php echo esc_js(wp_kses($api_key, array())); ?>");
66+
});
67+
});
68+
</script>
69+
4870
<p id="quick-weblog-description">Quickly create a simple Post that highlights an existing news article. Posts include a captioned image and quote with URL citation of original article. All fields are required.</p>
4971

5072
<div class="card">
51-
<form id="quick-weblog" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
73+
<form id="quick-weblog" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
74+
<div>
75+
<label for="url"><?php _e('Post URL', 'quick-weblog'); ?></label>
76+
<input type="text" name="url" id="quick-weblog-url" required>
77+
</div>
78+
5279
<div>
53-
<label for="url"><?php _e( 'Post URL', 'quick-weblog' ); ?></label>
54-
<input type="text" name="url" id="url" required>
80+
<button id="quick-weblog-auto">Auto Fill</button>
81+
<span id="quick-weblog-status">Attempt to auto fill form based on <strong>Post URL</strong>.</span>
5582
</div>
5683

5784
<div>
58-
<label for="title"><?php _e( 'Post Title', 'quick-weblog' ); ?></label>
59-
<input type="text" name="title" id="title" required>
85+
<label for="title"><?php _e('Post Title', 'quick-weblog'); ?></label>
86+
<input type="text" name="title" id="quick-weblog-title" required>
6087
</div>
6188

6289
<div>
63-
<label for="image_url"><?php _e( 'Image URL', 'quick-weblog' ); ?></label>
64-
<input type="text" name="image_url" id="image_url" required>
90+
<label for="image_url"><?php _e('Image URL', 'quick-weblog'); ?></label>
91+
<input type="text" name="image_url" id="quick-weblog-image_url" required>
6592
</div>
6693

6794
<div>
68-
<label for="image_description"><?php _e( 'Image Description', 'quick-weblog' ); ?></label>
69-
<input type="text" name="image_description" id="image_description" required>
95+
<label for="image_description"><?php _e('Image Description', 'quick-weblog'); ?></label>
96+
<input type="text" name="image_description" id="quick-weblog-image_description" required>
7097
</div>
7198

7299
<div>
73-
<label for="quote"><?php _e( 'Quote', 'quick-weblog' ); ?></label>
74-
<textarea name="quote" id="quote" rows="6" required></textarea>
100+
<label for="quote"><?php _e('Quote', 'quick-weblog'); ?></label>
101+
<textarea name="quote" id="quick-weblog-quote" rows="6" required></textarea>
75102
</div>
76103

77104
<div>
78-
<label for="category"><?php _e( 'Category', 'quick-weblog' ); ?></label>
79-
<?php wp_dropdown_categories( array( 'name' => 'category', 'orderby' => 'name', 'taxonomy' => 'category', 'selected' => 1) ); ?>
105+
<label for="category"><?php _e('Category', 'quick-weblog'); ?></label>
106+
<?php wp_dropdown_categories(array('name' => 'category', 'orderby' => 'name', 'taxonomy' => 'category', 'selected' => 1)); ?>
80107
</div>
81108

82109
<div>
83-
<label for="tags"><?php _e( 'Tags', 'quick-weblog' ); ?></label>
84-
<input type="text" name="tags" id="tags" required>
110+
<label for="tags"><?php _e('Tags', 'quick-weblog'); ?></label>
111+
<input type="text" name="tags" id="quick-weblog-tags" required>
112+
</div>
113+
114+
<div>
115+
<label for="post_date"><?php _e('Post Date', 'quick-weblog'); ?></label>
116+
<?php $default_date = date('Y-m-d\TH:i'); ?>
117+
<input type="datetime-local" name="post_date" id="quick-weblog-post_date" required value="<?php echo $default_date; ?>">
85118
</div>
86119

87120
<div>
88121
<input type="hidden" name="action" value="quick_weblog_submit_form">
89-
<?php wp_nonce_field( 'quick_weblog_submit_form', 'quick_weblog_form_nonce' ); ?>
90-
<input type="submit" value="<?php _e( 'Submit', 'quick-weblog' ); ?>">
122+
<?php wp_nonce_field('quick_weblog_submit_form', 'quick_weblog_form_nonce'); ?>
123+
<input type="submit" value="<?php _e('Submit', 'quick-weblog'); ?>">
91124
</div>
92125
</form>
93126
</div>
94-
<?php
127+
<?php
95128
}
96129

97-
function quick_weblog_add_menu_page() {
130+
function quick_weblog_add_menu_page()
131+
{
98132
add_menu_page(
99-
__( 'Quick Weblog', 'quick-weblog' ), // Page title
100-
__( 'Quick Weblog', 'quick-weblog' ), // Menu title
133+
__('Quick Weblog', 'quick-weblog'), // Page title
134+
__('Quick Weblog', 'quick-weblog'), // Menu title
101135
'manage_options', // Capability required to access the page
102136
'quick-weblog', // Menu slug
103137
'quick_weblog_menu_page', // Callback function to render the page
104138
'dashicons-welcome-write-blog', // Icon
105139
4.9021042 // Position in the menu
106140
);
141+
142+
add_submenu_page(
143+
'quick-weblog', // Parent slug
144+
__('API Settings', 'quick-weblog'), // Page title
145+
__('API Settings', 'quick-weblog'), // Menu title
146+
'manage_options', // Capability required to access the page
147+
'quick-weblog-settings', // Menu slug
148+
'quick_weblog_settings_page' // Callback function to render the page
149+
);
107150
}
108-
add_action( 'admin_menu', 'quick_weblog_add_menu_page' );
109151

110-
function quick_weblog_menu_page() {
111-
?>
152+
add_action('admin_menu', 'quick_weblog_add_menu_page');
153+
154+
function quick_weblog_menu_page()
155+
{
156+
?>
112157
<div class="wrap">
113-
<h1><?php _e( 'Quick Weblog', 'quick-weblog' ); ?></h1>
158+
<h1><?php _e('Quick Weblog', 'quick-weblog'); ?></h1>
114159
<?php quick_weblog_form(); ?>
115160
</div>
116-
<?php
161+
<?php
117162
}
118163

119-
function quick_weblog_submit_form() {
164+
function quick_weblog_submit_form()
165+
{
120166
// Check the nonce to verify the form submission
121-
if ( ! isset( $_POST['quick_weblog_form_nonce'] ) || ! wp_verify_nonce( $_POST['quick_weblog_form_nonce'], 'quick_weblog_submit_form' ) ) {
122-
wp_die( __( 'Error: Invalid nonce.', 'quick-weblog' ) );
167+
if (!isset($_POST['quick_weblog_form_nonce']) || !wp_verify_nonce($_POST['quick_weblog_form_nonce'], 'quick_weblog_submit_form')) {
168+
wp_die(__('Error: Invalid nonce.', 'quick-weblog'));
123169
}
124170

125171
// Get the form data
126-
$title = sanitize_text_field( $_POST['title'] );
127-
$image_url = sanitize_text_field( $_POST['image_url'] );
128-
$image_description = sanitize_text_field( $_POST['image_description'] );
129-
$quote = sanitize_text_field( $_POST['quote'] );
130-
$url = esc_url_raw( $_POST['url'] );
131-
$category = intval( $_POST['category'] );
132-
$tags = sanitize_text_field( $_POST['tags'] );
172+
$title = sanitize_text_field($_POST['title']);
173+
$image_url = sanitize_text_field($_POST['image_url']);
174+
$image_description = sanitize_text_field($_POST['image_description']);
175+
$quote = sanitize_text_field($_POST['quote']);
176+
$url = esc_url_raw($_POST['url']);
177+
$category = intval($_POST['category']);
178+
$tags = sanitize_text_field($_POST['tags']);
133179

134180
// Create block content
135181
$image_block = '<!-- wp:image {"url":"' . esc_attr($image_url) . '","alt":"' . esc_attr($image_description) . '"} -->' .
136-
'<figure class="wp-block-image">' .
137-
'<img src="' . esc_url($image_url) . '" alt="' . esc_attr($image_description) . '">' .
138-
'<figcaption>' . esc_html($image_description) . '</figcaption>' .
139-
'</figure><!-- /wp:image -->';
182+
'<figure class="wp-block-image">' .
183+
'<img src="' . esc_url($image_url) . '" alt="' . esc_attr($image_description) . '">' .
184+
'<figcaption>' . esc_html($image_description) . '</figcaption>' .
185+
'</figure><!-- /wp:image -->';
140186

141187
$quote_block = '<!-- wp:quote {"citation":"' . esc_attr($url) . '"} -->' .
142-
'<blockquote class="wp-block-quote">' .
143-
'<p>' . esc_html($quote) . '</p>' .
144-
'<cite><a href="' . esc_url($url) . '" target="_blank" rel="noreferrer noopener">' . esc_html($url) . '</a></cite>' .
145-
'</blockquote><!-- /wp:quote -->';
188+
'<blockquote class="wp-block-quote">' .
189+
'<p>' . esc_html($quote) . '</p>' .
190+
'<cite><a href="' . esc_url($url) . '" target="_blank" rel="noreferrer noopener">' . esc_html($url) . '</a></cite>' .
191+
'</blockquote><!-- /wp:quote -->';
146192

147193
$block_content = $image_block . $quote_block;
148194

149195
// Create a new post
150196
$post_data = array(
151197
'post_title' => $title,
152198
'post_content' => $block_content,
153-
'post_category' => array( $category ),
199+
'post_category' => array($category),
154200
'tags_input' => $tags,
155201
'post_status' => 'publish'
156202
);
157-
$post_id = wp_insert_post( $post_data );
203+
$post_id = wp_insert_post($post_data);
158204

159205
// Redirect to the new post
160-
wp_redirect( get_permalink( $post_id ) );
206+
wp_redirect(get_permalink($post_id));
161207
exit();
162208
}
163209

164-
add_action( 'admin_post_quick_weblog_submit_form', 'quick_weblog_submit_form' );
210+
add_action('admin_post_quick_weblog_submit_form', 'quick_weblog_submit_form');
165211

212+
function quick_weblog_settings_init()
213+
{
214+
add_settings_section('quick_weblog_api_section', 'API Settings', null, 'quick-weblog');
215+
add_settings_field('quick_weblog_api_key', 'API Key', 'quick_weblog_api_key_callback', 'quick-weblog', 'quick_weblog_api_section');
216+
217+
register_setting('quick_weblog_settings', 'quick_weblog_use_api', 'boolval');
218+
register_setting('quick_weblog_settings', 'quick_weblog_api_key', 'sanitize_text_field');
219+
}
220+
221+
add_action('admin_init', 'quick_weblog_settings_init');
222+
223+
function quick_weblog_api_key_callback()
224+
{
225+
$value = get_option('quick_weblog_api_key', '');
226+
echo '<input type="text" name="quick_weblog_api_key" value="' . esc_attr($value) . '" />';
227+
}
228+
229+
function quick_weblog_add_settings_link($links)
230+
{
231+
$settings_link = '<a href="' . admin_url('options-general.php?page=quick-weblog') . '">' . __('Settings', 'quick-weblog') . '</a>';
232+
array_push($links, $settings_link);
233+
return $links;
234+
}
235+
236+
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'quick_weblog_add_settings_link');
237+
238+
function quick_weblog_settings_page()
239+
{
166240
?>
241+
<div class="wrap">
242+
<h1><?php _e('API Settings', 'quick-weblog'); ?></h1>
243+
<form method="post" action="options.php">
244+
<?php settings_fields('quick_weblog_settings'); ?>
245+
<?php do_settings_sections('quick-weblog'); ?>
246+
<?php submit_button(); ?>
247+
</form>
248+
</div>
249+
<?php
250+
}
251+
252+
?>

0 commit comments

Comments
 (0)