class-steempress_sp-admin.php 28 KB
Newer Older
Martin Lees's avatar
Martin Lees committed
1
<?php
Martin Lees's avatar
Martin Lees committed
2

Martin Lees's avatar
Martin Lees committed
3
4
5
6
7
8
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://steemit.com/@howo
 * @since      1.0.0
 *
Martin Lees's avatar
Martin Lees committed
9
10
 * @package    Steempress_sp
 * @subpackage Steempress_sp/admin
Martin Lees's avatar
Martin Lees committed
11
12
13
14
15
16
17
18
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
Martin Lees's avatar
Martin Lees committed
19
20
 * @package    Steempress_sp
 * @subpackage Steempress_sp/admin
Martin Lees's avatar
Martin Lees committed
21
 */
Martin Lees's avatar
Martin Lees committed
22
23
24
25
26

require('partials/domlettersiterator.php');
require('partials/DOMWordsIterator.php');
require('partials/TruncateHTML.php');

Martin Lees's avatar
Martin Lees committed
27
class Steempress_sp_Admin {
Martin Lees's avatar
Martin Lees committed
28
29
30
31
32
33
34
35
36
37
38
39

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
40
41
42
43
44
45
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $version    The current version of this plugin.
     */
    private $version;
Martin Lees's avatar
Martin Lees committed
46

47
48
49
50
51
52
53
54
    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string    $plugin_name       The name of this plugin.
     * @param      string    $version    The version of this plugin.
     */
    public function __construct( $plugin_name, $version ) {
Martin Lees's avatar
Martin Lees committed
55

56
57
58
        $this->plugin_name = $plugin_name;
        $this->version = $version;
    }
Martin Lees's avatar
Martin Lees committed
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    /**
     * Register the stylesheets for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_styles() {

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Steempress_sp_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Steempress_sp_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/steempress_sp-admin.css', array(), $this->version, 'all' );
Martin Lees's avatar
Martin Lees committed
80

81
    }
Martin Lees's avatar
Martin Lees committed
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    /**
     * Register the JavaScript for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts() {

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Steempress_sp_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Steempress_sp_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/steempress_sp-admin.js', array( 'jquery' ), $this->version, false );
Martin Lees's avatar
Martin Lees committed
103

104
    }
Martin Lees's avatar
Martin Lees committed
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

    /**
     * Register the administration menu for this plugin into the WordPress Dashboard menu.
     *
     * @since    1.0.0
     */

    public function add_plugin_admin_menu() {
        add_options_page( 'SteemPress Options', 'SteemPress', 'manage_options', $this->plugin_name, array($this, 'display_plugin_setup_page')
        );
    }

    /**
     * Add settings action link to the plugins page.
     *
     * @since    1.0.0
     */

    public function add_action_links( $links ) {
        /*
        *  Documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
        */
        $settings_link = array(
            '<a href="' . admin_url( 'options-general.php?page=' . $this->plugin_name ) . '">' . __('Settings', $this->plugin_name) . '</a>',
        );
        return array_merge(  $settings_link, $links );

    }

    /**
     * Render the settings page for this plugin.
     *
     * @since    1.0.0
     */

    public function display_plugin_setup_page() {
Martin Lees's avatar
Martin Lees committed
141
        include_once('partials/steempress_sp-admin-display.php');
Martin Lees's avatar
Martin Lees committed
142
143
144
145
146
147
148
    }

