2
2
3
3
/*
4
4
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
6
6
Description: Quickly create a simple Post that highlights an existing news article.
7
- Version: 0.0.1
7
+ Version: 0.0.2
8
8
Author: Arlin Schaffel
9
9
Author URI: https://github.com/FeXd
10
10
License: MIT
11
11
License URI: https://github.com/FeXd/wordpress-plugin-quick-weblog/blob/main/LICENSE.md
12
12
Text Domain: quick-weblog
13
13
*/
14
14
15
- function quick_weblog_form () {
16
- ?>
15
+ function quick_weblog_form ()
16
+ {
17
+
18
+ $ api_key = get_option ('quick_weblog_api_key ' , '' );
19
+
20
+ ?>
17
21
<style>
18
22
#quick-weblog {
19
23
padding: 1em 0;
20
24
}
25
+
21
26
#quick-weblog div {
22
27
padding: 0.75em 0;
23
28
}
29
+
24
30
#quick-weblog div:nth-last-child(1) {
25
31
padding-bottom: 0;
26
- }
32
+ }
33
+
27
34
#quick-weblog div:nth-child(1) {
28
35
padding-top: 0;
29
- }
36
+ }
37
+
30
38
#quick-weblog div label {
31
39
font-weight: 600;
32
40
}
41
+
33
42
#quick-weblog-description {
34
43
max-width: 520px;
35
44
}
45
+
36
46
#quick-weblog div input,
37
47
#quick-weblog div textarea,
38
48
#quick-weblog div select {
39
49
display: block;
40
50
max-width: 600px;
41
51
width: 99%;
42
52
}
53
+
43
54
#quick-weblog div input[type=submit] {
44
55
width: auto;
45
56
}
46
57
</style>
47
58
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
+
48
70
<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>
49
71
50
72
<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
+
52
79
<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 >
55
82
</div>
56
83
57
84
<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>
60
87
</div>
61
88
62
89
<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>
65
92
</div>
66
93
67
94
<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>
70
97
</div>
71
98
72
99
<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>
75
102
</div>
76
103
77
104
<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 )); ?>
80
107
</div>
81
108
82
109
<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 ; ?> ">
85
118
</div>
86
119
87
120
<div>
88
121
<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 ' ); ?> ">
91
124
</div>
92
125
</form>
93
126
</div>
94
- <?php
127
+ <?php
95
128
}
96
129
97
- function quick_weblog_add_menu_page () {
130
+ function quick_weblog_add_menu_page ()
131
+ {
98
132
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
101
135
'manage_options ' , // Capability required to access the page
102
136
'quick-weblog ' , // Menu slug
103
137
'quick_weblog_menu_page ' , // Callback function to render the page
104
138
'dashicons-welcome-write-blog ' , // Icon
105
139
4.9021042 // Position in the menu
106
140
);
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
+ );
107
150
}
108
- add_action ( 'admin_menu ' , 'quick_weblog_add_menu_page ' );
109
151
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
+ ?>
112
157
<div class="wrap">
113
- <h1><?php _e ( 'Quick Weblog ' , 'quick-weblog ' ); ?> </h1>
158
+ <h1><?php _e ('Quick Weblog ' , 'quick-weblog ' ); ?> </h1>
114
159
<?php quick_weblog_form (); ?>
115
160
</div>
116
- <?php
161
+ <?php
117
162
}
118
163
119
- function quick_weblog_submit_form () {
164
+ function quick_weblog_submit_form ()
165
+ {
120
166
// 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 ' ) );
123
169
}
124
170
125
171
// 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 ' ]);
133
179
134
180
// Create block content
135
181
$ 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 --> ' ;
140
186
141
187
$ 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 --> ' ;
146
192
147
193
$ block_content = $ image_block . $ quote_block ;
148
194
149
195
// Create a new post
150
196
$ post_data = array (
151
197
'post_title ' => $ title ,
152
198
'post_content ' => $ block_content ,
153
- 'post_category ' => array ( $ category ),
199
+ 'post_category ' => array ($ category ),
154
200
'tags_input ' => $ tags ,
155
201
'post_status ' => 'publish '
156
202
);
157
- $ post_id = wp_insert_post ( $ post_data );
203
+ $ post_id = wp_insert_post ($ post_data );
158
204
159
205
// Redirect to the new post
160
- wp_redirect ( get_permalink ( $ post_id ) );
206
+ wp_redirect (get_permalink ($ post_id) );
161
207
exit ();
162
208
}
163
209
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 ' );
165
211
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
+ {
166
240
?>
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