예제 #1
0
/**
 * Update the additional meta for a post ID.
 *
 * @since 1.1
 * @param int     $post_id  Post ID
 * @param [type]  $new_meta New additional meta to update
 * @return void
 */
function update_additional_meta($post_id, $new_meta)
{
    $old_meta = get_post_meta($post_id, '_ac_additional_content', true);
    if (!(!empty($new_meta) && is_array($new_meta))) {
        if ($old_meta) {
            // Delete old meta if new meta is empty.
            delete_post_meta($post_id, '_ac_additional_content');
        }
        return;
    }
    $defaults = get_defaults();
    // Validate the new settings
    foreach ($new_meta as $key => $setting) {
        if (!is_array($setting)) {
            unset($new_meta[$key]);
            continue;
        }
        $setting = array_merge($defaults, $setting);
        // Filter content for users without the unfiltered_html capability.
        $filter_content = current_user_can('unfiltered_html') ? false : true;
        /**
         * Filter html in additional content before it is saved to the database. 
         *
         * @since 1.0
         * @param bool    $filter_content Filter content. True for users without the unfiltered_html capability.
         */
        $filter_content = apply_filters('ac_additional_content_filter_html', $filter_content, $setting, $post_id);
        if ($filter_content) {
            $setting['additional_content'] = wp_filter_post_kses($setting['additional_content']);
        }
        if (is_empty_string($setting['additional_content'])) {
            unset($new_meta[$key]);
            continue;
        }
        foreach (array('prepend', 'append') as $addition) {
            if ('on' !== $setting[$addition]) {
                $setting[$addition] = $defaults[$addition];
            }
        }
        $setting['priority'] = absint($setting['priority']) ? absint($setting['priority']) : 10;
        $new_meta[$key] = $setting;
    }
    $new_meta = array_values($new_meta);
    if (!empty($new_meta)) {
        $_new_meta = array();
        $priorities = sort_by_priority($new_meta);
        foreach ($priorities as $priority) {
            foreach ($priority as $option) {
                $_new_meta[] = $option;
            }
        }
        if ($_new_meta != $old_meta) {
            update_post_meta($post_id, '_ac_additional_content', $_new_meta);
        }
    } elseif (empty($new_meta) && $old_meta) {
        delete_post_meta($post_id, '_ac_additional_content');
    }
}
 /**
  * Sets up the additional content options.
  *
  * @since 1.0
  * @param integer $post_id (Optional) Post ID. If provided no filters arre added to the_content
  * @return void
  */
 private function setup_options($post_id = 0)
 {
     $this->options = array();
     $post_id = absint($post_id);
     $add_filters = true;
     if ($post_id) {
         // Don't add filters.
         $add_filters = false;
     } else {
         $post_id = get_the_ID();
     }
     if (empty($post_id)) {
         return;
     }
     // Get the additional content options from the database.
     $options = get_post_meta($post_id, '_ac_additional_content', true);
     if (!(!empty($options) && is_array($options))) {
         return;
     }
     $this->options = $this->validate_options($options);
     if (empty($this->options)) {
         return;
     }
     // Get the priorities.
     $priorities = array_unique(wp_list_pluck($this->options, 'priority'));
     // Sort and group options by priority.
     $this->options = sort_by_priority($this->options);
     if ($add_filters) {
         // store singular options
         $this->options_singular = $this->options;
         $this->options = array();
         // Add a filter to the_content for every priority.
         foreach ($priorities as $priority) {
             add_filter('the_content', array($this, 'the_content'), $priority);
         }
     }
 }