    public function validate($input) {
        // All checkboxes inputs
        $valid = array();
        $valid['reward'] = (isset($input['reward']) && !empty($input['reward'] ) && ($input['reward'] == "50" || $input['reward'] == "100")) ? $input['reward'] : "50";
        $valid['posting-key'] = (isset($input['posting-key']) && !empty($input['posting-key'])) ? htmlspecialchars($input['posting-key'], ENT_QUOTES) : "";
149
        $valid['tags'] = (isset($input['tags']) && !empty($input['tags'])) ? htmlspecialchars($input['tags'], ENT_QUOTES) : "";
Martin Lees's avatar
Martin Lees committed
150
        $valid['username'] = (isset($input['username']) && !empty($input['username'])) ? htmlspecialchars($input['username'], ENT_QUOTES) : "";
151
152
        $valid['seo'] = ((isset($input['seo']) && !empty($input['seo'])) && $input['seo'] == 'on') ? 'on' : "off";
        $valid['vote'] = ((isset($input['vote']) && !empty($input['vote'])) && $input['vote'] == 'on') ? 'on' : "off";
153
        $valid['append'] = ((isset($input['append']) && !empty($input['append'])) && $input['append'] == 'on') ? 'on' : "off";
154
        $valid['delay'] = ((isset($input['delay']) && !empty($input['delay']) && is_numeric($input['delay']) && $input['delay'] >= 0 && $input['delay'] <= 87600)) ?  htmlspecialchars($input['delay'], ENT_QUOTES) : "0";
155
        $valid['featured'] = ((isset($input['featured']) && !empty($input['featured'])) && $input['featured'] == 'on') ? 'on' : "off";
156
        $valid['footer'] = (isset($input['footer']) && !empty($input['footer'])) ? $input['footer'] : "<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : [%original_link%] </em><hr/></center>";
157
        $valid['twoway'] = ((isset($input['twoway']) && !empty($input['twoway'])) && $input['twoway'] == 'on') ? 'on' : "off";
158
        $valid['twoway-front'] = ((isset($input['twoway-front']) && !empty($input['twoway-front'])) && $input['twoway-front'] == 'on') ? 'on' : "off";
159
        $valid['update'] = ((isset($input['update']) && !empty($input['update'])) && $input['update'] == 'on') ? 'on' : "off";
Martin Lees's avatar
Martin Lees committed
160
        $valid['wordlimit'] = ((isset($input['wordlimit']) && !empty($input['wordlimit']) && is_numeric($input['wordlimit']) && $input['wordlimit'] >= 0)) ?  htmlspecialchars($input['wordlimit'], ENT_QUOTES) : "0";
Martin Lees's avatar
Martin Lees committed
161

162
163
164
165
166
167
168
        $users = get_users();

        for ($i = 0; $i < sizeof($users); $i++)
        {
            $valid['posting-key'.$users[$i]->data->ID] = (isset($input['posting-key'.$users[$i]->data->ID]) && !empty($input['posting-key'.$users[$i]->data->ID])) ? htmlspecialchars($input['posting-key'.$users[$i]->data->ID], ENT_QUOTES) : "";
            $valid['username'.$users[$i]->data->ID] = (isset($input['username'.$users[$i]->data->ID]) && !empty($input['username'.$users[$i]->data->ID])) ? htmlspecialchars($input['username'.$users[$i]->data->ID], ENT_QUOTES) : "";
        }
Martin Lees's avatar
Martin Lees committed
169

Martin Lees's avatar
Martin Lees committed
170
171
172
173
174
175
176
177
        $categories = get_categories(array('hide_empty' => FALSE));

        for ($i = 0; $i < sizeof($categories); $i++)
        {
            $valid['cat'.$categories[$i]->cat_ID] = ((isset($input['cat'.$categories[$i]->cat_ID]) && !empty($input['cat'.$categories[$i]->cat_ID])) && $input['cat'.$categories[$i]->cat_ID] == 'on') ? 'on' : "off";
        }


Martin Lees's avatar
Martin Lees committed
178
179
180
181
182
183
184
185
        return $valid;
    }

    public function options_update() {
        register_setting($this->plugin_name, $this->plugin_name, array($this, 'validate'));
    }


Martin Lees's avatar
Martin Lees committed
186
187


Martin Lees's avatar
Martin Lees committed
188
189

