public function save($post_id) { if (!SB_Core::verify_nonce('sb_meta_box', 'sb_meta_box_nonce') || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || !current_user_can('edit_post', $post_id)) { return $post_id; } foreach ($this->fields as $field) { $type = isset($field['type']) ? $field['type'] : ''; $name = isset($field['name']) ? $field['name'] : ''; if (empty($name)) { continue; } if ('checkbox' == $type) { $value = isset($_POST[$name]) ? 1 : 0; } elseif ('datetime' == $type) { $value = isset($_POST[$name]) ? SB_PHP::string_to_datetime($_POST[$name], 'Y-m-d') : ''; if (!empty($value)) { $value = strtotime($value); } } else { $value = isset($_POST[$name]) ? $_POST[$name] : ''; } $meta_value = SB_Core::sanitize($value, $type); SB_Post::update_meta($post_id, $name, $meta_value); } return $post_id; }
public static function select_post_type($args = array()) { $options = SB_Post::get_types(array(), 'objects'); $all_option = ''; $value = isset($args['value']) ? $args['value'] : ''; foreach ($options as $key => $option) { $tmp_args = array('value' => $key, 'selected' => $value, 'text' => $option->labels->name); $all_option .= SB_Field::get_option($tmp_args); } $args['all_option'] = $all_option; SB_Field::select($args); }
public static function select_page($args = array()) { $pages = SB_Post::get_all('page'); $id = ''; $name = ''; $description = ''; $container_class = ''; $value = ''; $field_class = ''; if (is_array($args)) { extract($args, EXTR_OVERWRITE); } ?> <div class="<?php echo $container_class; ?> "> <select id="<?php echo esc_attr($id); ?> " name="<?php echo esc_attr($name); ?> " class="<?php echo $field_class; ?> " autocomplete="off"> <option value="0"><?php _e('Select page:', 'sb-core'); ?> </option> <?php if ($pages->have_posts()) { ?> <?php while ($pages->have_posts()) { $pages->the_post(); ?> <?php $post_id = get_the_ID(); ?> <option value="<?php the_ID(); ?> " <?php selected($value, $post_id); ?> ><?php the_title(); ?> </option> <?php } wp_reset_postdata(); ?> <?php } ?> </select> <p class="description"><?php echo $description; ?> </p> </div> <?php }
public static function change_custom_menu_url($args = array()) { $site_url = isset($args['site_url']) ? $args['site_url'] : ''; $url = isset($args['url']) ? $args['url'] : ''; if (empty($url)) { $url = SB_Option::get_site_url(); } if (empty($site_url) || $url == $site_url) { return; } $menu_items = self::get_menu_custom_items(); if (!is_array($menu_items)) { return; } foreach ($menu_items as $item) { if ('trang-chu' == $item->post_name || 'home' == $item->post_name) { $item_url = $item->url; $item_url = mb_ereg_replace($url, $site_url, $item_url); SB_Post::update_custom_menu_url($item->ID, $item_url); } } }
public static function get_related_post($args = array()) { $related_posts = array(); $post_id = ''; $posts_per_page = 5; $post_type = 'post'; extract($args, EXTR_OVERWRITE); if (empty($post_id) && (is_single() || is_page() || is_singular())) { $post_id = get_the_ID(); } $tags = SB_Post::get_tag_ids($post_id); $defaults = array('post_type' => $post_type, 'tag__in' => $tags, 'posts_per_page' => -1); $defaults = wp_parse_args($defaults, $args); $posts = self::get($defaults); $tag_posts = $posts->posts; $cats = SB_Post::get_category_ids($post_id); $defaults = array('post_type' => $post_type, 'category__in' => $cats, 'posts_per_page' => -1); $defaults = wp_parse_args($defaults, $args); $posts = self::get($defaults); $cat_posts = $posts->posts; $a_part = SB_PHP::get_part_of(2 / 3, $posts_per_page); foreach ($tag_posts as $post) { if ($post->ID == $post_id || in_array($post, $related_posts)) { continue; } array_push($related_posts, $post); } $related_posts = array_slice($related_posts, 0, $a_part); if (count($related_posts) < $a_part) { $a_part_new = $posts_per_page - count($related_posts); } else { $a_part_new = $posts_per_page - $a_part; } $count = 0; foreach ($cat_posts as $post) { if ($post->ID == $post_id || in_array($post, $related_posts)) { continue; } array_push($related_posts, $post); $count++; if ($count >= $a_part_new) { break; } } return $related_posts; }
public static function insert_attachment($attachment, $file_path, $parent_post_id = 0) { if (!file_exists($file_path)) { return 0; } $file_type = wp_check_filetype(basename($file_path), null); $attachment['post_mime_type'] = $file_type['type']; if (!isset($attachment['guid'])) { return 0; } $attachment['post_status'] = isset($attachment['post_status']) ? $attachment['post_status'] : 'inherit'; if (!isset($attachment['post_title'])) { $attachment['post_title'] = preg_replace('/\\.[^.]+$/', '', basename($file_path)); } $attach_id = wp_insert_attachment($attachment, $file_path, $parent_post_id); if ($attach_id > 0) { self::update_attachment_meta($attach_id, $file_path); if ($parent_post_id > 0) { SB_Post::set_thumbnail($parent_post_id, $attach_id); } } return $attach_id; }
public static function get_widget_thumbnail_url() { $options = self::get(); $url = isset($options['post_widget']['no_thumbnail']) ? $options['post_widget']['no_thumbnail'] : ''; if (empty($url)) { $url = self::get_theme_option(array('keys' => array('thumbnail'))); } if (empty($url)) { $url = SB_Post::get_default_thumbnail_url(); } return $url; }
public static function get_blog_page() { return SB_Post::get_by_slug('blog', 'page'); }
public static function select_page($args = array()) { $pages = SB_Post::get_all('page'); $all_option = '<option value="0">' . __('Choose page', 'sb-core') . '</option>'; $value = isset($args['value']) ? $args['value'] : ''; while ($pages->have_posts()) { $pages->the_post(); $post_id = get_the_ID(); $all_option .= '<option value="' . esc_attr($post_id) . '" ' . selected($value, $post_id, false) . '>' . get_the_title() . '</option>'; } wp_reset_postdata(); $args['all_option'] = $all_option; self::select($args); }
public static function select_post_type($args = array()) { $container_class = ''; $id = ''; $name = ''; $field_class = ''; $label = ''; $options = SB_Post::get_types(array(), 'objects'); $value = ''; $description = ''; extract($args, EXTR_OVERWRITE); ?> <p class="<?php echo $container_class; ?> "> <label for="<?php echo esc_attr($id); ?> "><?php echo $label; ?> </label> <select id="<?php echo esc_attr($id); ?> " class="<?php echo $field_class; ?> " name="<?php echo esc_attr($name); ?> " autocomplete="off"> <?php foreach ($options as $key => $option) { ?> <option value="<?php echo esc_attr($key); ?> "<?php selected($value, $key); ?> ><?php echo $option->labels->name; ?> </option> <?php } ?> </select> <?php if (!empty($description)) { ?> <em><?php echo $description; ?> </em> <?php } ?> </p> <?php }