1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: WP Pocket |
---|
4 | Plugin URI: http://www.wp-pocket.com/ |
---|
5 | Description: WP Pocket is a Wordpress plugin, with which you can post interessting links from Pocket automaticly on your blog - with autogenerated titles, discriptions and tags. |
---|
6 | Author: Jan Karres |
---|
7 | Author URI: http://www.jankarres.de/ |
---|
8 | Version: 1.0 |
---|
9 | */ |
---|
10 | |
---|
11 | if(!class_exists('wppocketer')) { |
---|
12 | class wppocketer { |
---|
13 | private $wpdb; |
---|
14 | |
---|
15 | // Pocket default API-Key by developer |
---|
16 | private $global_pocket_api_key; |
---|
17 | |
---|
18 | function __construct() { |
---|
19 | global $wpdb; |
---|
20 | |
---|
21 | // Set database |
---|
22 | $this->wpdb = $wpdb; |
---|
23 | |
---|
24 | // Check WP version |
---|
25 | add_action('init', array(&$this, 'requires_wordpress_version')); |
---|
26 | |
---|
27 | // Change plugin status hooks |
---|
28 | register_activation_hook(__FILE__, array(&$this, 'activate')); |
---|
29 | register_deactivation_hook(__FILE__, array(&$this, 'deactivate')); |
---|
30 | |
---|
31 | // Hook a main page |
---|
32 | add_action('admin_menu', array(&$this, 'main_link')); |
---|
33 | |
---|
34 | // Hook an option page |
---|
35 | add_action('admin_menu', array(&$this, 'options_link')); |
---|
36 | |
---|
37 | // Add javascripts of the plugin in the system |
---|
38 | wp_register_script('wppocket_main', plugins_url('js/main.js', __FILE__), array('jquery', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-droppable')); |
---|
39 | |
---|
40 | // Pocket default API-Key by developer |
---|
41 | $this->global_pocket_api_key = '074A0c78dTi1bI26f2pU4b0X9dTbe798'; |
---|
42 | } |
---|
43 | |
---|
44 | function requires_wordpress_version() { |
---|
45 | global $wp_version; |
---|
46 | |
---|
47 | include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
---|
48 | $plugin = plugin_basename(__FILE__); |
---|
49 | |
---|
50 | // Check WP version |
---|
51 | if (version_compare($wp_version, '3.4', '<')) { |
---|
52 | if(is_plugin_active($plugin)) { |
---|
53 | deactivate_plugins($plugin); |
---|
54 | wp_die('<i>WP Pocket</i> requires WordPress 3.4 or higher. The plugin has been deactivated! Please update WordPress and try again.<br /><br />Back to <a href="' . admin_url() . '">WordPress Dashboard</a>.'); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | // Check if cURL is available |
---|
59 | if (!function_exists('curl_init')){ |
---|
60 | if(is_plugin_active($plugin)) { |
---|
61 | deactivate_plugins($plugin); |
---|
62 | wp_die('<i>WP Pocket</i> requires cURL. The plugin has been deactivated! Please activate it or ask your webhoster to activate it.<br /><br />Back to <a href="' . admin_url() . '">WordPress Dashboard</a>.'); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | function activate() { |
---|
68 | // Create database table for links |
---|
69 | $sql = ' |
---|
70 | CREATE TABLE IF NOT EXISTS `' . $this->wpdb->prefix . 'wppocket_posted_links` ( |
---|
71 | `id` int(255) NOT NULL AUTO_INCREMENT, |
---|
72 | `url` varchar(1000) NOT NULL, |
---|
73 | `timestamp` int(255) NOT NULL, |
---|
74 | PRIMARY KEY (`id`) |
---|
75 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; |
---|
76 | '; |
---|
77 | $this->wpdb->query($sql); |
---|
78 | |
---|
79 | // Create settings in database |
---|
80 | $sql = ' |
---|
81 | INSERT IGNORE INTO `' . $this->wpdb->prefix . 'options` ( |
---|
82 | `option_name` , |
---|
83 | `option_value` , |
---|
84 | `autoload` |
---|
85 | ) VALUES |
---|
86 | ("wppocket_links_to_use", "0", "yes"), |
---|
87 | ("wppocket_order", "0", "yes"), |
---|
88 | ("wppocket_mark_as_readed", "0", "yes"), |
---|
89 | ("wppocket_clean_url", "1", "yes"), |
---|
90 | ("wppocket_meta_keyword_as_tags", "0", "yes"), |
---|
91 | ("wppocket_reference_wppocket", "0", "yes"), |
---|
92 | ("wppocket_ping_status", "open", "yes"), |
---|
93 | ("wppocket_comment_status", "open", "yes"), |
---|
94 | ("wppocket_post_status", "draft", "yes"), |
---|
95 | ("wppocket_post_category", "1", "yes"), |
---|
96 | ("wppocket_pocket_username", "", "yes"), |
---|
97 | ("wppocket_pocket_password", "", "yes"), |
---|
98 | ("wppocket_title", "WP Pocket link collection: %%month%% with %%count%% links", "yes"), |
---|
99 | ("wppocket_introduction_text", "", "yes"), |
---|
100 | ("wppocket_statements_text", "", "yes"), |
---|
101 | ("wppocket_entry_building", "<strong>%%title%%</strong>[[: %%description%%]][[ - Tags: %%tags%%]] - %%url%%", "yes"), |
---|
102 | ("wppocket_key_a", "' . $this->keygen(128) . '", "yes"), |
---|
103 | ("wppocket_key_b", "' . $this->keygen(128) . '", "yes") |
---|
104 | '; |
---|
105 | $this->wpdb->query($sql); |
---|
106 | |
---|
107 | $sql = ' |
---|
108 | INSERT IGNORE INTO `' . $this->wpdb->prefix . 'options` ( |
---|
109 | `option_name` , |
---|
110 | `option_value` , |
---|
111 | `autoload` |
---|
112 | ) VALUES |
---|
113 | ("wppocket_pocket_api_key_custom", "0", "yes"), |
---|
114 | ("wppocket_pocket_api_key", "' . $this->xcrypt(0, $this->global_pocket_api_key) . '", "yes") |
---|
115 | '; |
---|
116 | $this->wpdb->query($sql); |
---|
117 | } |
---|
118 | |
---|
119 | function deactivate() { |
---|
120 | // nothing to do, because user cam reactivate |
---|
121 | } |
---|
122 | |
---|
123 | // Gennerator for random keys for encrytion |
---|
124 | private function keygen($length) { |
---|
125 | /* |
---|
126 | * Required parameters: |
---|
127 | * $length = length of the gennerated key |
---|
128 | */ |
---|
129 | |
---|
130 | $characters = '0123456789qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM'; |
---|
131 | |
---|
132 | for ($i = 0; $i < $length; $i++) { |
---|
133 | $key .= $characters[mt_rand(0, strlen($characters) - 1)]; |
---|
134 | } |
---|
135 | |
---|
136 | return $key; |
---|
137 | } |
---|
138 | |
---|
139 | // En- and decrypt strings |
---|
140 | private function xcrypt($mode, $string) { |
---|
141 | /* |
---|
142 | * Required parameters: |
---|
143 | * $mode = encrypt (0), decrypt (1) |
---|
144 | * $string = String, who shout en-/decrypt |
---|
145 | */ |
---|
146 | |
---|
147 | // Get crypt keys |
---|
148 | $sql = 'SELECT `wppocket_key_a`, `wppocket_key_b` FROM `' . $this->wpdb->prefix . 'options`'; |
---|
149 | $keys = $this->wpdb->get_row($sql); |
---|
150 | |
---|
151 | if ($mode == 0) { // Encrypt |
---|
152 | return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($keys->wppocket_key_a), $string, MCRYPT_MODE_CBC, md5(md5($keys->wppocket_key_b)))); |
---|
153 | }elseif ($mode == 1) { // Decrypt |
---|
154 | return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($keys->wppocket_key_a), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($keys->wppocket_key_b))), "\0"); |
---|
155 | }else { // False mode |
---|
156 | return false; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | private function cURL($url, $timeout = 5) { |
---|
161 | // Init cURL |
---|
162 | $cURL = curl_init(); |
---|
163 | |
---|
164 | // Settings |
---|
165 | curl_setopt($cURL, CURLOPT_URL, $url); |
---|
166 | curl_setopt($cURL, CURLOPT_HEADER, 0); |
---|
167 | curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); |
---|
168 | curl_setopt($cURL, CURLOPT_TIMEOUT, $timeout); |
---|
169 | |
---|
170 | // Download |
---|
171 | $output = curl_exec($cURL); |
---|
172 | |
---|
173 | // Close Connection |
---|
174 | curl_close($cURL); |
---|
175 | |
---|
176 | // Return |
---|
177 | return $output; |
---|
178 | } |
---|
179 | |
---|
180 | function cleanUpURL($url) { |
---|
181 | // Filter $_GET[p], falls vorhanden, aus link |
---|
182 | preg_match('/p=(.*)/', $url, $matches); |
---|
183 | $matches = explode('&', $matches[0]); |
---|
184 | |
---|
185 | // Clean url |
---|
186 | $url = explode('?', $url); |
---|
187 | $url = $url[0]; |
---|
188 | |
---|
189 | // If $_GET[p] founded, add this |
---|
190 | if (count($matches) != 0 && $matches[0] != '') { |
---|
191 | $url = $url . '?' . $matches[0]; |
---|
192 | } |
---|
193 | |
---|
194 | return $url; |
---|
195 | } |
---|
196 | |
---|
197 | function replaceEntryBuilding($title, $description, $url, $tags, $replace = true) { |
---|
198 | $wppocket_entry_building = get_option('wppocket_entry_building'); |
---|
199 | |
---|
200 | // Set the variables, if should do this |
---|
201 | if ($replace) { |
---|
202 | $wppocket_entry_building = str_replace('%%title%%', $title, $wppocket_entry_building); |
---|
203 | $wppocket_entry_building = str_replace('%%description%%', $description, $wppocket_entry_building); |
---|
204 | $wppocket_entry_building = str_replace('%%url%%', $url, $wppocket_entry_building); |
---|
205 | $wppocket_entry_building = str_replace('%%tags%%', $tags, $wppocket_entry_building); |
---|
206 | } |
---|
207 | |
---|
208 | // Explode the entry building, by replace ]] throw [[ for find seperators |
---|
209 | $wppocket_entry_building = explode('[[', str_replace(']]','[[', $wppocket_entry_building)); |
---|
210 | |
---|
211 | // Return the result Array |
---|
212 | return $wppocket_entry_building; |
---|
213 | } |
---|
214 | |
---|
215 | function getReplacedTitle($countLinks, $replace = true) { |
---|
216 | $wppocket_title = get_option('wppocket_title'); |
---|
217 | |
---|
218 | // Set the variables, if should do this |
---|
219 | if ($replace) { |
---|
220 | $wppocket_title = str_replace('%%day%%', date('l'), $wppocket_title); |
---|
221 | $wppocket_title = str_replace('%%month%%', date('F'), $wppocket_title); |
---|
222 | $wppocket_title = str_replace('%%year%%', date('Y'), $wppocket_title); |
---|
223 | $wppocket_title = str_replace('%%count%%', $countLinks, $wppocket_title); |
---|
224 | } |
---|
225 | |
---|
226 | return trim($wppocket_title); |
---|
227 | } |
---|
228 | |
---|
229 | function getReplacedIntroductionText($replace = true) { |
---|
230 | $wppocket_introduction_text = get_option('wppocket_introduction_text'); |
---|
231 | |
---|
232 | // Set the variables, if should do this |
---|
233 | if ($replace) { |
---|
234 | $wppocket_introduction_text = str_replace('%%day%%', date('l'), $wppocket_introduction_text); |
---|
235 | $wppocket_introduction_text = str_replace('%%month%%', date('F'), $wppocket_introduction_text); |
---|
236 | $wppocket_introduction_text = str_replace('%%year%%', date('Y'), $wppocket_introduction_text); |
---|
237 | } |
---|
238 | |
---|
239 | return trim($wppocket_introduction_text); |
---|
240 | } |
---|
241 | |
---|
242 | function getReplacedStatementsText($replace = true) { |
---|
243 | $wppocket_statements_text = get_option('wppocket_statements_text'); |
---|
244 | |
---|
245 | // Set the variables, if should do this |
---|
246 | if ($replace) { |
---|
247 | $wppocket_statements_text = str_replace('%%day%%', date('l'), $wppocket_statements_text); |
---|
248 | $wppocket_statements_text = str_replace('%%month%%', date('F'), $wppocket_statements_text); |
---|
249 | $wppocket_statements_text = str_replace('%%year%%', date('Y'), $wppocket_statements_text); |
---|
250 | } |
---|
251 | |
---|
252 | return trim($wppocket_statements_text); |
---|
253 | } |
---|
254 | |
---|
255 | function checkPocketAccess() { |
---|
256 | // Check if username, password and api-key is set, if not, print error |
---|
257 | if (get_option('wppocket_pocket_username') == '' || get_option('wppocket_pocket_password') == '' || get_option('wppocket_pocket_api_key') == '') { |
---|
258 | ?> |
---|
259 | <div id="message" class="error"> |
---|
260 | <p><strong>WP Pocket can NOT work. Please save your Pocket username, password and API-Key (optional)!</strong></p> |
---|
261 | </div> |
---|
262 | <?php |
---|
263 | return false; |
---|
264 | }else { // Test the settings |
---|
265 | // Init cURL |
---|
266 | $getAuth = $this->cURL('https://readitlaterlist.com/v2/auth?username=' . $this->xcrypt(1, get_option('wppocket_pocket_username')) . '&password=' . $this->xcrypt(1, get_option('wppocket_pocket_password')) . '&apikey=' . $this->xcrypt(1, get_option('wppocket_pocket_api_key'))); |
---|
267 | |
---|
268 | // Check if, user data ad api key correct |
---|
269 | if ($getAuth == '400 Bad Request') { |
---|
270 | ?> |
---|
271 | <div id="message" class="error"> |
---|
272 | <p><strong>Your API-Key it not valid. Please correct it, else WP Pocket can NOT work.</strong></p> |
---|
273 | </div> |
---|
274 | <?php |
---|
275 | return false; |
---|
276 | }elseif ($getAuth == '401 Unauthorized') { |
---|
277 | ?> |
---|
278 | <div id="message" class="error"> |
---|
279 | <p><strong>Your Pocket username and/or password it not valid. Please correct that, else WP Pocket can NOT work.</strong></p> |
---|
280 | </div> |
---|
281 | <?php |
---|
282 | return false; |
---|
283 | }else { |
---|
284 | return true; |
---|
285 | } |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | function support_div($display = true) { |
---|
290 | ?> |
---|
291 | <div class="support"<?php if (!$display) { echo ' style="display: none;"'; } ?>> |
---|
292 | <h3>support me</h3> |
---|
293 | |
---|
294 | <p>If you use the plugin and like it, you can say thanks and support me.</p> |
---|
295 | |
---|
296 | <h3>follow me on twitter</h3> |
---|
297 | |
---|
298 | <p> |
---|
299 | <a href="https://twitter.com/jankarres" class="twitter-follow-button" data-show-count="false" data-lang="en" data-size="large">Follow @jankarres</a> |
---|
300 | <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> |
---|
301 | </p> |
---|
302 | |
---|
303 | <h3>donate on flattr</h3> |
---|
304 | |
---|
305 | <a href="http://flattr.com/thing/881252/WP-Pocket" target="_blank"> |
---|
306 | <img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /> |
---|
307 | </a> |
---|
308 | </div> |
---|
309 | <?php |
---|
310 | } |
---|
311 | |
---|
312 | function options_link() { |
---|
313 | // Start the view of the options page |
---|
314 | add_options_page('WP Pocket Settings', 'WP Pocket', 'manage_options', 'settings_pocket_poster', array($this, 'options_page_view')); |
---|
315 | } |
---|
316 | |
---|
317 | function options_page_view() { |
---|
318 | // Add option page style |
---|
319 | wp_enqueue_style('wppocket_options', plugins_url('css/options.css', __FILE__)); |
---|
320 | wp_enqueue_style('wppocket_support', plugins_url('css/support.css', __FILE__)); |
---|
321 | ?> |
---|
322 | <div class="wrap wppocket_options"> |
---|
323 | <?php |
---|
324 | // Start the Controller of the options page |
---|
325 | $this->options_page_controller(); |
---|
326 | ?> |
---|
327 | |
---|
328 | <div id="icon-options-general" class="icon32"><br></div> |
---|
329 | <h2>Einstellungen › WP Pocket</h2> |
---|
330 | |
---|
331 | <?php $this->support_div(); ?> |
---|
332 | |
---|
333 | <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> |
---|
334 | <h3>Pocket Login</h3> |
---|
335 | <table class="form-table"> |
---|
336 | <tr> |
---|
337 | <th>Pocket Username</th> |
---|
338 | <td> |
---|
339 | <input type="text" size="57" name="wppocket_pocket_username" value="<?php if (get_option('wppocket_pocket_username') != '') { echo $this->xcrypt(1, get_option('wppocket_pocket_username')); } ?>" /> |
---|
340 | </td> |
---|
341 | </tr> |
---|
342 | <tr> |
---|
343 | <th>Pocket Password</th> |
---|
344 | <td> |
---|
345 | <input type="password" size="57" name="wppocket_pocket_password" value="" /> |
---|
346 | <p class="description">If you do not wish to edit something, leave it blank.</p> |
---|
347 | </td> |
---|
348 | </tr> |
---|
349 | <tr> |
---|
350 | <th>Pocket API-Key</th> |
---|
351 | <td> |
---|
352 | <input type="text" size="57" name="wppocket_pocket_api_key" value="<?php if(get_option('wppocket_pocket_api_key_custom') == 1) { echo $this->xcrypt(1, get_option('wppocket_pocket_api_key')); } ?>" /> |
---|
353 | |
---|
354 | <?php if(get_option('wppocket_pocket_api_key_custom') == 1) { ?> |
---|
355 | <label><input name="wppocket_pocket_api_key_custom_remove" type="checkbox" value="1" /> Use the default API-Key</label> |
---|
356 | <?php } ?> |
---|
357 | |
---|
358 | <p class="description">If you wish to use a private API-Key, please enter one. <strong>It is not necessary!</strong><br /> |
---|
359 | You can get one on <a href="http://getpocket.com/api/signup/" target="_blank">http://getpocket.com/api/signup/</a>.</p> |
---|
360 | </td> |
---|
361 | </tr> |
---|
362 | </table> |
---|
363 | |
---|
364 | <h3>Data processing</h3> |
---|
365 | <table class="form-table"> |
---|
366 | <tr> |
---|
367 | <th>Links to use</th> |
---|
368 | <td> |
---|
369 | <select name="wppocket_links_to_use"> |
---|
370 | <option value="0" <?php selected('0', get_option('wppocket_links_to_use')); ?>>Only unread links, which are unposted</option> |
---|
371 | <option value="1" <?php selected('1', get_option('wppocket_links_to_use')); ?>>Only read links, which are unposted</option> |
---|
372 | <option value="2" <?php selected('2', get_option('wppocket_links_to_use')); ?>>All links, which are unposted</option> |
---|
373 | <option value="3" <?php selected('3', get_option('wppocket_links_to_use')); ?>>All unread links, also already posted ones</option> |
---|
374 | <option value="4" <?php selected('4', get_option('wppocket_links_to_use')); ?>>All read links, also already posted ones</option> |
---|
375 | <option value="5" <?php selected('5', get_option('wppocket_links_to_use')); ?>>All links, also already posted ones</option> |
---|
376 | </select> |
---|
377 | <p class="description">Which links should be used for the new post?</p> |
---|
378 | </td> |
---|
379 | </tr> |
---|
380 | <tr> |
---|
381 | <th>Order</th> |
---|
382 | <td> |
---|
383 | <select name="wppocket_order"> |
---|
384 | <option value="0" <?php selected('0', get_option('wppocket_order')); ?>>Newest to oldest</option> |
---|
385 | <option value="1" <?php selected('1', get_option('wppocket_order')); ?>>Oldest to newest</option> |
---|
386 | </select> |
---|
387 | <p class="description">Which order should have the links in a new post?</p> |
---|
388 | </td> |
---|
389 | </tr> |
---|
390 | <tr> |
---|
391 | <th>Mark as readed</th> |
---|
392 | <td> |
---|
393 | <select name="wppocket_mark_as_readed"> |
---|
394 | <option value="1" <?php selected('1', get_option('wppocket_mark_as_readed')); ?>>Yes</option> |
---|
395 | <option value="0" <?php selected('0', get_option('wppocket_mark_as_readed')); ?>>No</option> |
---|
396 | </select> |
---|
397 | <p class="description">Should the links be marked as read in Pocket, after post?</p> |
---|
398 | </td> |
---|
399 | </tr> |
---|
400 | <tr> |
---|
401 | <th>Clean up URL</th> |
---|
402 | <td> |
---|
403 | <select name="wppocket_clean_url"> |
---|
404 | <option value="1" <?php selected('1', get_option('wppocket_clean_url')); ?>>Yes</option> |
---|
405 | <option value="0" <?php selected('0', get_option('wppocket_clean_url')); ?>>No</option> |
---|
406 | </select> |
---|
407 | <p class="description">If enabled, everything after the <i>?</i> in the url will be cut out. For example: |
---|
408 | <i>http://example.de/title-of-post/?utm_source=feedreader</i> will be edited to <i>http://example.de/title-of-post/</i> - |
---|
409 | The <i>p</i> parameter, which is used by default worldpress, will not be cleared - |
---|
410 | <strong>Sometimes necessary information will be lost!</strong></p> |
---|
411 | </td> |
---|
412 | </tr> |
---|
413 | <tr> |
---|
414 | <th>Use Meta Tag as Tags</th> |
---|
415 | <td> |
---|
416 | <select name="wppocket_meta_keyword_as_tags"> |
---|
417 | <option value="1" <?php selected('1', get_option('wppocket_meta_keyword_as_tags')); ?>>Yes</option> |
---|
418 | <option value="0" <?php selected('0', get_option('wppocket_meta_keyword_as_tags')); ?>>No</option> |
---|
419 | </select> |
---|
420 | <p class="description">If there are no tags given, the meta tags will be used as tags.</p> |
---|
421 | </td> |
---|
422 | </tr> |
---|
423 | <tr> |
---|
424 | <th>WP Pocket reference</th> |
---|
425 | <td> |
---|
426 | <select name="wppocket_reference_wppocket"> |
---|
427 | <option value="1" <?php selected('1', get_option('wppocket_reference_wppocket')); ?>>Yes</option> |
---|
428 | <option value="0" <?php selected('0', get_option('wppocket_reference_wppocket')); ?>>No</option> |
---|
429 | </select> |
---|
430 | <p class="description">Should there be a notification, that this post was generated by <i>WP Pocket</i>, at the end of the generated posts?</p> |
---|
431 | </td> |
---|
432 | </tr> |
---|
433 | </table> |
---|
434 | |
---|
435 | <h3>Post settings</h3> |
---|
436 | <table class="form-table"> |
---|
437 | <tr> |
---|
438 | <th>Post status</th> |
---|
439 | <td> |
---|
440 | <select name="wppocket_post_status"> |
---|
441 | <option value="publish" <?php selected('publish', get_option('wppocket_post_status')); ?>>Publish</option> |
---|
442 | <option value="pending" <?php selected('pending', get_option('wppocket_post_status')); ?>>Pending</option> |
---|
443 | <option value="draft" <?php selected('draft', get_option('wppocket_post_status')); ?>>Draft</option> |
---|
444 | </select> |
---|
445 | <p class="description">After generating the post, how should it be saved?</p> |
---|
446 | </td> |
---|
447 | </tr> |
---|
448 | <tr> |
---|
449 | <th>Post category</th> |
---|
450 | <td> |
---|
451 | <?php |
---|
452 | wp_dropdown_categories(array( |
---|
453 | 'name' => 'wppocket_post_category', |
---|
454 | 'hide_empty' => 0, |
---|
455 | 'selected' => get_option('wppocket_post_category') |
---|
456 | )); |
---|
457 | ?> |
---|
458 | <p class="description">Which category should the post be saved in?</p> |
---|
459 | </td> |
---|
460 | </tr> |
---|
461 | <tr> |
---|
462 | <th>Commect status</th> |
---|
463 | <td> |
---|
464 | <select name="wppocket_comment_status"> |
---|
465 | <option value="open" <?php selected('open', get_option('wppocket_comment_status')); ?>>Open</option> |
---|
466 | <option value="close" <?php selected('close', get_option('wppocket_comment_status')); ?>>Close</option> |
---|
467 | </select> |
---|
468 | <p class="description">Should the generated posts be commentable?</p> |
---|
469 | </td> |
---|
470 | </tr> |
---|
471 | <tr> |
---|
472 | <th>Ping status</th> |
---|
473 | <td> |
---|
474 | <select name="wppocket_ping_status"> |
---|
475 | <option value="open" <?php selected('open', get_option('wppocket_ping_status')); ?>>Open</option> |
---|
476 | <option value="close" <?php selected('close', get_option('wppocket_ping_status')); ?>>Close</option> |
---|
477 | </select> |
---|
478 | <p class="description">Should the generated posts be pingable?</p> |
---|
479 | </td> |
---|
480 | </tr> |
---|
481 | </table> |
---|
482 | |
---|
483 | <h3>Post content matching</h3> |
---|
484 | <table class="form-table"> |
---|
485 | <tr> |
---|
486 | <th>Title</th> |
---|
487 | <td> |
---|
488 | <input type="text" size="57" name="wppocket_title" value="<?php echo get_option('wppocket_title'); ?>" /> |
---|
489 | <p class="description"> |
---|
490 | This is the title of each post, which WP Pocket generates.<br /> |
---|
491 | <strong>Use <i>%%day%%</i> for the day, on which the post is being generated; <i>%%month%%</i> for the month; |
---|
492 | <i>%%year%%</i> for the year; <i>%%count%%</i> to get the number of posted links.</strong> |
---|
493 | </p> |
---|
494 | </td> |
---|
495 | </tr> |
---|
496 | <tr> |
---|
497 | <th>Entry building</th> |
---|
498 | <td> |
---|
499 | <?php |
---|
500 | $args = array( |
---|
501 | 'textarea_name' => 'wppocket_entry_building', |
---|
502 | 'textarea_rows' => 5 |
---|
503 | ); |
---|
504 | wp_editor(get_option('wppocket_entry_building'), 'wppocket_entry_building', $args); |
---|
505 | ?> |
---|
506 | <p class="description"> |
---|
507 | Configure how a link should be shown. If you wish to show a part of the text only if a variable is not empty, |
---|
508 | write the text and the variable in double square brackets. For example: %%title%%[[ - Tags: %%tags%%]]<br /> |
---|
509 | <strong>Use <i>%%title%%</i> for title of the link in pocket; <i>%%description%%</i> for the meta description of the link; |
---|
510 | <i>%%url%%</i> for the link itself; <i>%%tags%%</i> for added tags in pocket.</strong> |
---|
511 | </p> |
---|
512 | </td> |
---|
513 | </tr> |
---|
514 | <tr> |
---|
515 | <th>Introduction text</th> |
---|
516 | <td> |
---|
517 | <?php |
---|
518 | $args = array( |
---|
519 | 'textarea_name' => 'wppocket_introduction_text', |
---|
520 | 'textarea_rows' => 5 |
---|
521 | ); |
---|
522 | wp_editor(get_option('wppocket_introduction_text'), 'wppocket_introduction_text', $args); |
---|
523 | ?> |
---|
524 | <p class="description"> |
---|
525 | At the beginning of the post, before the links, this text will be shown.<br /> |
---|
526 | <strong>Use <i>%%day%%</i> for day, on which the post is being generated; <i>%%month%%</i> for the month; <i>%%year%%</i> for the year.</strong> |
---|
527 | </p> |
---|
528 | </td> |
---|
529 | </tr> |
---|
530 | <tr> |
---|
531 | <th>Statements text</th> |
---|
532 | <td> |
---|
533 | <?php |
---|
534 | $args = array( |
---|
535 | 'textarea_name' => 'wppocket_statements_text', |
---|
536 | 'textarea_rows' => 5 |
---|
537 | ); |
---|
538 | wp_editor(get_option('wppocket_statements_text'), 'wppocket_statements_text', $args); |
---|
539 | ?> |
---|
540 | <p class="description"> |
---|
541 | At the end of the post, after the links, this text will be shown.<br /> |
---|
542 | <strong>Use <i>%%day%%</i> for day, on which the post is being generated; <i>%%month%%</i> for the month; <i>%%year%%</i> for the year.</strong> |
---|
543 | </p> |
---|
544 | </td> |
---|
545 | </tr> |
---|
546 | </table> |
---|
547 | |
---|
548 | <input type="hidden" name="wppocket_edit" value="1" /> |
---|
549 | |
---|
550 | <p class="submit"> |
---|
551 | <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
---|
552 | </p> |
---|
553 | </form> |
---|
554 | </div> |
---|
555 | <?php |
---|
556 | } |
---|
557 | |
---|
558 | function options_page_controller() { |
---|
559 | // Check if setting sended |
---|
560 | if ($_POST['wppocket_edit']) { |
---|
561 | // Error Array |
---|
562 | $error = array(); |
---|
563 | |
---|
564 | // Error Text Array |
---|
565 | $errorLang = array( |
---|
566 | '0' => 'Please enter your Pocket username.', |
---|
567 | '1' => 'Please enter your Pocket password.', |
---|
568 | '2' => 'Please enter your Pocket API-Key or reset it to use the default key.', |
---|
569 | '3' => 'Please enter a title for the posts.', |
---|
570 | '4' => 'Please enter a building for each link which will be posted.' |
---|
571 | ); |
---|
572 | |
---|
573 | // Update "Pocket Login" |
---|
574 | |
---|
575 | // Check if Pocket username is isset |
---|
576 | if ($_POST['wppocket_pocket_username'] == '') { |
---|
577 | $error[] = 0; |
---|
578 | }else { |
---|
579 | update_option('wppocket_pocket_username', $this->xcrypt(0, $_POST['wppocket_pocket_username'])); |
---|
580 | } |
---|
581 | |
---|
582 | // Check if Pocket one password is isset |
---|
583 | if (get_option('wppocket_pocket_password') == '' && $_POST['wppocket_pocket_password'] == '') { |
---|
584 | $error[] = 1; |
---|
585 | }elseif ($_POST['wppocket_pocket_password'] != '') { |
---|
586 | update_option('wppocket_pocket_password', $this->xcrypt(0, $_POST['wppocket_pocket_password'])); |
---|
587 | } |
---|
588 | |
---|
589 | if ($_POST['wppocket_pocket_api_key_custom_remove'] == 1) { |
---|
590 | // Set default key |
---|
591 | update_option('wppocket_pocket_api_key_custom', 0); |
---|
592 | update_option('wppocket_pocket_api_key', $this->xcrypt(0, $this->global_pocket_api_key)); |
---|
593 | }elseif(get_option('wppocket_pocket_api_key_custom') == 1 && $_POST['wppocket_pocket_api_key'] == '') { |
---|
594 | $error[] = 2; |
---|
595 | }elseif(get_option('wppocket_pocket_api_key_custom') == 1 && $_POST['wppocket_pocket_api_key'] != '') { |
---|
596 | // Set new key |
---|
597 | update_option('wppocket_pocket_api_key', $this->xcrypt(0, $_POST['wppocket_pocket_api_key'])); |
---|
598 | }elseif (get_option('wppocket_pocket_api_key_custom') == 0 && $_POST['wppocket_pocket_api_key'] != '') { |
---|
599 | // Set custom API-Key first time |
---|
600 | update_option('wppocket_pocket_api_key_custom', 1); |
---|
601 | update_option('wppocket_pocket_api_key', $this->xcrypt(0, $_POST['wppocket_pocket_api_key'])); |
---|
602 | } |
---|
603 | |
---|
604 | // Updates "Data processing" |
---|
605 | if ($_POST['wppocket_links_to_use'] < 0 && $_POST['wppocket_links_to_use'] > 5) { |
---|
606 | $_POST['wppocket_links_to_use'] = 0; |
---|
607 | } |
---|
608 | |
---|
609 | update_option('wppocket_links_to_use', $_POST['wppocket_links_to_use']); |
---|
610 | update_option('wppocket_order', $_POST['wppocket_order']); |
---|
611 | update_option('wppocket_mark_as_readed', $_POST['wppocket_mark_as_readed']); |
---|
612 | update_option('wppocket_clean_url', $_POST['wppocket_clean_url']); |
---|
613 | update_option('wppocket_meta_keyword_as_tags', $_POST['wppocket_meta_keyword_as_tags']); |
---|
614 | update_option('wppocket_reference_wppocket', $_POST['wppocket_reference_wppocket']); |
---|
615 | |
---|
616 | // Updates "Post settings" |
---|
617 | |
---|
618 | // Check if post status is valid, else check default |
---|
619 | if ( |
---|
620 | $_POST['wppocket_post_status'] != 'publish' && |
---|
621 | $_POST['wppocket_post_status'] != 'pending' && |
---|
622 | $_POST['wppocket_post_status'] != 'draft' |
---|
623 | ) { |
---|
624 | $_POST['wppocket_post_status'] = 'draft'; |
---|
625 | } |
---|
626 | |
---|
627 | // Check if post category is valid, else check default |
---|
628 | if ( |
---|
629 | $_POST['wppocket_post_category'] == '' || |
---|
630 | $_POST['wppocket_post_category'] == 0 |
---|
631 | ) { |
---|
632 | $_POST['wppocket_post_category'] = 1; |
---|
633 | } |
---|
634 | |
---|
635 | // Check if comment status is valid, else check default |
---|
636 | if ( |
---|
637 | $_POST['wppocket_comment_status'] != 'open' && |
---|
638 | $_POST['wppocket_comment_status'] != 'close' |
---|
639 | ) { |
---|
640 | $_POST['wppocket_comment_status'] = 'open'; |
---|
641 | } |
---|
642 | |
---|
643 | // Check if comment status is valid, else check default |
---|
644 | if ( |
---|
645 | $_POST['wppocket_ping_status'] != 'open' && |
---|
646 | $_POST['wppocket_ping_status'] != 'close' |
---|
647 | ) { |
---|
648 | $_POST['wppocket_ping_status'] = 'open'; |
---|
649 | } |
---|
650 | |
---|
651 | update_option('wppocket_post_status', $_POST['wppocket_post_status']); |
---|
652 | update_option('wppocket_post_category', $_POST['wppocket_post_category']); |
---|
653 | update_option('wppocket_comment_status', $_POST['wppocket_comment_status']); |
---|
654 | update_option('wppocket_ping_status', $_POST['wppocket_ping_status']); |
---|
655 | |
---|
656 | // Update "Post content matching" |
---|
657 | |
---|
658 | // Check if title is not set |
---|
659 | if ($_POST['wppocket_title'] == '') { |
---|
660 | $error[] = 3; |
---|
661 | }else { |
---|
662 | update_option('wppocket_title', $_POST['wppocket_title']); |
---|
663 | } |
---|
664 | |
---|
665 | // Chech if entry building is not set |
---|
666 | if ($_POST['wppocket_entry_building'] == '') { |
---|
667 | $error[] = 4; |
---|
668 | }else { |
---|
669 | update_option('wppocket_entry_building', $_POST['wppocket_entry_building']); |
---|
670 | } |
---|
671 | |
---|
672 | // Updates without checks |
---|
673 | update_option('wppocket_introduction_text', $_POST['wppocket_introduction_text']); |
---|
674 | update_option('wppocket_statements_text', $_POST['wppocket_statements_text']); |
---|
675 | |
---|
676 | // Output all errors |
---|
677 | $i = 0; |
---|
678 | while ($i < count($error)) { |
---|
679 | ?> |
---|
680 | <div id="message" class="error"> |
---|
681 | <p><strong><?php echo $errorLang[$error[$i]]; ?></strong></p> |
---|
682 | </div> |
---|
683 | <?php |
---|
684 | $i++; |
---|
685 | } |
---|
686 | |
---|
687 | // Check if save without misstakes |
---|
688 | if (count($error) == 0) { |
---|
689 | ?> |
---|
690 | <div id="message" class="updated"> |
---|
691 | <p><strong><?php _e('Settings saved.') ?></strong></p> |
---|
692 | </div> |
---|
693 | <?php |
---|
694 | } |
---|
695 | } |
---|
696 | |
---|
697 | // Check Access |
---|
698 | $this->checkPocketAccess(); |
---|
699 | } |
---|
700 | |
---|
701 | function main_link() { |
---|
702 | // Start the view of the main page |
---|
703 | add_menu_page('WP Pocket', 'WP Pocket', 1, 'pocket_poster', array($this, 'main_page_view'), plugin_dir_url(__FILE__) . 'images/icons/16.png', 25); |
---|
704 | } |
---|
705 | |
---|
706 | function main_page_view() { |
---|
707 | // Add option page style |
---|
708 | wp_enqueue_style('wppocket_main', plugins_url('css/main.css', __FILE__)); |
---|
709 | wp_enqueue_style('wppocket_support', plugins_url('css/support.css', __FILE__)); |
---|
710 | |
---|
711 | // Add JavaScript functions |
---|
712 | wp_enqueue_script('jquery-ui-draggable'); |
---|
713 | wp_enqueue_script('jquery-ui-droppable'); |
---|
714 | wp_enqueue_script('nav-menu'); |
---|
715 | wp_enqueue_script('postbox'); |
---|
716 | wp_enqueue_script('wppocket_main'); |
---|
717 | ?> |
---|
718 | <div class="wrap wppocket"> |
---|
719 | <div id="icon-wppocket" class="icon32"><br></div> |
---|
720 | <h2>WP Pocket</h2> |
---|
721 | |
---|
722 | <?php // Bullshot Snippet for fix navi-menu JavaScript ?> |
---|
723 | <div class="nav-tabs-nav"> |
---|
724 | <div class="nav-tabs-arrow nav-tabs-arrow-left" style="display: none; "><a>«</a></div> |
---|
725 | <div class="nav-tabs-wrapper"> |
---|
726 | <div class="nav-tabs" style="padding: 0px; margin-right: -110px; margin-left: 0px; "> |
---|
727 | <span class="nav-tab nav-tab-active"></span><a href="#" class="nav-tab menu-add-new"></a> |
---|
728 | </div> |
---|
729 | </div> |
---|
730 | </div> |
---|
731 | |
---|
732 | <?php |
---|
733 | if ($_POST['mode'] != 'get') { |
---|
734 | $this->support_div(); |
---|
735 | } |
---|
736 | ?> |
---|
737 | |
---|
738 | <?php $this->main_page_controller(); ?> |
---|
739 | |
---|
740 | <?php |
---|
741 | // Check if should get Pocket elements or save as post |
---|
742 | if ($_POST['mode'] == 'get') { // Get Elements |
---|
743 | // Check Pocket Access |
---|
744 | $checkPocketAccess = $this->checkPocketAccess(); |
---|
745 | |
---|
746 | // Check if access is valid |
---|
747 | if ($checkPocketAccess) { |
---|
748 | // Get links from the user |
---|
749 | |
---|
750 | // Should get Readed, onreaded or all links? |
---|
751 | if (get_option('wppocket_links_to_use') == 0 || get_option('wppocket_links_to_use') == 3) { // Unreaded |
---|
752 | $state = '&state=unread'; |
---|
753 | }elseif (get_option('wppocket_links_to_use') == 1 || get_option('wppocket_links_to_use') == 4) { // Readed |
---|
754 | $state = '&state=read'; |
---|
755 | } // All |
---|
756 | |
---|
757 | $getLinks = $this->cURL('https://readitlaterlist.com/v2/get?username=' . $this->xcrypt(1, get_option('wppocket_pocket_username')) . '&password=' . $this->xcrypt(1, get_option('wppocket_pocket_password')) . '&apikey=' . $this->xcrypt(1, get_option('wppocket_pocket_api_key')) . $state . '&tags=1', 30); |
---|
758 | |
---|
759 | // Decode JSON to Object |
---|
760 | $getLinks = json_decode($getLinks); |
---|
761 | |
---|
762 | // Check if only not posted links should post |
---|
763 | if (get_option('wppocket_links_to_use') == 0 || get_option('wppocket_links_to_use') == 1 || get_option('wppocket_links_to_use') == 2) { |
---|
764 | // Remove all already posted links |
---|
765 | foreach ($getLinks->list as $key => $value) { |
---|
766 | // Check if aleady posted. If true, remove it |
---|
767 | $sql = 'SELECT `id` FROM `' . $this->wpdb->prefix . 'wppocket_posted_links` WHERE `url`="' . $value->url . '"'; |
---|
768 | $row = $this->wpdb->get_results($sql); |
---|
769 | |
---|
770 | if ($row[0]->id != '') { |
---|
771 | unset($getLinks->list->$key); |
---|
772 | } |
---|
773 | } |
---|
774 | } |
---|
775 | |
---|
776 | // Check if order should be oldest to newest |
---|
777 | if (get_option('wppocket_order')) { |
---|
778 | // Turn around the json object |
---|
779 | $tmp = json_encode($getLinks, true); |
---|
780 | $tmp = json_decode($tmp, true); |
---|
781 | $tmp['list'] = array_reverse($tmp['list']); |
---|
782 | $tmp = json_encode($tmp, true); |
---|
783 | $tmp = json_decode($tmp); |
---|
784 | $getLinks = $tmp; |
---|
785 | } |
---|
786 | |
---|
787 | if (count((array)$getLinks->list) == 0) { // No elements |
---|
788 | ?> |
---|
789 | <?php $this->support_div(); ?> |
---|
790 | |
---|
791 | <p><strong>You do not have usable elements in your Pocket account! Is the <i>Links to use</i> setting correct?</strong></p> |
---|
792 | <p>You have to look for interesting links, before you can post them ;-)</p> |
---|
793 | <?php |
---|
794 | }else { |
---|
795 | ?> |
---|
796 | <?php $this->support_div(false); ?> |
---|
797 | |
---|
798 | <p class="wppocket_loader" style="width: 308px; margin: 15px 0 0 0; text-align: center;"> |
---|
799 | <img src="<?php echo plugins_url('images/loading.gif', __FILE__) ?>" alt="loading" /><br /> |
---|
800 | <strong>Please wait while loading...</strong> |
---|
801 | </p> |
---|
802 | |
---|
803 | <form id="update-nav-menu" action="" method="post" enctype="multipart/form-data" style="display: none;"> |
---|
804 | <p><br /><strong>Please edit, sort and associate the links. If you finished, please click on the <i>Generate post</i> button at the end of the list!</strong></p> |
---|
805 | |
---|
806 | <ul class="menu ui-sortable" id="menu-to-edit"> |
---|
807 | <?php |
---|
808 | // Gennerate output of each link |
---|
809 | foreach ($getLinks->list as $key => $value) { |
---|
810 | // Get description |
---|
811 | $urlHtml = $this->cURL($value->url); |
---|
812 | |
---|
813 | $doc = new DOMDocument(); |
---|
814 | @$doc->loadHTML($urlHtml); |
---|
815 | |
---|
816 | if ($value->title == '') { |
---|
817 | $nodes = $doc->getElementsByTagName('title'); |
---|
818 | $value->title = utf8_decode(utf8_encode($nodes->item(0)->nodeValue)); |
---|
819 | } |
---|
820 | |
---|
821 | $metas = $doc->getElementsByTagName('meta'); |
---|
822 | |
---|
823 | for ($i = 0; $i < $metas->length; $i++) { |
---|
824 | $meta = $metas->item($i); |
---|
825 | |
---|
826 | if($meta->getAttribute('name') == 'description') { |
---|
827 | $value->description = utf8_decode(utf8_encode($meta->getAttribute('content'))); |
---|
828 | } |
---|
829 | |
---|
830 | if (get_option('wppocket_meta_keyword_as_tags') == 1) { |
---|
831 | if ($meta->getAttribute('name') == 'keywords') { |
---|
832 | $value->tags = utf8_decode(utf8_encode($meta->getAttribute('content'))); |
---|
833 | } |
---|
834 | } |
---|
835 | } |
---|
836 | ?> |
---|
837 | <li class="menu-item-edit-inactive"> |
---|
838 | <dl class="menu-item-bar"> |
---|
839 | <dt class="menu-item-handle"> |
---|
840 | <div class="left"> |
---|
841 | <?php echo $value->title; ?> |
---|
842 | </div> |
---|
843 | <div class="right"> |
---|
844 | <a class="item-remove" href="#item-remove-<?php echo $key; ?>">Remove element</a> |
---|
845 | <a class="item-edit" href="#item-<?php echo $key; ?>">Edit element</a> |
---|
846 | </div> |
---|
847 | </dt> |
---|
848 | </dl> |
---|
849 | |
---|
850 | <div class="menu-item-settings" id="item-<?php echo $key; ?>"> |
---|
851 | <p class="description description-thin"> |
---|
852 | <label for="item-<?php echo $key; ?>-input-title"> |
---|
853 | Title<br /> |
---|
854 | <input type="text" id="item-<?php echo $key; ?>-input-title" class="widefat input-title" name="element[][title]" value="<?php echo $value->title; ?>" /> |
---|
855 | </label> |
---|
856 | </p> |
---|
857 | <p class="description description-thin"> |
---|
858 | <label for="item-<?php echo $key; ?>-input-tags"> |
---|
859 | Tags (optional)<br /> |
---|
860 | <input type="text" id="item-<?php echo $key; ?>-input-tags" class="widefat" name="element[][tags]" value="<?php echo $value->tags; ?>" /> |
---|
861 | </label> |
---|
862 | </p> |
---|
863 | <p class="description"> |
---|
864 | <label for="item-<?php echo $key; ?>-input-description"> |
---|
865 | Description (optional)<br /> |
---|
866 | <textarea name="element[][description]" rows="4" id="item-<?php echo $key; ?>-input-description" class="widefat" type="textarea"><?php echo $value->description; ?></textarea> |
---|
867 | </label> |
---|
868 | </p> |
---|
869 | <p class="description"> |
---|
870 | <label for="item-<?php echo $key; ?>-input-url"> |
---|
871 | URL<br /> |
---|
872 | <input type="text" id="item-<?php echo $key; ?>-input-url" class="widefat" name="element[][url]" value="<?php if (get_option('wppocket_clean_url') == 1) { echo $this->cleanUpURL($value->url); }else { echo $value->url; } ?>" /> |
---|
873 | <input type="hidden" id="item-<?php echo $key; ?>-input-url" class="widefat" name="element[][url_uncleaned]" value="<?php echo $value->url; ?>" /> |
---|
874 | </label> |
---|
875 | </p> |
---|
876 | |
---|
877 | <input type="hidden" name="element[][id]" value="<?php echo $key; ?>" /> |
---|
878 | <input type="hidden" name="element[][associate]" class="associate" value="0" /> |
---|
879 | </div> |
---|
880 | </li> |
---|
881 | <?php } ?> |
---|
882 | </ul> |
---|
883 | |
---|
884 | <p class="submit"> |
---|
885 | <input type="hidden" name="mode" value="save" /> |
---|
886 | <input type="submit" name="save_menu" class="button-primary" value="Gennerate post" /> |
---|
887 | <strong>This can take up to 30 seconds!</strong> |
---|
888 | </p> |
---|
889 | </form> |
---|
890 | <?php |
---|
891 | } |
---|
892 | } |
---|
893 | }elseif ($_POST['mode'] == 'save') { // Save post |
---|
894 | // Check if elements was posted |
---|
895 | if ($_POST['count_elements'] == 0) { // No elements |
---|
896 | ?> |
---|
897 | <p><strong>No elements to post were found. Is the <i>Links to use</i> setting correct?</strong></strong></p> |
---|
898 | <p>You have to look for interesting links, before you can post them ;-)</p> |
---|
899 | <?php |
---|
900 | }else { // Success |
---|
901 | ?> |
---|
902 | <p class="short"><strong>Great! You posted <?php echo $_POST['count_elements']; ?> links from your Pocket account into your blog. <?php if ($_POST['wppocket_mark_as_readed']) { ?>[18:56:01] Elias: They are now marked as read in your pocket account!<?php } ?></strong></p> |
---|
903 | <p class="short">If you like WP Pocket, I would be pleased, if you supported me. How to? Look in the yellow box on the right!</p> |
---|
904 | <?php |
---|
905 | } |
---|
906 | }else { // Print introduction |
---|
907 | ?> |
---|
908 | <br /><br /> |
---|
909 | <p class="short">You would like to generate a new post with links from you pocket account? Great!</p> |
---|
910 | |
---|
911 | <p class="short">After you click on <i>Get links</i> you will see all links which will be posted. If you would like to |
---|
912 | associate to links, take the link which should be shown without title, description and tags in the second level and the main link.<br /> |
---|
913 | You will be able to edit all titles, descriptions, links and tags or remove them. If you finished, you can generate the post.<?php if (get_option('wppocket_mark_as_readed') == 1) { ?> Only if you click on <i>Generate post</i>, the links will be marked as read in your Pocket account.<?php } ?></p> |
---|
914 | |
---|
915 | <?php |
---|
916 | // Check Pocket Access |
---|
917 | $checkPocketAccess = $this->checkPocketAccess(); |
---|
918 | ?> |
---|
919 | �� |
---|
920 | <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> |
---|
921 | <p class="submit"> |
---|
922 | <input type="hidden" name="mode" value="get" /> |
---|
923 | <input type="submit" class="button-primary" value="Get links"<?php if($checkPocketAccess == false) { echo ' disabled'; } ?> /> |
---|
924 | <strong>This can take up to 60 seconds!</strong> |
---|
925 | </p> |
---|
926 | </form> |
---|
927 | <?php |
---|
928 | } |
---|
929 | ?> |
---|
930 | </div> |
---|
931 | <?php |
---|
932 | } |
---|
933 | |
---|
934 | function main_page_controller() { |
---|
935 | global $current_user; |
---|
936 | |
---|
937 | // Check if the post sould save |
---|
938 | if ($_POST['mode'] == 'save') { // Save post |
---|
939 | ######################## |
---|
940 | ## SET ENTRY BUILDING ## |
---|
941 | ######################## |
---|
942 | |
---|
943 | // Set en empty example for entry building with replace |
---|
944 | $replaceEntryBuilding = $this->replaceEntryBuilding(false, false, false, false); |
---|
945 | |
---|
946 | // Set en empty example for entry building without replace |
---|
947 | $noReplaceEntryBuilding = $this->replaceEntryBuilding(false, false, false, false, false); |
---|
948 | |
---|
949 | // Check, which Array elements are dynamic. Set empy replace and flag in new array |
---|
950 | $i = 0; |
---|
951 | $exampleReplaceEntryBuilding = array(); |
---|
952 | while($i < count($replaceEntryBuilding)) { |
---|
953 | if ($replaceEntryBuilding[$i] == $noReplaceEntryBuilding[$i]) { |
---|
954 | $exampleReplaceEntryBuilding[$i] = array( |
---|
955 | 'example' => $replaceEntryBuilding[$i], |
---|
956 | 'dynamic' => false |
---|
957 | ); |
---|
958 | }else { |
---|
959 | $exampleReplaceEntryBuilding[$i] = array( |
---|
960 | 'example' => $replaceEntryBuilding[$i], |
---|
961 | 'dynamic' => true |
---|
962 | ); |
---|
963 | } |
---|
964 | |
---|
965 | $i++; |
---|
966 | } |
---|
967 | |
---|
968 | // Unset not longer needed vars |
---|
969 | unset($replaceEntryBuilding); |
---|
970 | unset($noReplaceEntryBuilding); |
---|
971 | |
---|
972 | ######################### |
---|
973 | ## SET ASSOCIATE LINKS ## |
---|
974 | ######################### |
---|
975 | |
---|
976 | $i = 0; |
---|
977 | while ($i < (count($_POST['element']) / 7)) { |
---|
978 | // Check if this it not a associate link |
---|
979 | if (!$_POST['element'][($i * 7) + 5]['associate']) { |
---|
980 | // Search for associate links in the following |
---|
981 | $associateLinks = array(); |
---|
982 | $i2 = 1; |
---|
983 | while(true) { |
---|
984 | // If this is an associate links, add this to the array, else break the while-loop |
---|
985 | if ($_POST['element'][$i + 5 + ($i2 * 7)]['associate']) { |
---|
986 | $_POST['element'][($i * 7) + 3][] = $_POST['element'][(($i * 7) + 3 + ($i2 * 7))]['url']; |
---|
987 | $_POST['element'][($i * 7) + 4][] = $_POST['element'][(($i * 7) + 4 + ($i2 * 7))]['url_uncleaned']; |
---|
988 | }else { |
---|
989 | break; |
---|
990 | } |
---|
991 | |
---|
992 | $i2++; |
---|
993 | } |
---|
994 | } |
---|
995 | |
---|
996 | $i++; |
---|
997 | } |
---|
998 | |
---|
999 | #################### |
---|
1000 | ## GENNERATE POST ## |
---|
1001 | #################### |
---|
1002 | |
---|
1003 | // Set posted elements in $_POST for view |
---|
1004 | $_POST['count_elements'] = round((count($_POST['element']) / 7)); |
---|
1005 | |
---|
1006 | // Array for all posted links |
---|
1007 | $allPostedLinks = array(); |
---|
1008 | |
---|
1009 | $post_content = ''; |
---|
1010 | |
---|
1011 | // Set introduction text if not empty |
---|
1012 | if (trim(get_option('wppocket_introduction_text')) != '') { |
---|
1013 | $post_content .= $this->getReplacedIntroductionText(); |
---|
1014 | } |
---|
1015 | |
---|
1016 | // Gennerate all links intro text |
---|
1017 | $i = 0; |
---|
1018 | while ($i < (count($_POST['element']) / 7)) { |
---|
1019 | // Check if only not posted links should post |
---|
1020 | if (get_option('wppocket_links_to_use') == 0 || get_option('wppocket_links_to_use') == 1 || get_option('wppocket_links_to_use') == 2) { |
---|
1021 | // Check if aleady posted. If true, confine to the next loop |
---|
1022 | $sql = 'SELECT `id` FROM `' . $this->wpdb->prefix . 'wppocket_posted_links` WHERE `url`="' . $_POST['element'][($i * 7) + 3]['url'] . '"'; |
---|
1023 | $row = $this->wpdb->get_results($sql); |
---|
1024 | |
---|
1025 | if ($row[0]->id != '') { |
---|
1026 | $_POST['count_elements'] -= count($_POST['element'][($i * 7) + 3]); // - this and associated elements |
---|
1027 | $i++; |
---|
1028 | continue; |
---|
1029 | } |
---|
1030 | } |
---|
1031 | |
---|
1032 | // Check if this it not a associate link |
---|
1033 | if (!$_POST['element'][($i * 7) + 5]['associate']) { |
---|
1034 | // Gennerate url array in url string |
---|
1035 | $url = ''; |
---|
1036 | |
---|
1037 | $i2 = 0; |
---|
1038 | foreach ($_POST['element'][($i * 7) + 3] as $value) { |
---|
1039 | if ($i2 != 0) { |
---|
1040 | $url .= '; '; |
---|
1041 | } |
---|
1042 | |
---|
1043 | $url .= '<a href="' . $value . '" target="_blank">' . $value . '</a>'; |
---|
1044 | |
---|
1045 | $i2++; |
---|
1046 | } |
---|
1047 | |
---|
1048 | // Fill array for all posted links |
---|
1049 | foreach ($_POST['element'][($i * 7) + 4] as $value) { |
---|
1050 | $allPostedLinks[] = $value; |
---|
1051 | } |
---|
1052 | |
---|
1053 | // Set element in entry building |
---|
1054 | $element = $this->replaceEntryBuilding($_POST['element'][($i * 7)]['title'], $_POST['element'][($i * 7) + 2]['description'], $url, $_POST['element'][($i * 7) + 1]['tags']); |
---|
1055 | |
---|
1056 | // Check if a dynamic element is not in use an delete it |
---|
1057 | $i2 = 0; |
---|
1058 | while ($i2 < count($element)) { |
---|
1059 | if ($element[$i2] == $exampleReplaceEntryBuilding[$i2]['example'] && $exampleReplaceEntryBuilding[$i2]['dynamic']) { |
---|
1060 | unset($element[$i2]); |
---|
1061 | } |
---|
1062 | |
---|
1063 | $i2++; |
---|
1064 | } |
---|
1065 | |
---|
1066 | // Set element array as string for the post content |
---|
1067 | $post_content .= '<p>'; |
---|
1068 | |
---|
1069 | foreach ($element as $value) { |
---|
1070 | $post_content .= $value; |
---|
1071 | } |
---|
1072 | |
---|
1073 | $post_content .= '</p>'; |
---|
1074 | } |
---|
1075 | |
---|
1076 | $i++; |
---|
1077 | } |
---|
1078 | |
---|
1079 | // Set statements text if not empty |
---|
1080 | if (trim(get_option('wppocket_statements_text')) != '') { |
---|
1081 | $post_content .= $this->getReplacedStatementsText(); |
---|
1082 | } |
---|
1083 | |
---|
1084 | // Chech if reference to WP Pocket shot print |
---|
1085 | if (get_option('wppocket_reference_wppocket')) { |
---|
1086 | $post_content .= '<p><i>This post was automatically created by WP Pocket. If you look for this WordPress plugin, check out <a href="http://wp-pocket.com/" target="_blank">WP-Pocket.com</a> now!</i></p>'; |
---|
1087 | } |
---|
1088 | |
---|
1089 | ############### |
---|
1090 | ## SAVE POST ## |
---|
1091 | ############### |
---|
1092 | |
---|
1093 | // Save only, if one or more elements are valid |
---|
1094 | if ($_POST['count_elements'] != 0) { |
---|
1095 | // Set settings array |
---|
1096 | $post = array( |
---|
1097 | 'comment_status' => get_option('wppocket_comment_status'), |
---|
1098 | 'ping_status' => get_option('wppocket_ping_status'), |
---|
1099 | 'post_author' => $current_user->ID, |
---|
1100 | 'post_category' => array(get_option('wppocket_post_category')), |
---|
1101 | 'post_content' => $post_content, |
---|
1102 | 'post_date' => date('Y-m-d H:i:s'), |
---|
1103 | 'post_date_gmt' => gmdate('Y-m-d H:i:s'), |
---|
1104 | 'post_status' => get_option('wppocket_post_status'), |
---|
1105 | 'post_title' => $this->getReplacedTitle($_POST['count_elements']), |
---|
1106 | 'post_type' => 'post' |
---|
1107 | ); |
---|
1108 | |
---|
1109 | // Insert the post |
---|
1110 | wp_insert_post($post); |
---|
1111 | } |
---|
1112 | |
---|
1113 | ########################## |
---|
1114 | ## SAVE LINKS AS POSTED ## |
---|
1115 | ########################## |
---|
1116 | |
---|
1117 | // Save only, if one or more elements are valid |
---|
1118 | if ($_POST['count_elements'] != 0) { |
---|
1119 | // Set currently time |
---|
1120 | $time = time(); |
---|
1121 | |
---|
1122 | $i = 0; |
---|
1123 | while ($i < count($allPostedLinks)) { |
---|
1124 | $sql = 'SELECT `id` FROM `' . $this->wpdb->prefix . 'wppocket_posted_links` WHERE `url`="' . $allPostedLinks[$i] . '"'; |
---|
1125 | $row = $this->wpdb->get_results($sql); |
---|
1126 | |
---|
1127 | // Save only it already not saved |
---|
1128 | if ($row[0]->id == '') { |
---|
1129 | $sql = ' |
---|
1130 | INSERT IGNORE INTO `' . $this->wpdb->prefix . 'wppocket_posted_links` ( |
---|
1131 | `url`, |
---|
1132 | `timestamp` |
---|
1133 | ) |
---|
1134 | VALUES |
---|
1135 | (%s, %s) |
---|
1136 | '; |
---|
1137 | |
---|
1138 | // Save link as posted |
---|
1139 | $this->wpdb->query( |
---|
1140 | $this->wpdb->prepare( |
---|
1141 | $sql, |
---|
1142 | $allPostedLinks[$i], |
---|
1143 | $time |
---|
1144 | ) |
---|
1145 | ); |
---|
1146 | } |
---|
1147 | |
---|
1148 | $i++; |
---|
1149 | } |
---|
1150 | } |
---|
1151 | |
---|
1152 | ################################# |
---|
1153 | ## SET STATUS READED IN POCKET ## |
---|
1154 | ################################# |
---|
1155 | |
---|
1156 | // Check if link should marke as readed |
---|
1157 | if (get_option('wppocket_mark_as_readed')) { |
---|
1158 | // JSON Start |
---|
1159 | $markAsReaded = '{'; |
---|
1160 | |
---|
1161 | // Fill JSON |
---|
1162 | $i = 0; |
---|
1163 | while ($i < count($allPostedLinks)) { |
---|
1164 | if ($i != 0) { |
---|
1165 | $markAsReaded .= ','; |
---|
1166 | } |
---|
1167 | |
---|
1168 | $markAsReaded .= '"' . $i . '":{"url":"' . $allPostedLinks[$i] . '"}'; |
---|
1169 | |
---|
1170 | $i++; |
---|
1171 | } |
---|
1172 | |
---|
1173 | // JSON End |
---|
1174 | $markAsReaded .= '}'; |
---|
1175 | |
---|
1176 | // Send request |
---|
1177 | $responses = $this->cURL('https://readitlaterlist.com/v2/send?username=' . $this->xcrypt(1, get_option('wppocket_pocket_username')) . '&password=' . $this->xcrypt(1, get_option('wppocket_pocket_password')) . '&apikey=' . $this->xcrypt(1, get_option('wppocket_pocket_api_key')) . '&read=' . urlencode($markAsReaded), 10); |
---|
1178 | } |
---|
1179 | } |
---|
1180 | } |
---|
1181 | } |
---|
1182 | } |
---|
1183 | |
---|
1184 | // Init the plugin in wordpress |
---|
1185 | if(class_exists('wppocketer')) { |
---|
1186 | $wppocketer = new wppocketer(); |
---|
1187 | } |
---|
1188 | |
---|
1189 | if(isset($wppocketer)) { |
---|
1190 | add_action('init', array(&$wppocketer, 'activate')); |
---|
1191 | } |
---|
1192 | ?> |
---|