    public function Steempress_sp_publish($id)
Martin Lees's avatar
Martin Lees committed
190
    {
Martin Lees's avatar
Martin Lees committed
191

Martin Lees's avatar
Martin Lees committed
192
        $options = get_option($this->plugin_name);
Martin Lees's avatar
Martin Lees committed
193

Martin Lees's avatar
Martin Lees committed
194
195
196
197
198
199
200
201
202
203
204
205
206
        // Avoid undefined errors
        if (!isset($options["username"]))
            $options["username"] = "";
        if (!isset($options["posting-key"]))
            $options["posting-key"] = "";
        if (!isset($options["reward"]))
            $options["reward"] = "100";
        if (!isset($options["tags"]))
            $options["tags"] = "";
        if (!isset($options["seo"]))
            $options["seo"] = "on";
        if (!isset($options["vote"]))
            $options["vote"] = "on";
207
208
        if (!isset($options["append"]))
            $options["append"] = "off";
Martin Lees's avatar
Martin Lees committed
209
210
        if (!isset($options["delay"]))
            $options["delay"] = "0";
211
212
        if (!isset($options["featured"]))
            $options["featured"] = "on";
Martin Lees's avatar
Martin Lees committed
213
214
        if (!isset($options["footer"]))
            $options["footer"] = "<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : [%original_link%] </em><hr/></center>";
Martin Lees's avatar
Martin Lees committed
215
216
217
218
        if (!isset($options["wordlimit"]))
            $options["wordlimit"] = "0";


Martin Lees's avatar
Martin Lees committed
219
        $post = get_post($id);
Martin Lees's avatar
Martin Lees committed
220

Martin Lees's avatar
Martin Lees committed
221
222
223
224
225
226
227
228
229
230

        $categories = get_the_category($id);

        for($i = 0; $i < sizeof($categories); $i++)
        {
            if (isset($options['cat'.$categories[$i]->cat_ID]) && $options['cat'.$categories[$i]->cat_ID] == "on")
                return;
        }


Martin Lees's avatar
Martin Lees committed
231
232
233
234
235
236
237
238
239
240
241
        $author_id = $post->post_author;

        $username = $options["username"];
        $posting_key = $options["posting-key"];

        if (isset($options['username'.$author_id]) && $options['username'.$author_id] != "" && isset($options['posting-key'.$author_id]) && $options['posting-key'.$author_id] != "")
        {
            $username = $options['username'.$author_id];
            $posting_key = $options['posting-key'.$author_id];
        }

Martin Lees's avatar
Martin Lees committed
242
        $wp_tags = wp_get_post_tags($id);
Martin Lees's avatar
Martin Lees committed
243

Martin Lees's avatar
Martin Lees committed
244
        if (sizeof($wp_tags) != 0) {
Martin Lees's avatar
Martin Lees committed
245

Martin Lees's avatar
Martin Lees committed
246
            $tags = array();
Martin Lees's avatar
Martin Lees committed
247

Martin Lees's avatar
Martin Lees committed
248
            foreach ($wp_tags as $tag) {
Martin Lees's avatar
Martin Lees committed
249
                $tags[] = str_replace(" ", "", $tag->name);
Martin Lees's avatar
Martin Lees committed
250
251
            }

Martin Lees's avatar
Martin Lees committed
252
            $tags = implode(" ", $tags);
253
254
255

            if ($options["append"] == "on")
                $tags = $options["tags"]." ".$tags;
Martin Lees's avatar
Martin Lees committed
256
257
258
        }
        else
            $tags = $options["tags"];
Martin LEES's avatar
Martin LEES committed
259
        $link = get_permalink($post->ID);
Martin Lees's avatar
Martin Lees committed
260

Martin Lees's avatar
Martin Lees committed
261
        if ($options['seo'] == "on")
Martin LEES's avatar
Martin LEES committed
262
            $display_backlink = "true";
Martin Lees's avatar
Martin Lees committed
263
        else
Martin LEES's avatar
Martin LEES committed
264
            $display_backlink = "false";
Martin Lees's avatar
Martin Lees committed
265

266
267
268
269
270
271
        $content = $post->post_content;
        if ($options["featured"] == "on") {
            $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($id), 'thumbnail');
            if ($thumbnail != "0")
                $content = "<center>" . $thumbnail . "</center> <br/>" . $post->post_content;
        }
272

Martin Lees's avatar
Martin Lees committed
273
274
        $domain = get_site_url();

Martin Lees's avatar
Martin Lees committed
275
276
277
278
279
280
281
282
283
284
        $version = steempress_sp_compte;

