/**
  * Hooks the Babble bbl_pre_sync_properties filter to
  * log any changes to parent. We're not making changes
  * to the data, just logging significant changes for
  * debug purposes.
  *
  * @param array $postdata The data which will be applied to the post as part of the sync
  * @param int $origin_id The ID of the post we are syncing from
  * @return array The data which will be applied to the post as part of the sync
  **/
 public function pre_sync_properties($postdata, $origin_id)
 {
     $current_post = get_post($postdata['ID']);
     if ($current_post->post_parent != $postdata['post_parent']) {
         $user = wp_get_current_user();
         $remote_ip = $_SERVER['REMOTE_ADDR'];
         $referer = $_SERVER['HTTP_REFERER'];
         $lang = bbl_get_current_lang_code();
         $origin_lang = bbl_get_post_lang_code($origin_id);
         bbl_log("Babble: {$user->user_login} has changed {$postdata['ID']} parent from {$current_post->post_parent} ({$current_post->post_type}) to {$postdata['post_parent']}. \tOrigin: {$origin_id}. Origin lang: {$origin_lang}. IP {$remote_ip}. User lang: {$lang}. Referer {$referer}.", true);
     }
     return $postdata;
 }
 /**
  * Checks the DB structure is up to date.
  *
  * @return void
  * @author Simon Wheatley
  **/
 protected function maybe_update()
 {
     $option_name = 'bbl-locale-version';
     $version = get_option($option_name, 0);
     if ($this->version == $version) {
         return;
     }
     if ($version < 1) {
         bbl_log("Babble Locale: Flushing rewrite rules", true);
         flush_rewrite_rules();
     }
     bbl_log("Babble Locale: Done updates", true);
     update_option($option_name, $this->version);
 }
 /**
  * Checks the DB structure is up to date, rewrite rules,
  * theme image size options are set, etc.
  *
  * @return void
  **/
 protected function maybe_upgrade()
 {
     $option_name = 'bbl_post_public_version';
     $version = get_option($option_name, 0);
     if ($version == $this->version) {
         return;
     }
     if ($start_time = get_option("{$option_name}_running", false)) {
         $time_diff = time() - $start_time;
         // Check the lock is less than 30 mins old, and if it is, bail
         if ($time_diff < 60 * 30) {
             bbl_log("Babble Post Public: Existing update routine has been running for less than 30 minutes", true);
             return;
         }
         bbl_log("Babble Post Public: Update routine is running, but older than 30 minutes; going ahead regardless", true);
     } else {
         add_option("{$option_name}_running", time(), null, 'no');
     }
     if ($version < 9) {
         bbl_log("Babble Post Public: Start pruning metadata", true);
         $this->prune_post_meta();
         bbl_log("Babble Post Public: Remove excess post meta", true);
     }
     // N.B. Remember to increment $this->version above when you add a new IF
     update_option($option_name, $this->version);
     delete_option("{$option_name}_running", true, null, 'no');
     bbl_log("Babble Post Public: Done upgrade, now at version " . $this->version, true);
 }
 /**
  * Renders an admin template from this plugin's /templates-admin/ directory.
  *
  * @return void
  * @author Simon Wheatley
  **/
 protected function render_admin($template_file, $vars = null)
 {
     // Plus our specific template vars
     if (is_array($vars)) {
         extract($vars);
     }
     // Try to render
     if (file_exists($this->dir("templates-admin/" . sanitize_file_name($template_file)))) {
         require $this->dir("templates-admin/" . sanitize_file_name($template_file));
     } else {
         $msg = __("This plugin admin template could not be found: %s");
         bbl_log("Plugin template error: " . sprintf($msg, $this->dir("templates-admin/" . sanitize_file_name($template_file))), true);
         echo "<p style='background-color: #ffa; border: 1px solid red; color: #300; padding: 10px;'>" . esc_html(sprintf($msg, "templates-admin/" . sanitize_file_name($template_file))) . "</p>";
     }
 }
 /**
  * Set the translation group ID (a term ID) that the given term ID 
  * belongs to.
  *
  * @param int $target_term_id The term ID to set the translation group for
  * @param int $translation_group_id The ID of the translation group to add this 
  * @return int|false The transID the target term belongs to, false on failure
  **/
 public function set_transid($target_term_id, $transid = null)
 {
     if (!$target_term_id) {
         return false;
     }
     if (!$transid) {
         $transid_name = 'term_transid_' . uniqid();
         $result = wp_insert_term($transid_name, 'term_translation', array());
         if (is_wp_error($result)) {
             bbl_log("Problem creating a new Term TransID: " . print_r($result, true), true);
         } else {
             $transid = $result['term_id'];
         }
     }
     $result = wp_set_object_terms($target_term_id, absint($transid), 'term_translation');
     if (is_wp_error($result)) {
         bbl_log("Problem associating TransID with new posts: " . print_r($result, true), true);
     }
     wp_cache_delete($target_term_id, 'bbl_term_transids');
     return $transid;
 }