Plugin Directory

Changeset 2722017


Ignore:
Timestamp:
05/11/2022 01:36:27 PM (4 years ago)
Author:
crowdaa
Message:

Feature update. Added 1.1.0 tag.

Location:
crowdaa-sync
Files:
2 added
21 edited
14 copied

Legend:

Unmodified
Added
Removed
  • crowdaa-sync/tags/1.1.0/CHANGELOG

    r2620193 r2722017  
    66
    77## [Unreleased]
     8
     9## [1.1.0] - 2022-05-11
     10
     11### Added
     12- Some more internal actions & filters
     13- Blacklist & whitelist mode by categories
     14- A bit more logs & debug
     15- Media captions synchronization
     16- Post author name synchronization per post instead of a global shared name
     17- Notifications sending when publishing an article
     18- Automatic generation of auto-login tokens on JWT login token generation when both "Autologin Links" and "JWT Authentication for WP-API" are installed
     19
     20### Removed
     21- Several fields when synchronizing a category, to keep previous values from the API
     22
     23### Changed
     24- Minor code improvements (indentation)
     25- Some WP actions & filters names (to be more explicit)
    826
    927## [1.0.42] - 2020-09-06
     
    1937- Control to get the last plugin logs
    2038- Control to run the synchronization manually
    21 
    22 [Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...HEAD
    23 [1.0.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v1.0.0
    24 [0.3.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.2.0...v0.3.0
    25 [0.2.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.1.0...v0.2.0
    26 [0.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.8...v0.1.0
    27 [0.0.8]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.7...v0.0.8
    28 [0.0.7]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.6...v0.0.7
    29 [0.0.6]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.5...v0.0.6
    30 [0.0.5]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.4...v0.0.5
    31 [0.0.4]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.3...v0.0.4
    32 [0.0.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.2...v0.0.3
    33 [0.0.2]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.1...v0.0.2
    34 [0.0.1]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/v0.0.1
  • crowdaa-sync/tags/1.1.0/README.txt

    r2620193 r2722017  
    22Contributors: crowdaa
    33Donate link: https://www.crowdaa.com
    4 Tags: crowdaa, app, application, mobile, sync, synchronization, api
     4Tags: crowdaa, app, application, mobile, sync, synchronization, api, cloud
    55Requires at least: 5.5.5
    66Requires PHP: 7.3
    77Tested up to: 5.8
    8 Stable tag: 1.0.50
     8Stable tag: 1.1.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • crowdaa-sync/tags/1.1.0/admin/class-crowdaa-sync-add-info-api.php

    r2620193 r2722017  
    7070    }
    7171
    72     $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
    73     $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
    74 
    75     $result = $this->synchronization($api_to_wp_sync_from, $wp_to_api_sync_from);
     72    $result = $this->synchronization();
    7673
    7774    if (!$result['error']) {
     
    103100   */
    104101  public function get_opqueue_ajax() {
    105     $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
    106     $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
    107 
    108     $articles_opqueue = $this->get_articles_opqueue($api_to_wp_sync_from, $wp_to_api_sync_from);
     102    $articles_opqueue = $this->get_articles_opqueue();
    109103
    110104    wp_send_json([
     
    119113   * @return array, object
    120114   */
    121   public function get_articles_opqueue($api_to_wp_sync_from = null, $wp_to_api_sync_from = null) {
     115  public function get_articles_opqueue() {
     116    $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
     117    $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
     118
    122119    Crowdaa_Sync_Logs::log('Syncing, Articles OpQueue fetching', 'API>WP from:', $api_to_wp_sync_from, 'WP>API from:', $wp_to_api_sync_from);
    123120
     
    296293    }
    297294
    298     do_action('crowdaa_sync_opqueue_got');
     295    do_action('crowdaa_sync_articles_opqueue_built');
    299296
    300297    return $result;
     
    317314   * @return boolean
    318315   */
    319   private function synchronization($api_to_wp_sync_from, $wp_to_api_sync_from) {
     316  private function synchronization() {
    320317    $result = [];
    321318
     
    334331
    335332    try {
     333      do_action('crowdaa_sync_articles_synchronization_init');
     334
    336335      $info_wp = new Crowdaa_Sync_Add_Info_WP();
    337       $articles_opqueue = $this->get_articles_opqueue($api_to_wp_sync_from, $wp_to_api_sync_from);
     336      $articles_opqueue = $this->get_articles_opqueue();
    338337
    339338      if (isset($articles_opqueue['error']) && $articles_opqueue['error']) {
     
    356355      $api_errors = false;
    357356
     357      do_action('crowdaa_sync_articles_synchronization_start');
    358358      // Sync WP => API
    359359      if (is_array($articles_opqueue['wp_to_api']) && is_array($articles_opqueue['only_wp'])) {
     
    370370      }
    371371
    372       do_action('crowdaa_sync_synchronizing');
     372      do_action('crowdaa_sync_synchronization_custom');
    373373
    374374      if ($wp_errors || $api_errors) {
     
    403403   */
    404404  public function ajax_synchronization() {
    405     $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
    406     $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
    407 
    408     $result = $this->synchronization($api_to_wp_sync_from, $wp_to_api_sync_from);
     405    $result = $this->synchronization();
    409406
    410407    if (!isset($result['error'])) {
     
    452449
    453450
    454   /**
    455    * Check info for update or create
    456    *
    457    * @since    1.0.0
    458    * @param $wp_to_api
    459    * @param $only_wp
    460    * @return boolean
    461    */
     451  private function remove_already_synced_post($post_id) {
     452    $sync_api = new Crowdaa_Sync_API();
     453    $api_post_id = get_post_meta($post_id, 'api_post_id', true);
     454
     455    if ($api_post_id) {
     456      try {
     457        $sync_api->delete_post_api($api_post_id);
     458      } catch (Exception $e) {
     459        /* Do nothing */
     460      }
     461    }
     462
     463    delete_post_meta($post_id, 'crowdaa_need_sync');
     464    delete_post_meta($post_id, 'crowdaa_version');
     465    delete_post_meta($post_id, 'crowdaa_last_wp_to_api_sync');
     466    delete_post_meta($post_id, 'api_post_id');
     467  }
     468
    462469  public function sync_info_api(&$wp_to_api, &$only_wp) {
    463470    $posts_add_to_api = array_merge($wp_to_api, $only_wp);
     
    491498    }
    492499
    493     foreach ($terms_hash as $term) {
    494       $root_term = $term;
    495       while ($root_term->parent) {
    496         $root_term = $terms_hash[$root_term->parent];
    497       }
    498 
    499       if ($root_term->term_id !== $term->term_id) {
    500         $term->root_term_id = $root_term->term_id;
    501       } else {
    502         $term->root_term_id = $term->parent;
    503       }
    504     }
    505 
    506500    $args = array(
    507501      'hide_empty' => false,
     
    520514    }
    521515
     516    $sync_categories_mode = get_option('crowdaa_sync_categories_mode', 'blacklist');
     517    $sync_categories_list = get_option('crowdaa_sync_categories_list', '');
     518    if ($sync_categories_list === '') {
     519      $sync_categories_list = [];
     520    } else {
     521      $sync_categories_list = explode(',', $sync_categories_list);
     522    }
     523
     524    foreach ($terms_hash as $term) {
     525      $root_term = $term;
     526      while ($root_term->parent) {
     527        $root_term = $terms_hash[$root_term->parent];
     528      }
     529
     530      if ($sync_categories_mode === 'whitelist' && !in_array($root_term->term_id, $sync_categories_list)) {
     531        $term->root_term_id = null;
     532        continue;
     533      } else if ($sync_categories_mode === 'blacklist' && in_array($root_term->term_id, $sync_categories_list)) {
     534        $term->root_term_id = null;
     535        continue;
     536      }
     537
     538      if ($root_term->term_id !== $term->term_id) {
     539        $term->root_term_id = $root_term->term_id;
     540      } else {
     541        $term->root_term_id = $term->parent;
     542      }
     543    }
     544
    522545    foreach ($posts_add_to_api as $postArrayId => $post) {
    523546      Crowdaa_Sync_Timer::check();
     
    531554        Crowdaa_Sync_Logs::log('No categories for this post, skipping...');
    532555        continue;
    533       } else {
    534         foreach ($post_terms as $term) {
    535           if (in_array($term->term_id, $child_terms)) {
    536             $child_term = true;
    537             $post_term  = [
    538               'id'   => $term->term_id,
    539               'slug' => $term->slug,
    540               'name' => $term->name,
    541             ];
     556      }
     557
     558      $whitelisted = ($sync_categories_mode !== 'whitelist');
     559      $blacklisted = false;
     560      foreach ($post_terms as $term) {
     561        $blacklisted_term = false;
     562
     563        if (in_array($term->term_id, $sync_categories_list)) {
     564          if ($sync_categories_mode === 'blacklist') {
     565            $blacklisted = $term->term_id;
     566          } else {
     567            $whitelisted = $term->term_id;
     568            if (!$post_term) {
     569              $child_term = true; // Lie here to avoid losing this term.
     570              $post_term  = [
     571                'id'   => $term->term_id,
     572                'slug' => $term->slug,
     573                'name' => $term->name,
     574              ];
     575            }
    542576          }
    543         }
    544         if ($child_term == false) {
     577        } else if (
     578          $sync_categories_mode !== 'whitelist' &&
     579          !$blacklisted &&
     580          !$post_term &&
     581          in_array($term->term_id, $child_terms)
     582        ) {
     583          $child_term = true;
    545584          $post_term  = [
    546             'id'   => $post_terms[0]->term_id,
    547             'slug' => $post_terms[0]->slug,
    548             'name' => $post_terms[0]->name,
    549           ];
    550         }
    551 
    552         $term = $terms_hash[$post_term['id']];
    553         if ($term->root_term_id) {
    554           $term = $terms_hash[$term->root_term_id];
    555           $post_term['parent'] = [
    556585            'id'   => $term->term_id,
    557586            'slug' => $term->slug,
     
    561590      }
    562591
     592      if (!$whitelisted) {
     593        Crowdaa_Sync_Logs::log('Could not find a whitelisted category for this article, skipping');
     594        $api_post_id = get_post_meta($post['post_id'], 'api_post_id');
     595        if ($api_post_id) {
     596          Crowdaa_Sync_Logs::log('Post was already synchronized, removing it from the API');
     597          $this->remove_already_synced_post($post['post_id']);
     598        }
     599
     600        continue;
     601      } else if ($blacklisted) {
     602        Crowdaa_Sync_Logs::log('Blacklisted category', $blacklisted, 'skipping');
     603        $api_post_id = get_post_meta($post['post_id'], 'api_post_id');
     604        if ($api_post_id) {
     605          Crowdaa_Sync_Logs::log('Post was already synchronized, removing it from the API');
     606          $this->remove_already_synced_post($post['post_id']);
     607        }
     608
     609        continue;
     610      }
     611
     612      if ($child_term == false) {
     613        $post_term  = [
     614          'id'   => $post_terms[0]->term_id,
     615          'slug' => $post_terms[0]->slug,
     616          'name' => $post_terms[0]->name,
     617        ];
     618      }
     619
     620      $term = $terms_hash[$post_term['id']];
     621      if ($term->root_term_id) {
     622        $term = $terms_hash[$term->root_term_id];
     623        $post_term['parent'] = [
     624          'id'   => $term->term_id,
     625          'slug' => $term->slug,
     626          'name' => $term->name,
     627        ];
     628      }
     629
    563630      try {
     631        $can_sync = apply_filters('crowdaa_sync_can_sync_wp_post', true, $post['post_id']);
     632        if (!$can_sync) {
     633          Crowdaa_Sync_Logs::log('Post sync denied by "crowdaa_sync_can_sync_wp_post" filter');
     634          $api_post_id = get_post_meta($post['post_id'], 'api_post_id');
     635          if ($api_post_id) {
     636            Crowdaa_Sync_Logs::log('Post was already synchronized, removing it from the API');
     637            $this->remove_already_synced_post($post['post_id']);
     638          }
     639
     640          continue;
     641        }
     642
    564643        // Create or update parent category, if needed
    565644        $parent_api_term_id = false;
     
    607686        $img_errors = $sync_api->sync_post_images($post['post_id']);
    608687        if($img_errors) {
    609           $errors += $img_errors;
     688          throw new Crowdaa_Sync_Post_Skip_Error('Medias sync errors, skipping ('.implode(', ', $img_errors).')');
    610689        }
    611690
     
    625704        continue;
    626705      } catch (Crowdaa_Sync_Post_Error $e) {
     706        Crowdaa_Sync_Logs::log('Post sync error', $e->getMessage());
    627707        $errors[] = $e->getMessage();
    628708        continue;
  • crowdaa-sync/tags/1.1.0/admin/class-crowdaa-sync-admin-display.php

    r2620193 r2722017  
    268268
    269269  /**
     270   * Set which categories will be synchronized or not on the app
     271   *
     272   * @since    1.0.0
     273   */
     274  public function choose_sync_categories() {
     275    if(!isset($_POST['crowdaa_set_sync_categories']) || !wp_verify_nonce($_POST['crowdaa_set_sync_categories'], 'crowdaa_set_sync_categories_data')) {
     276      return;
     277    }
     278
     279    if(isset($_POST['sync_categories_mode_whitelist'])) {
     280      update_option('crowdaa_sync_categories_mode', 'whitelist');
     281    } else {
     282      update_option('crowdaa_sync_categories_mode', 'blacklist');
     283    }
     284
     285    $categories = array_filter($_POST['sync_categories'], function($v) {
     286      if (empty($v)) return (false);
     287      $v = (string) $v;
     288      if (!preg_match('/^[0-9]+$/', $v)) return (false);
     289
     290      return (true);
     291    });
     292
     293    update_option('crowdaa_sync_categories_list', implode(',', $categories));
     294
     295    Crowdaa_Sync_Versions::bump_version();
     296  }
     297
     298  /**
    270299   * Admin notices
    271300   *
  • crowdaa-sync/tags/1.1.0/admin/class-crowdaa-sync-api.php

    r2620193 r2722017  
    9696   */
    9797  private function fetch_post_api_medias($wp_post_id) {
     98    $captions = [];
    9899    $api_feedpicture_id = get_post_meta($wp_post_id, 'api_feedpicture_id', true) ?: '';
    99100    if ($api_feedpicture_id) {
    100101      $api_feedpicture_id = unserialize($api_feedpicture_id);
     102      $captions[] = wp_get_attachment_caption($api_feedpicture_id['attachment_id']);
    101103      $api_feedpicture_id = $api_feedpicture_id['api_id'];
    102104    }
     
    112114        $pictures_id[] = $api_id;
    113115      }
    114     }
     116      $captions[] = wp_get_attachment_caption($data['attachment_id']);
     117    }
     118
     119    $captions = array_filter($captions, 'strlen');
    115120
    116121    return ([
    117122      'api_feedpicture_id' => $api_feedpicture_id,
    118       'videos_id' => $videos_id,
    119       'pictures_id' => $pictures_id,
     123      'videos_id'          => $videos_id,
     124      'pictures_id'        => $pictures_id,
     125      'captions'           => $captions,
    120126    ]);
    121127  }
     
    154160
    155161    $post_content = get_post_field('post_content', $wp_post_id);
     162    $post_author = get_the_author_meta('display_name', get_post_field('post_author', $wp_post_id));
    156163    $publication_time = get_post_datetime($wp_post_id)->getTimestamp();
    157164
     
    159166    $wp_post_title = html_entity_decode($wp_post_title, ENT_QUOTES);
    160167    $data = [
    161       'articleId'    => $api_post_id,
    162       'actions'      => [],
    163       'categoryId'   => $category_id,
    164       'feedPicture'  => $api_feedpicture_id,
    165       'md'           => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
    166       'pictures'     => $pictures_id,
    167       'summary'      => $wp_post_title,
    168       'hideFromFeed' => !$feed_display,
    169       'productId'    => '',
    170       'title'        => $wp_post_title,
    171       'videos'       => $videos_id,
     168      'articleId'     => $api_post_id,
     169      'actions'       => [],
     170      'authorName'    => $post_author,
     171      'categoryId'    => $category_id,
     172      'feedPicture'   => $api_feedpicture_id,
     173      'md'            => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
     174      'pictures'      => $pictures_id,
     175      'summary'       => $wp_post_title,
     176      'hideFromFeed'  => !$feed_display,
     177      'productId'     => '',
     178      'title'         => $wp_post_title,
     179      'videos'        => $videos_id,
     180      'mediaCaptions' => implode(' | ', $api_media_data['captions']),
    172181    ];
     182    $data = apply_filters('crowdaa_sync_api_update_article_payload', $data, $wp_post_id);
    173183
    174184    $response = $this->http_request('PUT', '/press/articles', $data);
     
    187197      }
    188198
    189       $publish_error = $this->publish_post_draft_api($json->draftId, $api_post_id, $publication_time);
     199      $send_notification = (
     200        get_post_meta($wp_post_id, 'crowdaa_notification_send', true) === 'yes' &&
     201        get_post_meta($wp_post_id, 'crowdaa_notification_sent', true) !== 'yes'
     202      );
     203      $publish_error = $this->publish_post_draft_api(
     204        $json->draftId,
     205        $api_post_id,
     206        $publication_time,
     207        $send_notification,
     208        (get_post_meta($wp_post_id, 'crowdaa_notification_content', true) ?: null),
     209        (get_post_meta($wp_post_id, 'crowdaa_notification_title', true) ?: null),
     210      );
    190211      if ($publish_error) {
    191212        Crowdaa_Sync_Logs::log('Article publish error', $api_post_id, $publish_error);
    192213        throw new Crowdaa_Sync_Post_Error(__('Post publish error : ', CROWDAA_SYNC_PLUGIN_NAME).$publish_error);
     214      }
     215
     216      if ($send_notification) {
     217        update_post_meta($wp_post_id, 'crowdaa_notification_sent', 'yes');
    193218      }
    194219
     
    206231   * @return boolean
    207232   */
    208   public function publish_post_draft_api($draft_id, $api_post_id, $publication_time) {
     233  public function publish_post_draft_api(
     234    $draft_id,
     235    $api_post_id,
     236    $publication_time,
     237    $send_notification = false,
     238    $notification_content = null,
     239    $notification_title = null
     240  ) {
    209241    $data = [
    210       'draftId'           => $draft_id,
    211       'date'              => gmdate(CROWDAA_SYNC_ISO_TIME_FORMAT, $publication_time),
    212       'sendNotifications' => false,
     242      'draftId'             => $draft_id,
     243      'date'                => gmdate(CROWDAA_SYNC_ISO_TIME_FORMAT, $publication_time),
     244      'sendNotifications'   => $send_notification,
     245      'notificationContent' => $notification_content,
     246      'notificationTitle'   => $notification_title,
    213247    ];
    214248
     
    239273   * @param $article_data
    240274   */
    241   public function create_custom_article_api($article_data, $publication_time) {
     275  public function create_custom_article_api($article_data, $publication_time, $send_notification = false) {
    242276    $auth_token = get_option('crowdaa_auth_token');
    243277    if (!$auth_token) {
     
    277311      }
    278312
    279       $publish_error = $this->publish_post_draft_api($json->draftId, $json->articleId, $publication_time);
     313      $publish_error = $this->publish_post_draft_api(
     314        $json->draftId,
     315        $json->articleId,
     316        $publication_time,
     317        $send_notification
     318      );
    280319      if ($publish_error) {
    281320        Crowdaa_Sync_Logs::log('Custom article first publish error', $json->articleId, $publish_error);
     
    295334   * @param $article_data
    296335   */
    297   public function update_custom_article_api($article_data, $publication_time) {
     336  public function update_custom_article_api($article_data, $publication_time, $send_notification = false) {
    298337    $auth_token = get_option('crowdaa_auth_token');
    299338    if (!$auth_token) {
     
    333372      }
    334373
    335       $publish_error = $this->publish_post_draft_api($json->draftId, $json->articleId, $publication_time);
     374      $publish_error = $this->publish_post_draft_api(
     375        $json->draftId,
     376        $json->articleId,
     377        $publication_time,
     378        $send_notification
     379      );
    336380      if ($publish_error) {
    337381        Crowdaa_Sync_Logs::log('Custom article publish error', $json->articleId, $publish_error);
     
    377421
    378422    $post_content = get_post_field('post_content', $wp_post_id);
     423    $post_author = get_the_author_meta('display_name', get_post_field('post_author', $wp_post_id));
    379424    $publication_time = get_post_datetime($wp_post_id)->getTimestamp();
    380425
     
    382427    $wp_post_title = html_entity_decode($wp_post_title, ENT_QUOTES);
    383428    $data = [
    384       'actions'      => [],
    385       'categoryId'   => $category_id,
    386       'feedPicture'  => $api_feedpicture_id,
    387       'md'           => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
    388       'pictures'     => $pictures_id,
    389       'summary'      => $wp_post_title,
    390       'hideFromFeed' => !$feed_display,
    391       'productId'    => '',
    392       'title'        => $wp_post_title,
    393       'videos'       => $videos_id,
     429      'actions'       => [],
     430      'authorName'    => $post_author,
     431      'categoryId'    => $category_id,
     432      'feedPicture'   => $api_feedpicture_id,
     433      'md'            => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
     434      'pictures'      => $pictures_id,
     435      'summary'       => $wp_post_title,
     436      'hideFromFeed'  => !$feed_display,
     437      'productId'     => '',
     438      'title'         => $wp_post_title,
     439      'videos'        => $videos_id,
     440      'mediaCaptions' => implode(' | ', $api_media_data['captions']),
    394441    ];
     442    $data = apply_filters('crowdaa_sync_api_create_article_payload', $data, $wp_post_id);
    395443
    396444    Crowdaa_Sync_Logs::log('Creating API post', $wp_post_id, wp_json_encode($data));
     
    414462      }
    415463
    416       $publish_error = $this->publish_post_draft_api($json->draftId, $json->articleId, $publication_time);
     464      $send_notification = (
     465        get_post_meta($wp_post_id, 'crowdaa_notification_send', true) === 'yes' &&
     466        get_post_meta($wp_post_id, 'crowdaa_notification_sent', true) !== 'yes'
     467      );
     468      $publish_error = $this->publish_post_draft_api(
     469        $json->draftId,
     470        $json->articleId,
     471        $publication_time,
     472        $send_notification,
     473        (get_post_meta($wp_post_id, 'crowdaa_notification_content', true) ?: null),
     474        (get_post_meta($wp_post_id, 'crowdaa_notification_title', true) ?: null),
     475      );
    417476      if ($publish_error) {
    418477        Crowdaa_Sync_Logs::log('Article first publish error', $json->articleId, $json->draftId, $publish_error);
     
    421480
    422481      update_post_meta($wp_post_id, 'api_post_id', $json->articleId);
     482      if ($send_notification) {
     483        update_post_meta($wp_post_id, 'crowdaa_notification_sent', 'yes');
     484      }
    423485      Crowdaa_Sync_Logs::log('Created API post', $json->articleId);
    424486    }
     
    747809      'action'   => '',
    748810    ];
     811    $data = apply_filters('crowdaa_sync_api_create_category_payload', $data);
    749812
    750813    $response = $this->http_request('POST', '/press/categories', $data);
     
    789852      'name'     => $cat_name,
    790853      'pathName' => $cat_slug,
    791       'color'    => '',
    792       'order'    => 1,
    793854      'hidden'   => false,
    794       'picture'  => [],
    795855      'parentId' => $parent_category_id,
    796856      'action'   => '',
     857      // 'picture'  => null,
     858      // 'color'    => null,
     859      // 'order'    => null,
    797860    ];
     861    $data = apply_filters('crowdaa_sync_api_update_category_payload', $data);
    798862
    799863    $response = $this->http_request('PUT', '/press/categories/'.$cat_id, $data);
     
    829893   */
    830894  public function delete_post_api($api_post_id) {
    831     $data = [
    832       'draftId' => $api_post_id,
    833     ];
    834 
    835     $response = $this->http_request('DELETE', '/press/articles/'.$api_post_id, $data);
     895    $response = $this->http_request('DELETE', '/press/articles/'.$api_post_id);
    836896    $err    = is_wp_error($response) ? $response->get_error_message() : null;
    837897    if (!$err) {
  • crowdaa-sync/tags/1.1.0/admin/class-crowdaa-sync-meta-box.php

    r2620193 r2722017  
    1818   */
    1919  public function __construct() {
    20     add_action('add_meta_boxes', [$this, 'meta_box_add'], 10, 2);
    21     add_action('save_post', [$this, 'img_gallery_save']);
     20    add_action('add_meta_boxes', [$this, 'add_meta_boxes'], 10, 2);
     21    add_action('save_post', [$this, 'save_meta_boxes']);
    2222  }
    2323
     
    2929   * @return boolean
    3030   */
    31   function meta_box_add() {
     31  function add_meta_boxes() {
    3232    add_meta_box(
    3333      'crowdaa_feat_img_slider',
    34       'Featured Image Gallery',
    35       [$this, 'print_box'],
     34      'Crowdaa-sync: Featured Image Gallery',
     35      [$this, 'print_feat_img_slider_box'],
     36      'post',
     37      'advanced',
     38      'high',
     39      array(
     40        '__back_compat_meta_box' => false,
     41      )
     42    );
     43    add_meta_box(
     44      'crowdaa-sync-notification-send',
     45      'Crowdaa-sync: Send notifications when published?',
     46      [$this, 'print_send_notification_checkbox'],
    3647      'post',
    3748      'advanced',
     
    124135
    125136  /**
    126    * Meta box html
     137   * Image selector meta box
    127138   *
    128139   * @since    1.0.0
     
    130141   * @return string
    131142   */
    132   function print_box($post) {
     143  function print_feat_img_slider_box($post) {
    133144    wp_nonce_field('save_feat_gallery', 'feat_gallery_nonce');
    134145
     
    138149
    139150  /**
    140    * Save meta box data
    141    *
    142    * @since    1.0.0
    143    * @param $post_id
    144    * @return boolean
    145    */
    146   function img_gallery_save($post_id) {
     151   * Notification checkbox meta box
     152   *
     153   * @since    1.0.0
     154   * @param $post
     155   * @return string
     156   */
     157  function print_send_notification_checkbox($post) {
     158    $notification_content = get_post_meta($post->ID, 'crowdaa_notification_content', true) ?: '';
     159    $notification_title = get_post_meta($post->ID, 'crowdaa_notification_title', true) ?: '';
     160    wp_nonce_field('crowdaa_notification_checkbox', 'crowdaa_notification_checkbox_nonce');
     161
     162    ?>
     163    <p>
     164      <i>
     165        <?php
     166          esc_html_e('Check this box if you want to send a notification when the article is published or right now if it is already published.', CROWDAA_SYNC_PLUGIN_NAME);
     167        ?>
     168      </i>
     169    </p>
     170
     171    <div class="crowdaa-meta-box-input-row">
     172      <label>
     173        <?php
     174          esc_html_e('Send notification :', CROWDAA_SYNC_PLUGIN_NAME);
     175        ?>
     176        <input
     177          type="checkbox"
     178          name="crowdaa_send_post_notification"
     179        />
     180      </label>
     181    </div>
     182    <div class="crowdaa-meta-box-input-row">
     183      <label>
     184        <?php
     185          esc_html_e('Notification text (When both are not set, defaults to the article title & first characters) :', CROWDAA_SYNC_PLUGIN_NAME);
     186        ?>
     187        <br />
     188        <input
     189          style="display: inline-block; width: 500px;"
     190          type="text"
     191          name="crowdaa_send_post_notification_title"
     192          placeholder="<?php esc_attr_e('Notification title', CROWDAA_SYNC_PLUGIN_NAME); ?>"
     193          value="<?php echo esc_attr($notification_title); ?>"
     194        />
     195        <br />
     196        <input
     197          style="display: inline-block; width: 500px;"
     198          type="text"
     199          name="crowdaa_send_post_notification_content"
     200          placeholder="<?php esc_attr_e('Notification content', CROWDAA_SYNC_PLUGIN_NAME); ?>"
     201          value="<?php echo esc_attr($notification_content); ?>"
     202        />
     203      </label>
     204    </div>
     205    <?php
     206  }
     207
     208  function save_gallery_meta_box($post_id) {
    147209    if (!isset($_POST['feat_gallery_nonce'])) {
    148       return $post_id;
    149     }
    150 
    151     if (!wp_verify_nonce($_POST['feat_gallery_nonce'], 'save_feat_gallery')) {
    152       return $post_id;
     210      return;
     211    } else if (!wp_verify_nonce($_POST['feat_gallery_nonce'], 'save_feat_gallery')) {
     212      return;
     213    } else if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
     214      return;
     215    } else if (!current_user_can('edit_post', $post_id)) {
     216      return;
    153217    }
    154218
     
    167231    }
    168232  }
     233
     234  function save_notifications_meta_box($post_id) {
     235    if (!isset($_POST['crowdaa_notification_checkbox_nonce'])) {
     236      return;
     237    } else if (!wp_verify_nonce($_POST['crowdaa_notification_checkbox_nonce'], 'crowdaa_notification_checkbox')) {
     238      return;
     239    } else if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
     240      return;
     241    } else if (!current_user_can('edit_post', $post_id)) {
     242      return;
     243    }
     244
     245    if (isset($_POST['crowdaa_send_post_notification'])) {
     246      update_post_meta($post_id, 'crowdaa_notification_send', 'yes');
     247      update_post_meta($post_id, 'crowdaa_notification_sent', 'no');
     248      update_post_meta($post_id, 'crowdaa_notification_title', $_POST['crowdaa_send_post_notification_title'] ?: '');
     249      update_post_meta($post_id, 'crowdaa_notification_content', $_POST['crowdaa_send_post_notification_content'] ?: '');
     250    }
     251  }
     252
     253  /**
     254   * Save meta boxes data
     255   *
     256   * @since    1.0.0
     257   * @param $post_id
     258   * @return boolean
     259   */
     260  function save_meta_boxes($post_id) {
     261    $this->save_gallery_meta_box($post_id);
     262    $this->save_notifications_meta_box($post_id);
     263  }
    169264}
  • crowdaa-sync/tags/1.1.0/admin/css/crowdaa-sync-admin.css

    r2620193 r2722017  
    3131  display: flex;
    3232  flex-direction: row;
    33   max-width: 310px;
     33  justify-content: space-between;
     34  max-width: 400px;
    3435}
    3536
     
    3940
    4041.sync-form-label {
    41   width: 250px;
     42  width: 300px;
    4243  display: inline-block;
    4344}
     
    4748}
    4849
    49 .sync-feed-categories-select {
     50.sync-categories-select {
    5051  min-height: 150px !important;
    5152  width: 600px;
     
    6970  position: absolute;
    7071  top: -7px;
     72  right: 0px;
    7173  height: 30px;
    7274  line-height: 30px;
     
    8385  content: "ON";
    8486  color: #0a0;
     87}
     88
     89.sync-enable-checkbox-custom {
     90  visibility: hidden;
     91  cursor: pointer;
     92}
     93
     94.sync-enable-checkbox-custom+.sync-enable-checkbox-custom-content {
     95  display: block;
     96  height: 30px;
     97  line-height: 30px;
     98  width: auto;
     99  padding: 0 10px;
     100  text-align: center;
     101  border-radius: 4px;
     102  background: #fff;
     103  color: rgb(4, 174, 216);
     104  font-weight: 600;
     105  cursor: pointer;
     106}
     107
     108.sync-enable-checkbox-custom:checked+.sync-enable-checkbox-custom-content {
     109  color: rgb(255, 167, 0);
     110}
     111
     112.sync-enable-checkbox-custom+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-checked {
     113  display: none;
     114}
     115
     116.sync-enable-checkbox-custom+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-unchecked {
     117  display: inline;
     118}
     119
     120.sync-enable-checkbox-custom:checked+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-unchecked {
     121  display: none;
     122}
     123
     124.sync-enable-checkbox-custom:checked+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-checked {
     125  display: inline;
    85126}
    86127
     
    177218  padding: 5px 10px;
    178219}
     220
     221.crowdaa-meta-box-input-row {
     222  display: block;
     223  margin-bottom: 10px;
     224}
  • crowdaa-sync/tags/1.1.0/admin/js/crowdaa-sync-admin.js

    r2620193 r2722017  
    9191    updateFeedCategoriesSelect();
    9292
     93    function updateSyncCategoriesSelect() {
     94      const $field = $('#crowdaa-sync-categories-mode-whitelist-checkbox');
     95      const $whitelistText = $('#crowdaa-sync-categories-explanation-whitelist');
     96      const $blacklistText = $('#crowdaa-sync-categories-explanation-blacklist');
     97      if ($field.prop('checked')) {
     98        $whitelistText.show();
     99        $blacklistText.hide();
     100      } else {
     101        $whitelistText.hide();
     102        $blacklistText.show();
     103      }
     104    }
     105    $('#crowdaa-sync-categories-mode-whitelist-checkbox').on('change', updateSyncCategoriesSelect);
     106    updateSyncCategoriesSelect();
     107
    93108    /**
    94109     * Ajax add posts and terms
  • crowdaa-sync/tags/1.1.0/admin/partials/crowdaa-sync-admin-display.php

    r2620193 r2722017  
    115115        <?php do_action('crowdaa_sync_configuration_levers'); ?>
    116116        <label class="sync-form-checkbox-label">
    117           <span class="sync-form-label"><?php esc_html_e('Maximum synchronization duration'); ?></span> &nbsp;
     117          <span class="sync-form-label"><?php esc_html_e('Maximum synchronization duration (minutes)'); ?></span> &nbsp;
    118118          <input type="number" name="sync_max_duration" min="2" id="sync-duration-field"
    119119            value="<?php echo esc_attr($sync_max_duration); ?>"
    120120          />
    121           <?php esc_html_e('minutes'); ?>
    122121        </label>
    123122        <br />
     
    147146        <?php
    148147        submit_button('Save picture');
     148        ?>
     149      </form>
     150    </div>
     151
     152    <hr/>
     153
     154    <div>
     155      <h2><?php esc_html_e('Set categories to synchronize', CROWDAA_SYNC_PLUGIN_NAME) ?></h2>
     156      <form method="post" action="<?php $admin_utils->choose_sync_categories(); ?>"  enctype="multipart/form-data">
     157        <?php
     158          $sync_categories_mode = get_option('crowdaa_sync_categories_mode', 'blacklist');
     159          $sync_categories_list = explode(',', get_option('crowdaa_sync_categories_list', ''));
     160        ?>
     161        <label class="sync-form-checkbox-label">
     162          <span class="sync-form-label"><?php esc_html_e('Categories sync mode', CROWDAA_SYNC_PLUGIN_NAME); ?></span> &nbsp;
     163          <input
     164            type="checkbox"
     165            name="sync_categories_mode_whitelist"
     166            class="sync-enable-checkbox-custom"
     167            id="crowdaa-sync-categories-mode-whitelist-checkbox"
     168            <?php echo ($sync_categories_mode === 'whitelist' ? 'checked="checked"' : ''); ?>
     169          />
     170          <span class="sync-enable-checkbox-custom-content">
     171            <span class="sync-enable-checkbox-custom-checked">Whitelist</span>
     172            <span class="sync-enable-checkbox-custom-unchecked">Blacklist</span>
     173          </span>
     174        </label>
     175        <div id="crowdaa-sync-categories-select-area">
     176          <label>
     177            <p>
     178              <span id="crowdaa-sync-categories-explanation-whitelist">
     179                <?php esc_html_e('Select below the categories that you *want* to synchronize to the app.', CROWDAA_SYNC_PLUGIN_NAME); ?>
     180              </span>
     181              <span id="crowdaa-sync-categories-explanation-blacklist">
     182                <?php esc_html_e('Select below the categories that you *do not want* to synchronize to the app.', CROWDAA_SYNC_PLUGIN_NAME); ?>
     183              </span>
     184              <br />
     185              <?php esc_html_e('Use the Ctrl or Shift keys to select multiple categories.', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
     186            </p>
     187            <?php
     188              $terms = get_terms([
     189                "hide_empty" => false,
     190                "taxonomy" => "category",
     191              ]);
     192            ?>
     193            <select class="sync-categories-select" name="sync_categories[]" multiple>
     194                <option
     195                  value=""
     196                  <?php echo (array_search('', $sync_categories_list) !== false ? 'selected="selected"' : ''); ?>
     197                >
     198                  <?php esc_html_e('---', CROWDAA_SYNC_PLUGIN_NAME); ?>
     199                </option>
     200              <?php foreach ($terms as $term) { ?>
     201                <option
     202                  value="<?php echo esc_attr($term->term_id); ?>"
     203                  <?php echo (array_search($term->term_id, $sync_categories_list) !== false ? 'selected="selected"' : ''); ?>
     204                >
     205                  <?php esc_html_e($term->name.' ('.$term->slug.')', CROWDAA_SYNC_PLUGIN_NAME); ?>
     206                </option>
     207              <?php } ?>
     208            </select>
     209          </label>
     210        </div>
     211        <?php wp_nonce_field('crowdaa_set_sync_categories_data', 'crowdaa_set_sync_categories'); ?>
     212        <?php
     213        submit_button('Save mode & categories');
    149214        ?>
    150215      </form>
     
    173238            <p>
    174239              <?php esc_html_e('Select below the categories that you want to see on the feed page.', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
    175               <?php esc_html_e('Use the Ctrl or Shift keys to select multiple categories', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
     240              <?php esc_html_e('Use the Ctrl or Shift keys to select multiple categories.', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
    176241            </p>
    177242            <?php
     
    179244                "hide_empty" => false,
    180245                "taxonomy" => "category",
    181                 "childless" => true
    182246              ]);
    183247              if ($feed_categories === 'all') {
     
    187251              }
    188252            ?>
    189             <select class="sync-feed-categories-select" name="sync_feed_categories[]" multiple>
    190                 <option value="">
    191                   <?php esc_html_e('None', CROWDAA_SYNC_PLUGIN_NAME); ?>
     253            <select class="sync-categories-select" name="sync_feed_categories[]" multiple>
     254                <option
     255                  value=""
     256                  <?php echo (array_search('', $feed_categories) !== false ? 'selected="selected"' : ''); ?>
     257                >
     258                  <?php esc_html_e('---', CROWDAA_SYNC_PLUGIN_NAME); ?>
    192259                </option>
    193260              <?php foreach ($terms as $term) { ?>
  • crowdaa-sync/tags/1.1.0/crowdaa-sync.php

    r2620193 r2722017  
    1414 * Plugin URI:       
    1515 * Description:       Plugin for synchronizing WordPress site and Crowdaa CMS
    16  * Version:           1.0.50
     16 * Version:           1.1.0
    1717 * Requires at least: 5.5
    1818 * Requires PHP:      7.2
     
    3434 * Uses SemVer - https://semver.org
    3535 */
    36 define('CROWDAA_SYNC_VERSION', '1.0.50');
     36define('CROWDAA_SYNC_VERSION', '1.1.0');
    3737define('CROWDAA_SYNC_PLUGIN_DIR', __DIR__);
    3838define('CROWDAA_SYNC_PLUGIN_NAME', 'crowdaa-sync');
     
    4848$last_version = get_option('crowdaa_last_version', false);
    4949if (!$last_version || $last_version < CROWDAA_SYNC_META_VERSION) {
    50   update_option('crowdaa_sync_wp_to_api_from', 0);
     50  update_option('crowdaa_sync_articles_wp_to_api_from', 0);
    5151  update_option('crowdaa_last_version', CROWDAA_SYNC_META_VERSION);
    5252}
     
    111111  }
    112112
     113  if (class_exists('Crowdaa_Sync_Ext_Hooks')) {
     114    new Crowdaa_Sync_Ext_Hooks;
     115  }
     116
    113117  if (class_exists('Crowdaa_Sync_Admin_Display')) {
    114118    new Crowdaa_Sync_Admin_Display;
  • crowdaa-sync/tags/1.1.0/includes/class-crowdaa-sync-versions.php

    r2620193 r2722017  
    1919    if (!$last_version || $last_version < CROWDAA_SYNC_META_VERSION) {
    2020      update_option('crowdaa_last_version', CROWDAA_SYNC_META_VERSION);
    21       update_option('crowdaa_sync_wp_to_api_from', 0);
     21      update_option('crowdaa_sync_articles_wp_to_api_from', 0);
    2222      update_option('crowdaa_sync_internal_version', '0');
    2323    }
     
    3636    update_option('crowdaa_sync_internal_version', "$ver");
    3737
    38     update_option('crowdaa_sync_wp_to_api_from', 0);
     38    update_option('crowdaa_sync_articles_wp_to_api_from', 0);
    3939
    4040    self::update_versions();
  • crowdaa-sync/tags/1.1.0/includes/class-crowdaa-sync.php

    r2620193 r2722017  
    180180    require_once CROWDAA_SYNC_PLUGIN_DIR.'/admin/class-crowdaa-sync-wp-hooks.php';
    181181
     182    /**
     183     * The class for other plugin hooks
     184     */
     185    require_once CROWDAA_SYNC_PLUGIN_DIR.'/admin/class-crowdaa-sync-ext-hooks.php';
     186
    182187    $this->loader = new Crowdaa_Sync_Loader();
    183188  }
  • crowdaa-sync/trunk/CHANGELOG

    r2620193 r2722017  
    66
    77## [Unreleased]
     8
     9## [1.1.0] - 2022-05-11
     10
     11### Added
     12- Some more internal actions & filters
     13- Blacklist & whitelist mode by categories
     14- A bit more logs & debug
     15- Media captions synchronization
     16- Post author name synchronization per post instead of a global shared name
     17- Notifications sending when publishing an article
     18- Automatic generation of auto-login tokens on JWT login token generation when both "Autologin Links" and "JWT Authentication for WP-API" are installed
     19
     20### Removed
     21- Several fields when synchronizing a category, to keep previous values from the API
     22
     23### Changed
     24- Minor code improvements (indentation)
     25- Some WP actions & filters names (to be more explicit)
    826
    927## [1.0.42] - 2020-09-06
     
    1937- Control to get the last plugin logs
    2038- Control to run the synchronization manually
    21 
    22 [Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...HEAD
    23 [1.0.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v1.0.0
    24 [0.3.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.2.0...v0.3.0
    25 [0.2.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.1.0...v0.2.0
    26 [0.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.8...v0.1.0
    27 [0.0.8]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.7...v0.0.8
    28 [0.0.7]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.6...v0.0.7
    29 [0.0.6]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.5...v0.0.6
    30 [0.0.5]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.4...v0.0.5
    31 [0.0.4]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.3...v0.0.4
    32 [0.0.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.2...v0.0.3
    33 [0.0.2]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.1...v0.0.2
    34 [0.0.1]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/v0.0.1
  • crowdaa-sync/trunk/README.txt

    r2620193 r2722017  
    22Contributors: crowdaa
    33Donate link: https://www.crowdaa.com
    4 Tags: crowdaa, app, application, mobile, sync, synchronization, api
     4Tags: crowdaa, app, application, mobile, sync, synchronization, api, cloud
    55Requires at least: 5.5.5
    66Requires PHP: 7.3
    77Tested up to: 5.8
    8 Stable tag: 1.0.50
     8Stable tag: 1.1.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • crowdaa-sync/trunk/admin/class-crowdaa-sync-add-info-api.php

    r2620193 r2722017  
    7070    }
    7171
    72     $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
    73     $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
    74 
    75     $result = $this->synchronization($api_to_wp_sync_from, $wp_to_api_sync_from);
     72    $result = $this->synchronization();
    7673
    7774    if (!$result['error']) {
     
    103100   */
    104101  public function get_opqueue_ajax() {
    105     $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
    106     $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
    107 
    108     $articles_opqueue = $this->get_articles_opqueue($api_to_wp_sync_from, $wp_to_api_sync_from);
     102    $articles_opqueue = $this->get_articles_opqueue();
    109103
    110104    wp_send_json([
     
    119113   * @return array, object
    120114   */
    121   public function get_articles_opqueue($api_to_wp_sync_from = null, $wp_to_api_sync_from = null) {
     115  public function get_articles_opqueue() {
     116    $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
     117    $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
     118
    122119    Crowdaa_Sync_Logs::log('Syncing, Articles OpQueue fetching', 'API>WP from:', $api_to_wp_sync_from, 'WP>API from:', $wp_to_api_sync_from);
    123120
     
    296293    }
    297294
    298     do_action('crowdaa_sync_opqueue_got');
     295    do_action('crowdaa_sync_articles_opqueue_built');
    299296
    300297    return $result;
     
    317314   * @return boolean
    318315   */
    319   private function synchronization($api_to_wp_sync_from, $wp_to_api_sync_from) {
     316  private function synchronization() {
    320317    $result = [];
    321318
     
    334331
    335332    try {
     333      do_action('crowdaa_sync_articles_synchronization_init');
     334
    336335      $info_wp = new Crowdaa_Sync_Add_Info_WP();
    337       $articles_opqueue = $this->get_articles_opqueue($api_to_wp_sync_from, $wp_to_api_sync_from);
     336      $articles_opqueue = $this->get_articles_opqueue();
    338337
    339338      if (isset($articles_opqueue['error']) && $articles_opqueue['error']) {
     
    356355      $api_errors = false;
    357356
     357      do_action('crowdaa_sync_articles_synchronization_start');
    358358      // Sync WP => API
    359359      if (is_array($articles_opqueue['wp_to_api']) && is_array($articles_opqueue['only_wp'])) {
     
    370370      }
    371371
    372       do_action('crowdaa_sync_synchronizing');
     372      do_action('crowdaa_sync_synchronization_custom');
    373373
    374374      if ($wp_errors || $api_errors) {
     
    403403   */
    404404  public function ajax_synchronization() {
    405     $api_to_wp_sync_from = get_option('crowdaa_sync_articles_api_to_wp_from', 0);
    406     $wp_to_api_sync_from = get_option('crowdaa_sync_articles_wp_to_api_from', 0);
    407 
    408     $result = $this->synchronization($api_to_wp_sync_from, $wp_to_api_sync_from);
     405    $result = $this->synchronization();
    409406
    410407    if (!isset($result['error'])) {
     
    452449
    453450
    454   /**
    455    * Check info for update or create
    456    *
    457    * @since    1.0.0
    458    * @param $wp_to_api
    459    * @param $only_wp
    460    * @return boolean
    461    */
     451  private function remove_already_synced_post($post_id) {
     452    $sync_api = new Crowdaa_Sync_API();
     453    $api_post_id = get_post_meta($post_id, 'api_post_id', true);
     454
     455    if ($api_post_id) {
     456      try {
     457        $sync_api->delete_post_api($api_post_id);
     458      } catch (Exception $e) {
     459        /* Do nothing */
     460      }
     461    }
     462
     463    delete_post_meta($post_id, 'crowdaa_need_sync');
     464    delete_post_meta($post_id, 'crowdaa_version');
     465    delete_post_meta($post_id, 'crowdaa_last_wp_to_api_sync');
     466    delete_post_meta($post_id, 'api_post_id');
     467  }
     468
    462469  public function sync_info_api(&$wp_to_api, &$only_wp) {
    463470    $posts_add_to_api = array_merge($wp_to_api, $only_wp);
     
    491498    }
    492499
    493     foreach ($terms_hash as $term) {
    494       $root_term = $term;
    495       while ($root_term->parent) {
    496         $root_term = $terms_hash[$root_term->parent];
    497       }
    498 
    499       if ($root_term->term_id !== $term->term_id) {
    500         $term->root_term_id = $root_term->term_id;
    501       } else {
    502         $term->root_term_id = $term->parent;
    503       }
    504     }
    505 
    506500    $args = array(
    507501      'hide_empty' => false,
     
    520514    }
    521515
     516    $sync_categories_mode = get_option('crowdaa_sync_categories_mode', 'blacklist');
     517    $sync_categories_list = get_option('crowdaa_sync_categories_list', '');
     518    if ($sync_categories_list === '') {
     519      $sync_categories_list = [];
     520    } else {
     521      $sync_categories_list = explode(',', $sync_categories_list);
     522    }
     523
     524    foreach ($terms_hash as $term) {
     525      $root_term = $term;
     526      while ($root_term->parent) {
     527        $root_term = $terms_hash[$root_term->parent];
     528      }
     529
     530      if ($sync_categories_mode === 'whitelist' && !in_array($root_term->term_id, $sync_categories_list)) {
     531        $term->root_term_id = null;
     532        continue;
     533      } else if ($sync_categories_mode === 'blacklist' && in_array($root_term->term_id, $sync_categories_list)) {
     534        $term->root_term_id = null;
     535        continue;
     536      }
     537
     538      if ($root_term->term_id !== $term->term_id) {
     539        $term->root_term_id = $root_term->term_id;
     540      } else {
     541        $term->root_term_id = $term->parent;
     542      }
     543    }
     544
    522545    foreach ($posts_add_to_api as $postArrayId => $post) {
    523546      Crowdaa_Sync_Timer::check();
     
    531554        Crowdaa_Sync_Logs::log('No categories for this post, skipping...');
    532555        continue;
    533       } else {
    534         foreach ($post_terms as $term) {
    535           if (in_array($term->term_id, $child_terms)) {
    536             $child_term = true;
    537             $post_term  = [
    538               'id'   => $term->term_id,
    539               'slug' => $term->slug,
    540               'name' => $term->name,
    541             ];
     556      }
     557
     558      $whitelisted = ($sync_categories_mode !== 'whitelist');
     559      $blacklisted = false;
     560      foreach ($post_terms as $term) {
     561        $blacklisted_term = false;
     562
     563        if (in_array($term->term_id, $sync_categories_list)) {
     564          if ($sync_categories_mode === 'blacklist') {
     565            $blacklisted = $term->term_id;
     566          } else {
     567            $whitelisted = $term->term_id;
     568            if (!$post_term) {
     569              $child_term = true; // Lie here to avoid losing this term.
     570              $post_term  = [
     571                'id'   => $term->term_id,
     572                'slug' => $term->slug,
     573                'name' => $term->name,
     574              ];
     575            }
    542576          }
    543         }
    544         if ($child_term == false) {
     577        } else if (
     578          $sync_categories_mode !== 'whitelist' &&
     579          !$blacklisted &&
     580          !$post_term &&
     581          in_array($term->term_id, $child_terms)
     582        ) {
     583          $child_term = true;
    545584          $post_term  = [
    546             'id'   => $post_terms[0]->term_id,
    547             'slug' => $post_terms[0]->slug,
    548             'name' => $post_terms[0]->name,
    549           ];
    550         }
    551 
    552         $term = $terms_hash[$post_term['id']];
    553         if ($term->root_term_id) {
    554           $term = $terms_hash[$term->root_term_id];
    555           $post_term['parent'] = [
    556585            'id'   => $term->term_id,
    557586            'slug' => $term->slug,
     
    561590      }
    562591
     592      if (!$whitelisted) {
     593        Crowdaa_Sync_Logs::log('Could not find a whitelisted category for this article, skipping');
     594        $api_post_id = get_post_meta($post['post_id'], 'api_post_id');
     595        if ($api_post_id) {
     596          Crowdaa_Sync_Logs::log('Post was already synchronized, removing it from the API');
     597          $this->remove_already_synced_post($post['post_id']);
     598        }
     599
     600        continue;
     601      } else if ($blacklisted) {
     602        Crowdaa_Sync_Logs::log('Blacklisted category', $blacklisted, 'skipping');
     603        $api_post_id = get_post_meta($post['post_id'], 'api_post_id');
     604        if ($api_post_id) {
     605          Crowdaa_Sync_Logs::log('Post was already synchronized, removing it from the API');
     606          $this->remove_already_synced_post($post['post_id']);
     607        }
     608
     609        continue;
     610      }
     611
     612      if ($child_term == false) {
     613        $post_term  = [
     614          'id'   => $post_terms[0]->term_id,
     615          'slug' => $post_terms[0]->slug,
     616          'name' => $post_terms[0]->name,
     617        ];
     618      }
     619
     620      $term = $terms_hash[$post_term['id']];
     621      if ($term->root_term_id) {
     622        $term = $terms_hash[$term->root_term_id];
     623        $post_term['parent'] = [
     624          'id'   => $term->term_id,
     625          'slug' => $term->slug,
     626          'name' => $term->name,
     627        ];
     628      }
     629
    563630      try {
     631        $can_sync = apply_filters('crowdaa_sync_can_sync_wp_post', true, $post['post_id']);
     632        if (!$can_sync) {
     633          Crowdaa_Sync_Logs::log('Post sync denied by "crowdaa_sync_can_sync_wp_post" filter');
     634          $api_post_id = get_post_meta($post['post_id'], 'api_post_id');
     635          if ($api_post_id) {
     636            Crowdaa_Sync_Logs::log('Post was already synchronized, removing it from the API');
     637            $this->remove_already_synced_post($post['post_id']);
     638          }
     639
     640          continue;
     641        }
     642
    564643        // Create or update parent category, if needed
    565644        $parent_api_term_id = false;
     
    607686        $img_errors = $sync_api->sync_post_images($post['post_id']);
    608687        if($img_errors) {
    609           $errors += $img_errors;
     688          throw new Crowdaa_Sync_Post_Skip_Error('Medias sync errors, skipping ('.implode(', ', $img_errors).')');
    610689        }
    611690
     
    625704        continue;
    626705      } catch (Crowdaa_Sync_Post_Error $e) {
     706        Crowdaa_Sync_Logs::log('Post sync error', $e->getMessage());
    627707        $errors[] = $e->getMessage();
    628708        continue;
  • crowdaa-sync/trunk/admin/class-crowdaa-sync-admin-display.php

    r2620193 r2722017  
    268268
    269269  /**
     270   * Set which categories will be synchronized or not on the app
     271   *
     272   * @since    1.0.0
     273   */
     274  public function choose_sync_categories() {
     275    if(!isset($_POST['crowdaa_set_sync_categories']) || !wp_verify_nonce($_POST['crowdaa_set_sync_categories'], 'crowdaa_set_sync_categories_data')) {
     276      return;
     277    }
     278
     279    if(isset($_POST['sync_categories_mode_whitelist'])) {
     280      update_option('crowdaa_sync_categories_mode', 'whitelist');
     281    } else {
     282      update_option('crowdaa_sync_categories_mode', 'blacklist');
     283    }
     284
     285    $categories = array_filter($_POST['sync_categories'], function($v) {
     286      if (empty($v)) return (false);
     287      $v = (string) $v;
     288      if (!preg_match('/^[0-9]+$/', $v)) return (false);
     289
     290      return (true);
     291    });
     292
     293    update_option('crowdaa_sync_categories_list', implode(',', $categories));
     294
     295    Crowdaa_Sync_Versions::bump_version();
     296  }
     297
     298  /**
    270299   * Admin notices
    271300   *
  • crowdaa-sync/trunk/admin/class-crowdaa-sync-api.php

    r2620193 r2722017  
    9696   */
    9797  private function fetch_post_api_medias($wp_post_id) {
     98    $captions = [];
    9899    $api_feedpicture_id = get_post_meta($wp_post_id, 'api_feedpicture_id', true) ?: '';
    99100    if ($api_feedpicture_id) {
    100101      $api_feedpicture_id = unserialize($api_feedpicture_id);
     102      $captions[] = wp_get_attachment_caption($api_feedpicture_id['attachment_id']);
    101103      $api_feedpicture_id = $api_feedpicture_id['api_id'];
    102104    }
     
    112114        $pictures_id[] = $api_id;
    113115      }
    114     }
     116      $captions[] = wp_get_attachment_caption($data['attachment_id']);
     117    }
     118
     119    $captions = array_filter($captions, 'strlen');
    115120
    116121    return ([
    117122      'api_feedpicture_id' => $api_feedpicture_id,
    118       'videos_id' => $videos_id,
    119       'pictures_id' => $pictures_id,
     123      'videos_id'          => $videos_id,
     124      'pictures_id'        => $pictures_id,
     125      'captions'           => $captions,
    120126    ]);
    121127  }
     
    154160
    155161    $post_content = get_post_field('post_content', $wp_post_id);
     162    $post_author = get_the_author_meta('display_name', get_post_field('post_author', $wp_post_id));
    156163    $publication_time = get_post_datetime($wp_post_id)->getTimestamp();
    157164
     
    159166    $wp_post_title = html_entity_decode($wp_post_title, ENT_QUOTES);
    160167    $data = [
    161       'articleId'    => $api_post_id,
    162       'actions'      => [],
    163       'categoryId'   => $category_id,
    164       'feedPicture'  => $api_feedpicture_id,
    165       'md'           => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
    166       'pictures'     => $pictures_id,
    167       'summary'      => $wp_post_title,
    168       'hideFromFeed' => !$feed_display,
    169       'productId'    => '',
    170       'title'        => $wp_post_title,
    171       'videos'       => $videos_id,
     168      'articleId'     => $api_post_id,
     169      'actions'       => [],
     170      'authorName'    => $post_author,
     171      'categoryId'    => $category_id,
     172      'feedPicture'   => $api_feedpicture_id,
     173      'md'            => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
     174      'pictures'      => $pictures_id,
     175      'summary'       => $wp_post_title,
     176      'hideFromFeed'  => !$feed_display,
     177      'productId'     => '',
     178      'title'         => $wp_post_title,
     179      'videos'        => $videos_id,
     180      'mediaCaptions' => implode(' | ', $api_media_data['captions']),
    172181    ];
     182    $data = apply_filters('crowdaa_sync_api_update_article_payload', $data, $wp_post_id);
    173183
    174184    $response = $this->http_request('PUT', '/press/articles', $data);
     
    187197      }
    188198
    189       $publish_error = $this->publish_post_draft_api($json->draftId, $api_post_id, $publication_time);
     199      $send_notification = (
     200        get_post_meta($wp_post_id, 'crowdaa_notification_send', true) === 'yes' &&
     201        get_post_meta($wp_post_id, 'crowdaa_notification_sent', true) !== 'yes'
     202      );
     203      $publish_error = $this->publish_post_draft_api(
     204        $json->draftId,
     205        $api_post_id,
     206        $publication_time,
     207        $send_notification,
     208        (get_post_meta($wp_post_id, 'crowdaa_notification_content', true) ?: null),
     209        (get_post_meta($wp_post_id, 'crowdaa_notification_title', true) ?: null),
     210      );
    190211      if ($publish_error) {
    191212        Crowdaa_Sync_Logs::log('Article publish error', $api_post_id, $publish_error);
    192213        throw new Crowdaa_Sync_Post_Error(__('Post publish error : ', CROWDAA_SYNC_PLUGIN_NAME).$publish_error);
     214      }
     215
     216      if ($send_notification) {
     217        update_post_meta($wp_post_id, 'crowdaa_notification_sent', 'yes');
    193218      }
    194219
     
    206231   * @return boolean
    207232   */
    208   public function publish_post_draft_api($draft_id, $api_post_id, $publication_time) {
     233  public function publish_post_draft_api(
     234    $draft_id,
     235    $api_post_id,
     236    $publication_time,
     237    $send_notification = false,
     238    $notification_content = null,
     239    $notification_title = null
     240  ) {
    209241    $data = [
    210       'draftId'           => $draft_id,
    211       'date'              => gmdate(CROWDAA_SYNC_ISO_TIME_FORMAT, $publication_time),
    212       'sendNotifications' => false,
     242      'draftId'             => $draft_id,
     243      'date'                => gmdate(CROWDAA_SYNC_ISO_TIME_FORMAT, $publication_time),
     244      'sendNotifications'   => $send_notification,
     245      'notificationContent' => $notification_content,
     246      'notificationTitle'   => $notification_title,
    213247    ];
    214248
     
    239273   * @param $article_data
    240274   */
    241   public function create_custom_article_api($article_data, $publication_time) {
     275  public function create_custom_article_api($article_data, $publication_time, $send_notification = false) {
    242276    $auth_token = get_option('crowdaa_auth_token');
    243277    if (!$auth_token) {
     
    277311      }
    278312
    279       $publish_error = $this->publish_post_draft_api($json->draftId, $json->articleId, $publication_time);
     313      $publish_error = $this->publish_post_draft_api(
     314        $json->draftId,
     315        $json->articleId,
     316        $publication_time,
     317        $send_notification
     318      );
    280319      if ($publish_error) {
    281320        Crowdaa_Sync_Logs::log('Custom article first publish error', $json->articleId, $publish_error);
     
    295334   * @param $article_data
    296335   */
    297   public function update_custom_article_api($article_data, $publication_time) {
     336  public function update_custom_article_api($article_data, $publication_time, $send_notification = false) {
    298337    $auth_token = get_option('crowdaa_auth_token');
    299338    if (!$auth_token) {
     
    333372      }
    334373
    335       $publish_error = $this->publish_post_draft_api($json->draftId, $json->articleId, $publication_time);
     374      $publish_error = $this->publish_post_draft_api(
     375        $json->draftId,
     376        $json->articleId,
     377        $publication_time,
     378        $send_notification
     379      );
    336380      if ($publish_error) {
    337381        Crowdaa_Sync_Logs::log('Custom article publish error', $json->articleId, $publish_error);
     
    377421
    378422    $post_content = get_post_field('post_content', $wp_post_id);
     423    $post_author = get_the_author_meta('display_name', get_post_field('post_author', $wp_post_id));
    379424    $publication_time = get_post_datetime($wp_post_id)->getTimestamp();
    380425
     
    382427    $wp_post_title = html_entity_decode($wp_post_title, ENT_QUOTES);
    383428    $data = [
    384       'actions'      => [],
    385       'categoryId'   => $category_id,
    386       'feedPicture'  => $api_feedpicture_id,
    387       'md'           => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
    388       'pictures'     => $pictures_id,
    389       'summary'      => $wp_post_title,
    390       'hideFromFeed' => !$feed_display,
    391       'productId'    => '',
    392       'title'        => $wp_post_title,
    393       'videos'       => $videos_id,
     429      'actions'       => [],
     430      'authorName'    => $post_author,
     431      'categoryId'    => $category_id,
     432      'feedPicture'   => $api_feedpicture_id,
     433      'md'            => $post_content ?: __('Without content', CROWDAA_SYNC_PLUGIN_NAME),
     434      'pictures'      => $pictures_id,
     435      'summary'       => $wp_post_title,
     436      'hideFromFeed'  => !$feed_display,
     437      'productId'     => '',
     438      'title'         => $wp_post_title,
     439      'videos'        => $videos_id,
     440      'mediaCaptions' => implode(' | ', $api_media_data['captions']),
    394441    ];
     442    $data = apply_filters('crowdaa_sync_api_create_article_payload', $data, $wp_post_id);
    395443
    396444    Crowdaa_Sync_Logs::log('Creating API post', $wp_post_id, wp_json_encode($data));
     
    414462      }
    415463
    416       $publish_error = $this->publish_post_draft_api($json->draftId, $json->articleId, $publication_time);
     464      $send_notification = (
     465        get_post_meta($wp_post_id, 'crowdaa_notification_send', true) === 'yes' &&
     466        get_post_meta($wp_post_id, 'crowdaa_notification_sent', true) !== 'yes'
     467      );
     468      $publish_error = $this->publish_post_draft_api(
     469        $json->draftId,
     470        $json->articleId,
     471        $publication_time,
     472        $send_notification,
     473        (get_post_meta($wp_post_id, 'crowdaa_notification_content', true) ?: null),
     474        (get_post_meta($wp_post_id, 'crowdaa_notification_title', true) ?: null),
     475      );
    417476      if ($publish_error) {
    418477        Crowdaa_Sync_Logs::log('Article first publish error', $json->articleId, $json->draftId, $publish_error);
     
    421480
    422481      update_post_meta($wp_post_id, 'api_post_id', $json->articleId);
     482      if ($send_notification) {
     483        update_post_meta($wp_post_id, 'crowdaa_notification_sent', 'yes');
     484      }
    423485      Crowdaa_Sync_Logs::log('Created API post', $json->articleId);
    424486    }
     
    747809      'action'   => '',
    748810    ];
     811    $data = apply_filters('crowdaa_sync_api_create_category_payload', $data);
    749812
    750813    $response = $this->http_request('POST', '/press/categories', $data);
     
    789852      'name'     => $cat_name,
    790853      'pathName' => $cat_slug,
    791       'color'    => '',
    792       'order'    => 1,
    793854      'hidden'   => false,
    794       'picture'  => [],
    795855      'parentId' => $parent_category_id,
    796856      'action'   => '',
     857      // 'picture'  => null,
     858      // 'color'    => null,
     859      // 'order'    => null,
    797860    ];
     861    $data = apply_filters('crowdaa_sync_api_update_category_payload', $data);
    798862
    799863    $response = $this->http_request('PUT', '/press/categories/'.$cat_id, $data);
     
    829893   */
    830894  public function delete_post_api($api_post_id) {
    831     $data = [
    832       'draftId' => $api_post_id,
    833     ];
    834 
    835     $response = $this->http_request('DELETE', '/press/articles/'.$api_post_id, $data);
     895    $response = $this->http_request('DELETE', '/press/articles/'.$api_post_id);
    836896    $err    = is_wp_error($response) ? $response->get_error_message() : null;
    837897    if (!$err) {
  • crowdaa-sync/trunk/admin/class-crowdaa-sync-meta-box.php

    r2620193 r2722017  
    1818   */
    1919  public function __construct() {
    20     add_action('add_meta_boxes', [$this, 'meta_box_add'], 10, 2);
    21     add_action('save_post', [$this, 'img_gallery_save']);
     20    add_action('add_meta_boxes', [$this, 'add_meta_boxes'], 10, 2);
     21    add_action('save_post', [$this, 'save_meta_boxes']);
    2222  }
    2323
     
    2929   * @return boolean
    3030   */
    31   function meta_box_add() {
     31  function add_meta_boxes() {
    3232    add_meta_box(
    3333      'crowdaa_feat_img_slider',
    34       'Featured Image Gallery',
    35       [$this, 'print_box'],
     34      'Crowdaa-sync: Featured Image Gallery',
     35      [$this, 'print_feat_img_slider_box'],
     36      'post',
     37      'advanced',
     38      'high',
     39      array(
     40        '__back_compat_meta_box' => false,
     41      )
     42    );
     43    add_meta_box(
     44      'crowdaa-sync-notification-send',
     45      'Crowdaa-sync: Send notifications when published?',
     46      [$this, 'print_send_notification_checkbox'],
    3647      'post',
    3748      'advanced',
     
    124135
    125136  /**
    126    * Meta box html
     137   * Image selector meta box
    127138   *
    128139   * @since    1.0.0
     
    130141   * @return string
    131142   */
    132   function print_box($post) {
     143  function print_feat_img_slider_box($post) {
    133144    wp_nonce_field('save_feat_gallery', 'feat_gallery_nonce');
    134145
     
    138149
    139150  /**
    140    * Save meta box data
    141    *
    142    * @since    1.0.0
    143    * @param $post_id
    144    * @return boolean
    145    */
    146   function img_gallery_save($post_id) {
     151   * Notification checkbox meta box
     152   *
     153   * @since    1.0.0
     154   * @param $post
     155   * @return string
     156   */
     157  function print_send_notification_checkbox($post) {
     158    $notification_content = get_post_meta($post->ID, 'crowdaa_notification_content', true) ?: '';
     159    $notification_title = get_post_meta($post->ID, 'crowdaa_notification_title', true) ?: '';
     160    wp_nonce_field('crowdaa_notification_checkbox', 'crowdaa_notification_checkbox_nonce');
     161
     162    ?>
     163    <p>
     164      <i>
     165        <?php
     166          esc_html_e('Check this box if you want to send a notification when the article is published or right now if it is already published.', CROWDAA_SYNC_PLUGIN_NAME);
     167        ?>
     168      </i>
     169    </p>
     170
     171    <div class="crowdaa-meta-box-input-row">
     172      <label>
     173        <?php
     174          esc_html_e('Send notification :', CROWDAA_SYNC_PLUGIN_NAME);
     175        ?>
     176        <input
     177          type="checkbox"
     178          name="crowdaa_send_post_notification"
     179        />
     180      </label>
     181    </div>
     182    <div class="crowdaa-meta-box-input-row">
     183      <label>
     184        <?php
     185          esc_html_e('Notification text (When both are not set, defaults to the article title & first characters) :', CROWDAA_SYNC_PLUGIN_NAME);
     186        ?>
     187        <br />
     188        <input
     189          style="display: inline-block; width: 500px;"
     190          type="text"
     191          name="crowdaa_send_post_notification_title"
     192          placeholder="<?php esc_attr_e('Notification title', CROWDAA_SYNC_PLUGIN_NAME); ?>"
     193          value="<?php echo esc_attr($notification_title); ?>"
     194        />
     195        <br />
     196        <input
     197          style="display: inline-block; width: 500px;"
     198          type="text"
     199          name="crowdaa_send_post_notification_content"
     200          placeholder="<?php esc_attr_e('Notification content', CROWDAA_SYNC_PLUGIN_NAME); ?>"
     201          value="<?php echo esc_attr($notification_content); ?>"
     202        />
     203      </label>
     204    </div>
     205    <?php
     206  }
     207
     208  function save_gallery_meta_box($post_id) {
    147209    if (!isset($_POST['feat_gallery_nonce'])) {
    148       return $post_id;
    149     }
    150 
    151     if (!wp_verify_nonce($_POST['feat_gallery_nonce'], 'save_feat_gallery')) {
    152       return $post_id;
     210      return;
     211    } else if (!wp_verify_nonce($_POST['feat_gallery_nonce'], 'save_feat_gallery')) {
     212      return;
     213    } else if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
     214      return;
     215    } else if (!current_user_can('edit_post', $post_id)) {
     216      return;
    153217    }
    154218
     
    167231    }
    168232  }
     233
     234  function save_notifications_meta_box($post_id) {
     235    if (!isset($_POST['crowdaa_notification_checkbox_nonce'])) {
     236      return;
     237    } else if (!wp_verify_nonce($_POST['crowdaa_notification_checkbox_nonce'], 'crowdaa_notification_checkbox')) {
     238      return;
     239    } else if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
     240      return;
     241    } else if (!current_user_can('edit_post', $post_id)) {
     242      return;
     243    }
     244
     245    if (isset($_POST['crowdaa_send_post_notification'])) {
     246      update_post_meta($post_id, 'crowdaa_notification_send', 'yes');
     247      update_post_meta($post_id, 'crowdaa_notification_sent', 'no');
     248      update_post_meta($post_id, 'crowdaa_notification_title', $_POST['crowdaa_send_post_notification_title'] ?: '');
     249      update_post_meta($post_id, 'crowdaa_notification_content', $_POST['crowdaa_send_post_notification_content'] ?: '');
     250    }
     251  }
     252
     253  /**
     254   * Save meta boxes data
     255   *
     256   * @since    1.0.0
     257   * @param $post_id
     258   * @return boolean
     259   */
     260  function save_meta_boxes($post_id) {
     261    $this->save_gallery_meta_box($post_id);
     262    $this->save_notifications_meta_box($post_id);
     263  }
    169264}
  • crowdaa-sync/trunk/admin/css/crowdaa-sync-admin.css

    r2620193 r2722017  
    3131  display: flex;
    3232  flex-direction: row;
    33   max-width: 310px;
     33  justify-content: space-between;
     34  max-width: 400px;
    3435}
    3536
     
    3940
    4041.sync-form-label {
    41   width: 250px;
     42  width: 300px;
    4243  display: inline-block;
    4344}
     
    4748}
    4849
    49 .sync-feed-categories-select {
     50.sync-categories-select {
    5051  min-height: 150px !important;
    5152  width: 600px;
     
    6970  position: absolute;
    7071  top: -7px;
     72  right: 0px;
    7173  height: 30px;
    7274  line-height: 30px;
     
    8385  content: "ON";
    8486  color: #0a0;
     87}
     88
     89.sync-enable-checkbox-custom {
     90  visibility: hidden;
     91  cursor: pointer;
     92}
     93
     94.sync-enable-checkbox-custom+.sync-enable-checkbox-custom-content {
     95  display: block;
     96  height: 30px;
     97  line-height: 30px;
     98  width: auto;
     99  padding: 0 10px;
     100  text-align: center;
     101  border-radius: 4px;
     102  background: #fff;
     103  color: rgb(4, 174, 216);
     104  font-weight: 600;
     105  cursor: pointer;
     106}
     107
     108.sync-enable-checkbox-custom:checked+.sync-enable-checkbox-custom-content {
     109  color: rgb(255, 167, 0);
     110}
     111
     112.sync-enable-checkbox-custom+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-checked {
     113  display: none;
     114}
     115
     116.sync-enable-checkbox-custom+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-unchecked {
     117  display: inline;
     118}
     119
     120.sync-enable-checkbox-custom:checked+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-unchecked {
     121  display: none;
     122}
     123
     124.sync-enable-checkbox-custom:checked+.sync-enable-checkbox-custom-content .sync-enable-checkbox-custom-checked {
     125  display: inline;
    85126}
    86127
     
    177218  padding: 5px 10px;
    178219}
     220
     221.crowdaa-meta-box-input-row {
     222  display: block;
     223  margin-bottom: 10px;
     224}
  • crowdaa-sync/trunk/admin/js/crowdaa-sync-admin.js

    r2620193 r2722017  
    9191    updateFeedCategoriesSelect();
    9292
     93    function updateSyncCategoriesSelect() {
     94      const $field = $('#crowdaa-sync-categories-mode-whitelist-checkbox');
     95      const $whitelistText = $('#crowdaa-sync-categories-explanation-whitelist');
     96      const $blacklistText = $('#crowdaa-sync-categories-explanation-blacklist');
     97      if ($field.prop('checked')) {
     98        $whitelistText.show();
     99        $blacklistText.hide();
     100      } else {
     101        $whitelistText.hide();
     102        $blacklistText.show();
     103      }
     104    }
     105    $('#crowdaa-sync-categories-mode-whitelist-checkbox').on('change', updateSyncCategoriesSelect);
     106    updateSyncCategoriesSelect();
     107
    93108    /**
    94109     * Ajax add posts and terms
  • crowdaa-sync/trunk/admin/partials/crowdaa-sync-admin-display.php

    r2620193 r2722017  
    115115        <?php do_action('crowdaa_sync_configuration_levers'); ?>
    116116        <label class="sync-form-checkbox-label">
    117           <span class="sync-form-label"><?php esc_html_e('Maximum synchronization duration'); ?></span> &nbsp;
     117          <span class="sync-form-label"><?php esc_html_e('Maximum synchronization duration (minutes)'); ?></span> &nbsp;
    118118          <input type="number" name="sync_max_duration" min="2" id="sync-duration-field"
    119119            value="<?php echo esc_attr($sync_max_duration); ?>"
    120120          />
    121           <?php esc_html_e('minutes'); ?>
    122121        </label>
    123122        <br />
     
    147146        <?php
    148147        submit_button('Save picture');
     148        ?>
     149      </form>
     150    </div>
     151
     152    <hr/>
     153
     154    <div>
     155      <h2><?php esc_html_e('Set categories to synchronize', CROWDAA_SYNC_PLUGIN_NAME) ?></h2>
     156      <form method="post" action="<?php $admin_utils->choose_sync_categories(); ?>"  enctype="multipart/form-data">
     157        <?php
     158          $sync_categories_mode = get_option('crowdaa_sync_categories_mode', 'blacklist');
     159          $sync_categories_list = explode(',', get_option('crowdaa_sync_categories_list', ''));
     160        ?>
     161        <label class="sync-form-checkbox-label">
     162          <span class="sync-form-label"><?php esc_html_e('Categories sync mode', CROWDAA_SYNC_PLUGIN_NAME); ?></span> &nbsp;
     163          <input
     164            type="checkbox"
     165            name="sync_categories_mode_whitelist"
     166            class="sync-enable-checkbox-custom"
     167            id="crowdaa-sync-categories-mode-whitelist-checkbox"
     168            <?php echo ($sync_categories_mode === 'whitelist' ? 'checked="checked"' : ''); ?>
     169          />
     170          <span class="sync-enable-checkbox-custom-content">
     171            <span class="sync-enable-checkbox-custom-checked">Whitelist</span>
     172            <span class="sync-enable-checkbox-custom-unchecked">Blacklist</span>
     173          </span>
     174        </label>
     175        <div id="crowdaa-sync-categories-select-area">
     176          <label>
     177            <p>
     178              <span id="crowdaa-sync-categories-explanation-whitelist">
     179                <?php esc_html_e('Select below the categories that you *want* to synchronize to the app.', CROWDAA_SYNC_PLUGIN_NAME); ?>
     180              </span>
     181              <span id="crowdaa-sync-categories-explanation-blacklist">
     182                <?php esc_html_e('Select below the categories that you *do not want* to synchronize to the app.', CROWDAA_SYNC_PLUGIN_NAME); ?>
     183              </span>
     184              <br />
     185              <?php esc_html_e('Use the Ctrl or Shift keys to select multiple categories.', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
     186            </p>
     187            <?php
     188              $terms = get_terms([
     189                "hide_empty" => false,
     190                "taxonomy" => "category",
     191              ]);
     192            ?>
     193            <select class="sync-categories-select" name="sync_categories[]" multiple>
     194                <option
     195                  value=""
     196                  <?php echo (array_search('', $sync_categories_list) !== false ? 'selected="selected"' : ''); ?>
     197                >
     198                  <?php esc_html_e('---', CROWDAA_SYNC_PLUGIN_NAME); ?>
     199                </option>
     200              <?php foreach ($terms as $term) { ?>
     201                <option
     202                  value="<?php echo esc_attr($term->term_id); ?>"
     203                  <?php echo (array_search($term->term_id, $sync_categories_list) !== false ? 'selected="selected"' : ''); ?>
     204                >
     205                  <?php esc_html_e($term->name.' ('.$term->slug.')', CROWDAA_SYNC_PLUGIN_NAME); ?>
     206                </option>
     207              <?php } ?>
     208            </select>
     209          </label>
     210        </div>
     211        <?php wp_nonce_field('crowdaa_set_sync_categories_data', 'crowdaa_set_sync_categories'); ?>
     212        <?php
     213        submit_button('Save mode & categories');
    149214        ?>
    150215      </form>
     
    173238            <p>
    174239              <?php esc_html_e('Select below the categories that you want to see on the feed page.', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
    175               <?php esc_html_e('Use the Ctrl or Shift keys to select multiple categories', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
     240              <?php esc_html_e('Use the Ctrl or Shift keys to select multiple categories.', CROWDAA_SYNC_PLUGIN_NAME); ?><br />
    176241            </p>
    177242            <?php
     
    179244                "hide_empty" => false,
    180245                "taxonomy" => "category",
    181                 "childless" => true
    182246              ]);
    183247              if ($feed_categories === 'all') {
     
    187251              }
    188252            ?>
    189             <select class="sync-feed-categories-select" name="sync_feed_categories[]" multiple>
    190                 <option value="">
    191                   <?php esc_html_e('None', CROWDAA_SYNC_PLUGIN_NAME); ?>
     253            <select class="sync-categories-select" name="sync_feed_categories[]" multiple>
     254                <option
     255                  value=""
     256                  <?php echo (array_search('', $feed_categories) !== false ? 'selected="selected"' : ''); ?>
     257                >
     258                  <?php esc_html_e('---', CROWDAA_SYNC_PLUGIN_NAME); ?>
    192259                </option>
    193260              <?php foreach ($terms as $term) { ?>
  • crowdaa-sync/trunk/crowdaa-sync.php

    r2620193 r2722017  
    1414 * Plugin URI:       
    1515 * Description:       Plugin for synchronizing WordPress site and Crowdaa CMS
    16  * Version:           1.0.50
     16 * Version:           1.1.0
    1717 * Requires at least: 5.5
    1818 * Requires PHP:      7.2
     
    3434 * Uses SemVer - https://semver.org
    3535 */
    36 define('CROWDAA_SYNC_VERSION', '1.0.50');
     36define('CROWDAA_SYNC_VERSION', '1.1.0');
    3737define('CROWDAA_SYNC_PLUGIN_DIR', __DIR__);
    3838define('CROWDAA_SYNC_PLUGIN_NAME', 'crowdaa-sync');
     
    4848$last_version = get_option('crowdaa_last_version', false);
    4949if (!$last_version || $last_version < CROWDAA_SYNC_META_VERSION) {
    50   update_option('crowdaa_sync_wp_to_api_from', 0);
     50  update_option('crowdaa_sync_articles_wp_to_api_from', 0);
    5151  update_option('crowdaa_last_version', CROWDAA_SYNC_META_VERSION);
    5252}
     
    111111  }
    112112
     113  if (class_exists('Crowdaa_Sync_Ext_Hooks')) {
     114    new Crowdaa_Sync_Ext_Hooks;
     115  }
     116
    113117  if (class_exists('Crowdaa_Sync_Admin_Display')) {
    114118    new Crowdaa_Sync_Admin_Display;
  • crowdaa-sync/trunk/includes/class-crowdaa-sync-versions.php

    r2620193 r2722017  
    1919    if (!$last_version || $last_version < CROWDAA_SYNC_META_VERSION) {
    2020      update_option('crowdaa_last_version', CROWDAA_SYNC_META_VERSION);
    21       update_option('crowdaa_sync_wp_to_api_from', 0);
     21      update_option('crowdaa_sync_articles_wp_to_api_from', 0);
    2222      update_option('crowdaa_sync_internal_version', '0');
    2323    }
     
    3636    update_option('crowdaa_sync_internal_version', "$ver");
    3737
    38     update_option('crowdaa_sync_wp_to_api_from', 0);
     38    update_option('crowdaa_sync_articles_wp_to_api_from', 0);
    3939
    4040    self::update_versions();
  • crowdaa-sync/trunk/includes/class-crowdaa-sync.php

    r2620193 r2722017  
    180180    require_once CROWDAA_SYNC_PLUGIN_DIR.'/admin/class-crowdaa-sync-wp-hooks.php';
    181181
     182    /**
     183     * The class for other plugin hooks
     184     */
     185    require_once CROWDAA_SYNC_PLUGIN_DIR.'/admin/class-crowdaa-sync-ext-hooks.php';
     186
    182187    $this->loader = new Crowdaa_Sync_Loader();
    183188  }
Note: See TracChangeset for help on using the changeset viewer.