        $pos = strrpos(steempress_sp_compte, ".");

        if($pos !== false)
            $version = substr_replace(steempress_sp_compte, "", $pos, strlen("."));

        $version = ((float)$version)*100;


Martin Lees's avatar
Martin Lees committed
285
286
287
288
        if ($options['wordlimit'] != "0") {
            $limit = intval($options["wordlimit"]);
            $content = TruncateHTML::truncateWords($content, $limit, '');
        }
Martin Lees's avatar
Martin Lees committed
289

Martin Lees's avatar
Martin Lees committed
290
        $data = array("body" => array(
291
292
293
294
295
296
297
298
299
300
301
302
303
304
            "title" => $post->post_title,
            "content" => $content,
            "tags" => $tags,
            "author" => $username,
            "wif" => $posting_key,
            "original_link" => $link,
            "reward" => $options['reward'],
            "vote"=> $options["vote"],
            "delay"=> $options["delay"],
            "wordpress_id"=> $id,
            "domain"=> $domain,
            "display_backlink" => $display_backlink,
            "version" =>  $version,
            "footer" =>$options['footer'],
Martin Lees's avatar
Martin Lees committed
305
        ));
306

Martin Lees's avatar
Martin Lees committed
307
308
309
310
311
312
        // A few local verifications as to not overload the server with useless txs

        $test = $data['body'];
        // Last minute checks before sending it to the server
        if ($test['tags'] != "" && $test['author'] != "" && $test['wif'] != "") {
            // Post to the api who will publish it on the steem blockchain.
Martin Lees's avatar
Martin Lees committed
313
            $result = wp_remote_post(steempress_sp_api_url, $data);
Martin Lees's avatar
Martin Lees committed
314
            if (!isset($result->errors)) {
Martin Lees's avatar
Martin Lees committed
315
316
                update_post_meta($id,'steempress_sp_permlink',$result['body']);
                update_post_meta($id,'steempress_sp_author',$username);
Martin Lees's avatar
Martin Lees committed
317
            }
Martin Lees's avatar
Martin Lees committed
318
319
320
        }
    }

321
    public function steempress_sp_bulk_update_action($bulk_actions) {
322
323
324
325
326
327
328
329
        $options = get_option($this->plugin_name);

        if (!isset($options["update"]))
            $options["update"] = "on";

        if ($options["update"] == "on")
            $bulk_actions['update_to_steem'] = __('Update to STEEM', 'update_to_steem');

330
331
332
333
        return $bulk_actions;
    }

    public function steempress_sp_bulk_publish_action($bulk_actions) {
Martin Lees's avatar
Martin Lees committed
334
        $bulk_actions['publish_to_steem'] = __( 'Publish to STEEM', 'publish_to_steem');
Martin Lees's avatar
Martin Lees committed
335
336
337
        return $bulk_actions;
    }

338
339

    public function steempress_sp_bulk_publish_handler( $redirect_to, $doaction, $post_ids ) {
Martin Lees's avatar
Martin Lees committed
340
341
342
        if ( $doaction !== 'publish_to_steem' ) {
            return $redirect_to;
        }
343
        for ($i = sizeof($post_ids)-1; $i >= 0; $i--) {
Martin Lees's avatar
Martin Lees committed
344
            // Perform action for each post.
345
            $this->Steempress_sp_publish($post_ids[$i]);
Martin Lees's avatar
Martin Lees committed
346
        }
347
348
349
350
351
352
353
354
        $redirect_to = add_query_arg('published_to_steem', count( $post_ids ), $redirect_to );
        return $redirect_to;
    }

    public function steempress_sp_bulk_update_handler( $redirect_to, $doaction, $post_ids ) {
        if ( $doaction !== 'update_to_steem' ) {
            return $redirect_to;
        }
Martin Lees's avatar
Martin Lees committed
355
356
357

        $updated = 0;

358
359
        for ($i = sizeof($post_ids)-1; $i >= 0; $i--) {
            // Perform action for each post.
Martin Lees's avatar
Martin Lees committed
360
361
            if ($this->steempress_sp_update($post_ids[$i], true) == 1)
                $updated++;
362
        }
Martin Lees's avatar
Martin Lees committed
363

Martin Lees's avatar
Martin Lees committed
364
365
366
367
        if ($updated != count($post_ids))
            $redirect_to = add_query_arg('updated_to_steem_err', $updated, $redirect_to );
        else
            $redirect_to = add_query_arg('updated_to_steem', $updated, $redirect_to );
Martin Lees's avatar
Martin Lees committed
368
369
370
        return $redirect_to;
    }

371
    public function steempress_sp_bulk_update_notice() {
372
        if ( !empty( $_REQUEST['updated_to_steem']) ) {
373
374
375
376
377
378
379
            $published_count = intval( $_REQUEST['updated_to_steem'] );
            printf( '<div id="message" class="updated fade">' .
                _n( 'Added %s post to be updated on STEEM. Check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
                    'Added %s posts to be updated on STEEM. Check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
                    $published_count,
                    'updated_to_steem'
                ) . '</div>', $published_count );
Martin Lees's avatar
Martin Lees committed
380
381
382
        }

        if (!empty($_REQUEST['updated_to_steem_err']))
Martin Lees's avatar
Martin Lees committed
383
384
385
386
387
388
389
390
        {
            $published_count = intval( $_REQUEST['updated_to_steem_err'] );
            printf( '<div id="message" class="updated fade">' .
                _n( 'Your post was not updated probably because the metadata was not correctly set. Please edit the article you wanted to update on STEEM and edit the metadata. Then resubmit it.',
                    'Added %s posts to be updated on STEEM. Some were not updated probably because the metadata was not correctly set. Please edit the articles you want to update to STEEM and edit the metadata. Then resubmit them.',
                    $published_count,
                    'updated_to_steem'
                ) . '</div>', $published_count );
391
392
393
        }
    }

Martin Lees's avatar
Martin Lees committed
394
395
396
397
398
399
400
401
402
403
404
405
406
    public function steempress_sp_bulk_publish_notice() {
        if ( !empty($_REQUEST['published_to_steem'])) {
            $published_count = intval( $_REQUEST['published_to_steem'] );
            printf( '<div id="message" class="updated fade">' .
                _n( 'Added %s post to be published on STEEM. STEEM only allows one article to be published per 5 minutes so it may take a while. Check your posting queue on <a href="https://steempress.io">https://steempress.io</a>  to track the progress.',
                    'Added %s posts to be published on STEEM. STEEM only allows one article to be published per 5 minutes so it may take a while. check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
                    $published_count,
                    'published_to_steem'
                ) . '</div>', $published_count );
        }
    }


407
408
409

    public function Steempress_sp_post($new_status, $old_status, $post)
    {
Martin Lees's avatar
Martin Lees committed
410
411
        // New post
        if ($new_status == 'publish' &&  $old_status != 'publish' && $post->post_type == 'post') {
412
            if (!isset($_POST['Steempress_sp_steem_publish']) && isset($_POST['Steempress_sp_steem_do_not_publish']) )
413
                return;
414
415
416
417
418

            $value = get_post_meta($post->ID, 'Steempress_sp_steem_publish', true);
            if ($value != "0")
                $this->Steempress_sp_publish($post->ID);

Martin Lees's avatar
Martin Lees committed
419
            // Edited post
Martin Lees's avatar
Martin Lees committed
420
        } else if ($new_status == 'publish' &&  $old_status == 'publish' && $post->post_type == 'post') {
421
422
            if (!isset($_POST['Steempress_sp_steem_update']) && isset($_POST['Steempress_sp_steem_do_not_update']) )
                return;
Martin Lees's avatar
Martin Lees committed
423
            $this->steempress_sp_update($post->ID);
Martin Lees's avatar
Martin Lees committed
424
        }
Martin Lees's avatar
Martin Lees committed
425
            return;
426
427
    }

Martin Lees's avatar
Martin Lees committed
428
429
430



Martin Lees's avatar
Martin Lees committed
431
432
433
434
435
436
437
438
439
440
441
    function createSteemPublishField()
    {
        $post_id = get_the_ID();

        if (get_post_type($post_id) != 'post') {
            return;
        }

        if (get_post_status ($post_id) == 'publish')
            return;

442
443
444
445
446
447
448
449
        wp_nonce_field('Steempress_sp_custom_nonce_'.$post_id, 'Steempress_sp_custom_nonce');

        $value = get_post_meta($post_id, 'Steempress_sp_steem_publish', true);
        if ($value == "0")
            $checked = "";
        else
            $checked = "checked";

Martin Lees's avatar
Martin Lees committed
450
451
        ?>
        <div class="misc-pub-section misc-pub-section-last">
452
            <label><input type="checkbox" value="1" <?php echo $checked; ?> name="Steempress_sp_steem_publish" /> <input type="hidden" name="Steempress_sp_steem_do_not_publish" value="0" />Publish to steem </label>
Martin Lees's avatar
Martin Lees committed
453
454
455
456
        </div>
        <?php
    }

457
    function create_update_checkbox()
Martin Lees's avatar
Martin Lees committed
458
    {
459
460
461
        $post_id = get_the_ID();

        if (get_post_type($post_id) != 'post') {
Martin Lees's avatar
Martin Lees committed
462
463
464
            return;
        }

465
        if (get_post_status ($post_id) != 'publish')
Martin Lees's avatar
Martin Lees committed
466
467
            return;

468
469
470
471
472
473
474
475
        $options = get_option($this->plugin_name);

        if (!isset($options["update"]))
            $options["update"] = "on";

        if ($options["update"] != "on")
            return;

476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
        wp_nonce_field('Steempress_sp_custom_update_nonce_'.$post_id, 'Steempress_sp_custom_update_nonce');

        $value = get_post_meta($post_id, 'Steempress_sp_steem_update', true);
        if ($value == "0")
            $checked = "";
        else
            $checked = "checked";

        ?>
        <div class="misc-pub-section misc-pub-section-last">
            <label><input type="checkbox" value="1" <?php echo $checked; ?> name="Steempress_sp_steem_update" /> <input type="hidden" name="Steempress_sp_steem_do_not_update" value="0" />Update to steem </label>
        </div>
        <?php
    }

    function saveSteemPublishField($post_id)
    {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
Martin Lees's avatar
Martin Lees committed
494
            return;
495
496
497
498
499
500
501
502
503
504
505
506
507

        if ((!isset($_POST['Steempress_sp_custom_nonce']) || !wp_verify_nonce($_POST['Steempress_sp_custom_nonce'], 'Steempress_sp_custom_nonce_'.$post_id)) && (!isset($_POST['Steempress_sp_custom_update_nonce']) || !wp_verify_nonce($_POST['Steempress_sp_custom_update_nonce'], 'Steempress_sp_custom_update_nonce_'.$post_id)))
            return;

        if (!current_user_can('edit_post', $post_id))
            return;

        if (get_post_status ($post_id) != 'publish') {
            if (isset($_POST['Steempress_sp_steem_publish'])) {
                update_post_meta($post_id, 'Steempress_sp_steem_publish', $_POST['Steempress_sp_steem_publish']);
            } else {
                update_post_meta($post_id, 'Steempress_sp_steem_publish', '0');
            }
Martin Lees's avatar
Martin Lees committed
508
        } else {
509
510
511
512
513
            if (isset($_POST['Steempress_sp_steem_update'])) {
                update_post_meta($post_id, 'Steempress_sp_steem_update', $_POST['Steempress_sp_steem_update']);
            } else {
                update_post_meta($post_id, 'Steempress_sp_steem_update', '0');
            }
Martin Lees's avatar
Martin Lees committed
514
515
516
        }
    }

517
518
    public function steempress_sp_custom_box_html($post)
    {
Martin Lees's avatar
Martin Lees committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

        $author_id = $post->post_author;

        $options = get_option($this->plugin_name);

        if (!isset($options["username"]))
            $options["username"] = "";


        $author = $options["username"];

        if (isset($options['username' . $author_id]) && $options['username' . $author_id] != "") {
            $author = $options['username' . $author_id];
        }

534
        $permlink = get_post_meta($post->ID, 'steempress_sp_permlink', true);
535
536
537
538
        $meta_author = get_post_meta($post->ID, 'steempress_sp_author', true);
        
        if ($meta_author != $author && $meta_author != "")
            $author = $meta_author;
539

540
        $body = "
541
              <p>These options are only for advanced users regarding steem integration</p>
Martin Lees's avatar
Martin Lees committed
542
543
              <label for=\"steempress_sp_author\">Author : </label><br>
              <input type='text' name='steempress_sp_author' value='".$author."'/><br>
544
545
546
              <label for=\"steempress_sp_author\">Permlink</label> 
              <input type='text' name='steempress_sp_permlink' value='".$permlink."'/><br>
              ";
547
        // Minified js to handle the "test parameters" function
Martin Lees's avatar
Martin Lees committed
548
        $body .= "<script>function steempress_sp_createCORSRequest(){var e=\"".steempress_sp_twoway_api_url."/test_param\",t=new XMLHttpRequest;return\"withCredentials\"in t?t.open(\"POST\",e,!0):\"undefined\"!=typeof XDomainRequest?(t=new XDomainRequest).open(\"POST\",e):t=null,t}function steempress_sp_test_params(){document.getElementById(\"steempress_sp_status\").innerHTML=\"loading...\";var e=steempress_sp_createCORSRequest(),s=document.getElementsByName(\"steempress_sp_author\")[0].value,n=document.getElementsByName(\"steempress_sp_permlink\")[0].value,r=\"username=\"+s+\"&permlink=\"+n;e.setRequestHeader(\"Content-type\",\"application/x-www-form-urlencoded\"),e&&(e.username=s,e.permlink=n,e.onload=function(){var t=e.responseText;document.getElementById(\"steempress_sp_status\").innerHTML=\"ok\"===t?\"The parameters are correct. this article is linked to this <a href='https://steemit.com/@\"+this.username+\"/\"+this.permlink+\"'>steem post</a>\":\"Error : the permlink or username is incorrect.\"},e.send(r))}</script>";
549

550
        $body .= "<button type=\"button\" onclick='steempress_sp_test_params()'>Test parameters</button><br/><p id='steempress_sp_status'></p>";
551
552
553


        echo $body;
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
    }

    public function steempress_sp_add_custom_box()
    {
        $post_id = get_the_ID();

        if (get_post_type($post_id) != 'post') {
            return;
        }

        if (get_post_status ($post_id) != 'publish')
            return;

        add_meta_box(
            'steempress_sp_box_id',
            'SteemPress options',
            array($this,'steempress_sp_custom_box_html'),
            'post',
            'side'
        );
    }

    function steempress_sp_save_post_data($post_id)
    {
Martin Lees's avatar
Martin Lees committed
578
        if (array_key_exists('steempress_sp_permlink', $_POST) && array_key_exists('steempress_sp_author', $_POST)) {
579
            update_post_meta($post_id,'steempress_sp_permlink',$_POST['steempress_sp_permlink']);
Martin Lees's avatar
Martin Lees committed
580
            update_post_meta($post_id,'steempress_sp_author',$_POST['steempress_sp_author']);
581
582
583
        }
    }

Martin Lees's avatar
Martin Lees committed
584
585
586
587
588
589
590
591
    /* Returned codes :
    1 : ok
    -1 : metadata is incorrect
    -2 : update is not activated
    -3 : Post is not in the published state

    */
    function steempress_sp_update($post_id, $bulk = false)
Martin Lees's avatar
Martin Lees committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
    {
        $post = get_post($post_id);
        if ($post->post_status == "publish") {
            $options = get_option($this->plugin_name);

            // Avoid undefined errors
            if (!isset($options["username"]))
                $options["username"] = "";
            if (!isset($options["posting-key"]))
                $options["posting-key"] = "";
            if (!isset($options["reward"]))
                $options["reward"] = "100";
            if (!isset($options["tags"]))
                $options["tags"] = "";
            if (!isset($options["seo"]))
                $options["seo"] = "on";
            if (!isset($options["vote"]))
                $options["vote"] = "on";
            if (!isset($options["append"]))
                $options["append"] = "off";
            if (!isset($options["delay"]))
                $options["delay"] = "0";
            if (!isset($options["featured"]))
                $options["featured"] = "on";
            if (!isset($options["footer"]))
                $options["footer"] = "<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : [%original_link%] </em><hr/></center>";
            if (!isset($options["update"]))
                $options["update"] = "on";
Martin Lees's avatar
Martin Lees committed
620
621
            if (!isset($options["wordlimit"]))
                $options["wordlimit"] = "0";
Martin Lees's avatar
Martin Lees committed
622

Martin Lees's avatar
Martin Lees committed
623
            if ($options["update"] == "on" || $bulk) {
Martin Lees's avatar
Martin Lees committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

                $author_id = $post->post_author;

                $username = $options["username"];
                $posting_key = $options["posting-key"];

                if (isset($options['username' . $author_id]) && $options['username' . $author_id] != "" && isset($options['posting-key' . $author_id]) && $options['posting-key' . $author_id] != "") {
                    $username = $options['username' . $author_id];
                    $posting_key = $options['posting-key' . $author_id];
                }

                $wp_tags = wp_get_post_tags($post_id);

                if (sizeof($wp_tags) != 0) {

                    $tags = array();

                    foreach ($wp_tags as $tag) {
                        $tags[] = str_replace(" ", "", $tag->name);
                    }

                    $tags = implode(" ", $tags);

                    if ($options["append"] == "on")
                        $tags = $options["tags"] . " " . $tags;
                } else
                    $tags = $options["tags"];
                $link = get_permalink($post->ID);

                if ($options['seo'] == "on")
                    $display_backlink = "true";
                else
                    $display_backlink = "false";

                $content = $post->post_content;
                if ($options["featured"] == "on") {
                    $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_id), 'thumbnail');
                    if ($thumbnail != "0")
                        $content = "<center>" . $thumbnail . "</center> <br/>" . $post->post_content;
                }


                $version = steempress_sp_compte;

                $pos = strrpos(steempress_sp_compte, ".");

                if ($pos !== false)
                    $version = substr_replace(steempress_sp_compte, "", $pos, strlen("."));

                $version = ((float)$version) * 100;

                $permlink = get_post_meta($post_id, "steempress_sp_permlink");

Martin Lees's avatar
Martin Lees committed
677
678
679
680
681
                if ($options['wordlimit'] != "0") {
                    $limit = intval($options["wordlimit"]);
                    $content = TruncateHTML::truncateWords($content, $limit, '');
                }

Martin Lees's avatar
Martin Lees committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
                $data = array("body" => array(
                    "title" => $post->post_title,
                    "content" => $content,
                    "tags" => $tags,
                    "author" => $username,
                    "wif" => $posting_key,
                    "original_link" => $link,
                    "wordpress_id" => $post_id,
                    "display_backlink" => $display_backlink,
                    "version" => $version,
                    "footer" => $options['footer'],
                    "permlink" => $permlink[0],
                    "vote"=> $options["vote"],
                    "reward" => $options['reward'],
                ));


                // A few local verifications as to not overload the server with useless txs

                $test = $data['body'];
                if ($test['tags'] != "" && $test['author'] != "" && $test['wif'] != "") {
Martin Lees's avatar
Martin Lees committed
703
                    // Post to the api who will update it on the steem blockchain.
Martin Lees's avatar
Martin Lees committed
704
                    $result = wp_remote_post(steempress_sp_api_url . "/update", $data);
Martin Lees's avatar
Martin Lees committed
705
706
707
708
709
710
711
712
713
714
715
716
717
                    if (!isset($result->errors)) {
                        $data = $result['body'];
                        if ($data == "ok")
                            return 1;
                        else
                            return -1;
                    }
                } else
                    return -1;
            } else
                return -2;
        } else
            return -3;
Martin Lees's avatar
Martin Lees committed
718
719
    }

Martin Lees's avatar
Martin Lees committed
720
}