WordPress User Bookmarks Plugin - Collections - php

I bought and Install "WordPress User Bookmarks (Standalone version)" So the problem is, this plugin have 1 default collection, by default, for each user but I need 3 or more default collections for each user I need to know how can I edit codes to add 3 or more collections by default.
THANKS ALL
Also I Attached API Documents here:
<?php
class wpb_api {
function __construct() {
}
/******************************************
Get first image in a post
******************************************/
function get_first_image($postid) {
$post = get_post($postid);
setup_postdata($post);
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if (isset( $matches[1][0])) {
$first_img = $matches[1][0];
}
if(isset($first_img) && !empty($first_img)) {
return $first_img;
}
}
/******************************************
Get thumbnail URL based on post ID
******************************************/
function post_thumb_url( $postid ) {
$encoded = '';
if (get_post_thumbnail_id( $postid ) != '') {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $postid ), 'large' );
$encoded = urlencode($image[0]);
} elseif ( $this->get_first_image($postid) != '' ) {
$encoded = urlencode( $this->get_first_image($postid) );
} else {
$encoded = urlencode ( wpb_url . 'img/placeholder.jpg' );
}
return $encoded;
}
/******************************************
Get post thumbnail image (size wise)
******************************************/
function post_thumb( $postid, $size=400 ) {
$post_thumb_url = $this->post_thumb_url( $postid );
if (isset($post_thumb_url)) {
$cropped_thumb = wpb_url . "lib/timthumb.php?src=".$post_thumb_url."&w=".$size."&h=".$size."&a=c&q=100";
$img = '<img src="'.$cropped_thumb.'" alt="" />';
return $img;
}
}
/* New collection */
function new_collection($name) {
$user_id = get_current_user_id();
$collections = $this->get_collections($user_id);
$collections[] = array('label' => $name);
update_user_meta($user_id, '_wpb_collections', $collections);
}
/* Remove a collection */
function hard_remove_collection($id){
$user_id = get_current_user_id();
$collections = $this->get_collections($user_id);
$bookmarks = $this->get_bookmarks( $user_id );
// remove bookmarks
foreach($collections[$id] as $k => $arr) {
if ($k != 'label') {
if (isset($bookmarks[$k])){
unset($bookmarks[$k]);
}
}
}
// remove collection
if ($id > 0){
unset($collections[$id]);
}
update_user_meta($user_id, '_wpb_bookmarks', $bookmarks);
update_user_meta($user_id, '_wpb_collections', $collections);
}
/* Soft-Remove a collection */
function soft_remove_collection($id){
$user_id = get_current_user_id();
$collections = $this->get_collections($user_id);
$bookmarks = $this->get_bookmarks( $user_id );
// transfer bookmarks to default collection
foreach($collections[$id] as $k => $arr) {
if ($k != 'label') {
$collections[0][$k] = 1;
}
}
// remove collection
if ($id > 0){
unset($collections[$id]);
}
update_user_meta($user_id, '_wpb_bookmarks', $bookmarks);
update_user_meta($user_id, '_wpb_collections', $collections);
}
/* Get bookmarks by collection */
function get_bookmarks_by_collection($id){
$collections = $this->get_collections( get_current_user_id() );
return $collections[$id];
}
function get_bookmarks_count_by_collection($id){
$collections = $this->get_collections( get_current_user_id() );
if ($id == 0){
if (empty($collections[$id])){
return 0;
} else {
return (int)count($collections[$id]);
}
} else {
return (int)count($collections[$id])-1;
}
}
/* print bookmarks */
function print_bookmarks($coll_id) {
$output = '';
$output .= '<div class="wpb-coll-count">';
$output .= sprintf(__('%s Bookmarks in Collection','wpb'), $this->get_bookmarks_count_by_collection($coll_id));
if ($coll_id != 0) { // default cannot be removed
$output .= ''.__('Remove Collection','wpb').'';
/* To hide a collection */
$output .= '<div class="wpb-coll-remove">';
$output .= __('Choose how do you want to remove this collection. This action cannot be undone!','wpb');
$output .= '<div class="wpb-coll-remove-btns">';
if ($this->get_bookmarks_count_by_collection($coll_id) > 0) {
$output .= ''.__('Remove collection and all bookmarks in it','wpb').'';
$output .= ''.__('Remove collection only','wpb').'';
} else {
$output .= ''.__('Remove collection','wpb').'';
}
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
$bks = $this->get_bookmarks_by_collection( $coll_id );
$results = 0;
if (is_array($bks)){
$bks = array_reverse($bks, true);
foreach($bks as $id => $array) {
if ($id != 'label') {
$results++;
if (get_post_status($id) == 'publish') { // active post
$output .= '<div class="wpb-coll-item">';
$output .= ''.__('Remove','wpb').'';
$output .= '<div class="uci-thumb">'.$this->post_thumb($id, 50).'</div>';
$output .= '<div class="uci-content">';
$output .= '<div class="uci-title">'. get_the_title($id) . '</div>';
$output .= '<div class="uci-url">'.get_permalink($id).'</div>';
$output .= '</div><div class="wpb-clear"></div>';
$output .= '</div><div class="wpb-clear"></div>';
} else {
$output .= '<div class="wpb-coll-item">';
$output .= ''.__('Remove','wpb').'';
$output .= '<div class="uci-thumb"></div>';
$output .= '<div class="uci-content">';
$output .= '<div class="uci-title">'.__('Content Removed','wpb').'</div>';
$output .= '<div class="uci-url"></div>';
$output .= '</div><div class="wpb-clear"></div>';
$output .= '</div><div class="wpb-clear"></div>';
}
}
}
}
if ($results == 0){
$output .= '<div class="wpb-coll-item">';
$output .= __('You did not add any content to this collection yet.','wpb');
$output .= '<div class="wpb-clear"></div></div><div class="wpb-clear"></div>';
}
return $output;
}
/* Get collections for user */
function collection_options($default_collection, $post_id){
$output = '';
$user_id = get_current_user_id();
$collections = $this->get_collections($user_id);
$bookmarks = (array) get_user_meta($user_id, '_wpb_bookmarks', true);
if (isset($bookmarks[$post_id])){
$cur_collection = $bookmarks[$post_id];
} else {
$cur_collection = 0;
}
foreach($collections as $k => $v) {
if (!isset($v['label'])) $v['label'] = $default_collection;
$output .= '<option value="'.$k.'" '.selected($k, $cur_collection, 0).' >'.$v['label'];
$output .= '</option>';
}
return $output;
}
/* Find collection ID */
function collection_id($post_id){
$user_id = get_current_user_id();
$bookmarks = (array) get_user_meta($user_id, '_wpb_bookmarks', true);
if (isset($bookmarks[$post_id])){
return $bookmarks[$post_id];
}
}
/**
Is post bookmarked
**/
function bookmarked($post_id){
$user_id = get_current_user_id();
$bookmarks = (array) get_user_meta($user_id, '_wpb_bookmarks', true);
if (isset($bookmarks[$post_id])){
return true;
}
return false;
}
/* Delete collection */
function delete_collection($collection_id, $user_id) {
$array = $this->get_collections($user_id);
unset($array[$collection_id]);
update_user_meta($user_id, '_wpb_collections', $array);
}
/* Get collections */
function get_collections($user_id) {
return (array)get_user_meta($user_id, '_wpb_collections', true);
}
/* Get bookmarks */
function get_bookmarks($user_id) {
return (array)get_user_meta($user_id, '_wpb_bookmarks', true);
}
/* Count bookmarks */
function bookmarks_count($user_id) {
$bookmarks = (array)get_user_meta($user_id, '_wpb_bookmarks', true);
unset($bookmarks[0]);
if (!empty($bookmarks) ){
return count($bookmarks);
} else {
return 0;
}
}
/* Get current page url */
function get_permalink(){
global $post;
if (is_home()){
$permalink = home_url();
} else {
if (isset($post->ID)){
$permalink = get_permalink($post->ID);
} else {
$permalink = '';
}
}
return $permalink;
}
/**
Display the bookmarks in
organized collections
**/
function bookmarks( $args = array() ){
global $post;
$defaults = array(
'default_collection' => wpb_get_option('default_collection'),
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
/* output */
$output = '';
// logged in
if (is_user_logged_in()){
$output .= '<div class="wpb-coll">';
$output .= '<div class="wpb-coll-list">';
$collections = $this->get_collections( get_current_user_id() );
$active_coll = 0;
foreach($collections as $id => $array) {
if (!isset($array['label'])) { $array['label'] = $default_collection; }
if ($id == $active_coll) { $class = 'active'; } else { $class = ''; }
$output .= '<a href="#collection_'.$id.'" data-collection_id="'.$id.'" class="'.$class.'">';
if ($class == 'active'){
$output .= '<i class="wpb-icon-caret-left"></i>';
$output .= '<span class="wpb-coll-list-count wpb-coll-hide">'.$this->get_bookmarks_count_by_collection($id).'</span>';
} else {
$output .= '<i class="wpb-icon-caret-left wpb-coll-hide"></i>';
$output .= '<span class="wpb-coll-list-count">'.$this->get_bookmarks_count_by_collection($id).'</span>';
}
$output .= $array['label'].'</a>';
}
$output .= '</div>';
$output .= '<div class="wpb-coll-body">';
$output .= '<div class="wpb-coll-body-inner">';
$output .= $this->print_bookmarks($coll_id = 0);
$output .= '</div></div><div class="wpb-clear"></div>';
$output .= '</div>';
// guest
} else {
$output .= '<p>'.sprintf(__('You need to login or register to view and manage your bookmarks.','wpb'),wp_login_url( get_permalink() ), site_url('/wp-login.php?action=register&redirect_to=' . get_permalink())).'</p>';
}
return $output;
}
/**
Bookmark: display the widget that allow
bookmarks
**/
function bookmark( $args = array() ){
global $post;
$defaults = array(
'width' => wpb_get_option('width'),
'align' => wpb_get_option('align'),
'inline' => wpb_get_option('inline'),
'no_top_margin' => wpb_get_option('no_top_margin'),
'no_bottom_margin' => wpb_get_option('no_bottom_margin'),
'pct_gap' => wpb_get_option('pct_gap'),
'px_gap' => wpb_get_option('px_gap'),
'widgetized' => wpb_get_option('widgetized'),
'remove_bookmark' => wpb_get_option('remove_bookmark'),
'dialog_bookmarked' => wpb_get_option('dialog_bookmarked'),
'dialog_unbookmarked' => wpb_get_option('dialog_unbookmarked'),
'default_collection' => wpb_get_option('default_collection'),
'add_to_collection' => wpb_get_option('add_to_collection'),
'new_collection' => wpb_get_option('new_collection'),
'new_collection_placeholder' => wpb_get_option('new_collection_placeholder'),
'add_new_collection' => wpb_get_option('add_new_collection'),
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
/* options */
if (strstr($width, 'px')) { $px = 'px'; } else { $px = '%'; }
if ($px == '%') {
$btn_width = 50 - $pct_gap . $px;
} else {
$btn_width = ($width / 2 ) - $px_gap . $px;
}
if ($widgetized == 1){
$btn_width = '100%';
}
/* output */
$output = '';
// logged in
if (is_user_logged_in()){
if (isset($post->ID)){
$post_id = $post->ID;
} else {
$post_id = null;
}
$output .= '<div class="wpb-bm wpb-bm-nobottommargin-'.$no_bottom_margin.' wpb-bm-notopmargin-'.$no_top_margin.' wpb-bm-inline-'.$inline.' wpb-bm-'.$align.' wpb-bm-widgetized-'.(int)$widgetized.'" style="width:'.$width.' !important;" data-add_new_collection="'.$add_new_collection.'" data-default_collection="'.$default_collection.'" data-new_collection_placeholder="'.$new_collection_placeholder.'" data-dialog_unbookmarked="'.$dialog_unbookmarked.'" data-dialog_bookmarked="'.$dialog_bookmarked.'" data-add_to_collection="'.$add_to_collection.'" data-remove_bookmark="'.$remove_bookmark.'" data-post_id="'.$post_id.'">';
$output .= '<div class="wpb-bm-inner">';
/* collections list */
$output .= '<div class="wpb-bm-list">';
$output .= '<select class="chosen-select-collections" name="wpb_bm_collection" id="wpb_bm_collection" data-placeholder="">';
$output .= $this->collection_options( $default_collection, $post_id );
$output .= '</select>';
$output .= '</div>';
/* action buttons */
$output .= '<div class="wpb-bm-act">';
if ($this->bookmarked($post_id)) {
$output .= '<input type="hidden" name="collection_id" id="collection_id" value="'.$this->collection_id($post_id).'" />';
$output .= '<div class="wpb-bm-btn-contain" style="width:'.$btn_width.' !important;">'.$remove_bookmark.'</div>';
} else {
$output .= '<div class="wpb-bm-btn-contain" style="width:'.$btn_width.' !important;">'.$add_to_collection.'</div>';
}
$output .= '<div class="wpb-bm-btn-contain bm-right" style="width:'.$btn_width.' !important;">'.$new_collection.'</div>';
$output .= '</div><div class="wpb-clear"></div>';
$output .= '</div>';
$output .= '</div>';
if (!$inline) {
$output .= '<div class="wpb-clear"></div>';
}
// guest
} else {
$output .= '<p>'.sprintf(__('You need to login or register to bookmark/favorite this content.','wpb'),wp_login_url( get_permalink() ), site_url('/wp-login.php?action=register&redirect_to=' . get_permalink())).'</p>';
}
return $output;
}
}
$wpb = new wpb_api();

Related

Wordpress shortcode ouput via function

What is the best approach for a shortcode to get its output from another function. echo works but it generate the shortcode before all content. before ending file there is a php switch that I am loading different functions in base of which style selected. This is the function:
staticMarkup($titleRendered, $contentRendered, $feedbackRendered, $roleRendered, $companyRendered, $image_urlRendered, $slider, $color);
<?php
add_shortcode('sample', 'sample_shortcode');
function sample_shortcode($atts)
{
global $tesio;
$markup = '';
// Attributes
$atts = shortcode_atts(array(
'style' => '',
'testio_id' => '',
'slider' => '',
'colors' => '',
'color' => '',
'wide' => 'no',
'bgcolor' => '',
'dpadding' => '',
'tpadding' => '',
'spadding' => '',
'instance' => 1
) , $atts);
wp_enqueue_script('testio-front-js');
wp_localize_script('testio-front-js', 'testio_var', $atts);
$testio_id = $explodeID = $style = $slider = $colors = $bgcolor = $vpadding = $color = $wide = $colorContrast = '';
$testio_id = $atts['testio_id'];
$explodeID = explode(',', $testio_id);
$style = $atts['style'];
$slider = $atts['slider'];
$colors = $atts['colors'];
$bgcolor = $atts['bgcolor'];
$dpadding = $atts['dpadding'];
$tpadding = $atts['tpadding'];
$spadding = $atts['spadding'];
$color = $atts['color'];
$wide = $atts['wide'];
$testioAmount = count(explode(',', $testio_id));
if (!empty($colors))
{
$colors = explode(',', $colors);
}
if (!empty($bgcolor))
{
$colorContrast = getContrast50($bgcolor);
}
static $uniqueID;
if ($testio_id !== '')
{
if ($testio_id == 'all')
{
$args = array(
'post_type' => 'testio_wp',
'post_status' => 'publish',
'posts_per_page' => - 1
);
}
else
if ($testioAmount > 1)
{
$args = array(
'post_type' => 'testio_wp',
'post_status' => 'publish',
'post__in' => $explodeID,
'posts_per_page' => $testioAmount,
'orderby' => 'post__in'
);
}
else
if ($testioAmount == 1)
{
$args = array(
'post_type' => 'testio_wp',
'post_status' => 'publish',
'p' => $testio_id,
'posts_per_page' => 1
);
}
$posts = get_posts($args);
$wrapperClass = 'staticWrap';
$wideClass = '';
$sliderClass = '';
$masonryContainer = '';
$masonryContainerClose = '';
$backgroundStyle = '';
$sliderMarkup = '';
$sliderCloseMarkup = '';
$wrapperClassExtra = '';
$dataAttr = 'data-padding = ' . $dpadding . ',' . $tpadding . ',' . $spadding;
switch ($style)
{
case 'static':
if ($wide == 'yes')
{
$wideClass = ' wide';
}
default:
break;
}
if ($testioAmount == 1)
{
$slider == 'no';
$sliderContainer = '';
}
$markup.= '<div class="testioWrap ' . $wrapperClass . $wrapperClassExtra . '" ' . $backgroundStyle . ' data-id="' . $wrapperClass . $uniqueID . '" ' . $dataAttr . '>';
$markup.= $sliderMarkup;
$markup.= $masonryContainer;
$x = 0;
$colorClass = '';
$postCount = '';
foreach($posts as $post)
{
$titleRendered = $post->post_title;
$contentRendered = $post->post_content;
$idRendered = $post->ID;
$feedbackRendered = get_field('testio_rate', $idRendered);
$roleRendered = get_field('testio_role', $idRendered);
$companyRendered = get_field('testio_company', $idRendered);
$image_urlRendered = get_the_post_thumbnail($idRendered, 'thumbnail');
if (!empty($colors))
{
$colorClass = $colors[$x % 5];
}
$x++;
$postCount++;
$freeMessage = '<div class="testio-notice"> Please get the pro version to unlock ' . $style . ' style Upgrade</div>';
switch ($style)
{
case "static":
staticMarkup($titleRendered, $contentRendered, $feedbackRendered, $roleRendered, $companyRendered, $image_urlRendered, $slider, $color);
default:
break;
}
}
$markup.= $masonryContainerClose;
$markup.= $sliderCloseMarkup;
$markup.= '</div>';
return $markup;
}
else
{
$markup.= '<div class="testio">';
$markup.= __('Please put an id to the shortcode', 'testio-wp');
$markup.= '</div>';
}
$uniqueID++;
}
?>
This is the codes inside the staticMarkup function:
<?php
function staticMarkup($title, $content, $feedback, $role, $company, $image_url, $slider, $color){
$markup ='';
$colorStyle = 'style="color: '.$color.';"';
if($slider == 'yes'){
$markup .= '<div>';
}
$markup .= '<div class="testio testioStatic" data-color="'.$color.'">';
$markup .= '<div class="leftBorder"></div>';
if($image_url !== '' ){
$markup .= '<div class="thumbnailWrapper">';
$markup .= $image_url;
$markup .= '</div>';
}
$markup .= feedbackMarkup($feedback);
$markup .= '<div class="testio-content">';
$markup .= '<p>'.$content.'</p>';
$markup .= '</div>';
$markup .= '<h3 class="testio-name" '.$colorStyle.'>~ ' .$title. '</h3>';
$markup .= '<h4 class="testio-role">' .$role. ' <span class="role-divider icon-at-sign"></span> ' .$company. '</h4>';
$markup .= '</div>';
if($slider == 'yes'){
$markup .= '</div>';
}
echo $markup;
}
?>
Within the staticMarkup function replace:
echo $markup;
with:
return $markup;
This will return the variable where it has been positioned/called as opposed to above all content.

Related posts, wordpress query

The theme that I have been using for my wp site has related posts shortcode which works, but it doesn't hide the currently viewed post. I had some idea to add a variable before the query to a post so the query sees that post which is currently viewed has that variable set to true and skip it. Here is the code:
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category[0]) {
if ($ceker == false){
$ceker=true;
?>
content etc...
I use this in my own themes and it works. How can I do something similar to these shortcode that this theme uses:
#BLOG LIST...
if(!function_exists('dt_blog_posts')) {
function dt_blog_posts( $atts, $content = null ) {
extract(shortcode_atts(array(
'show_feature_image' => 'true',
'excerpt_length' => 35,
'show_meta' => 'true',
'limit' => -1,
'categories' => '',
'posts_column' => 'one-column', // one-column, one-half-column, one-third-column
), $atts));
global $post;
$meta_set = get_post_meta($post->ID, '_tpl_default_settings', true);
$page_layout = !empty($meta_set['layout']) ? $meta_set['layout'] : 'content-full-width';
$post_layout = $posts_column;
$article_class = "";
$feature_image = "";
$column = ""; $out = "";
//POST LAYOUT CHECK...
if($post_layout == "one-column") {
$article_class = "column dt-sc-one-column blog-fullwidth";
$feature_image = "blog-full";
}
elseif($post_layout == "one-half-column") {
$article_class = "column dt-sc-one-half";
$feature_image = "blog-twocolumn";
$column = 2;
}
elseif($post_layout == "one-third-column") {
$article_class = "column dt-sc-one-third";
$feature_image = "blog-threecolumn";
$column = 3;
}
//PAGE LAYOUT CHECK...
if($page_layout != "content-full-width") {
$article_class = $article_class." with-sidebar";
$feature_image = $feature_image."-sidebar";
}
//POST VALUES....
if($categories == "") $categories = 0;
//PERFORMING QUERY...
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array('post_type' => 'post', 'paged' => $paged, 'posts_per_page' => $limit, 'cat' => $categories, 'ignore_sticky_posts' => 1);
$wp_query = new WP_Query($args);
$pholder = dt_theme_option('general', 'disable-placeholder-images');
if($wp_query->have_posts()): $i = 1;
while($wp_query->have_posts()): $wp_query->the_post();
$temp_class = $format = "";
if($i == 1) $temp_class = $article_class." first"; else $temp_class = $article_class;
if($i == $column) $i = 1; else $i = $i + 1;
$out .= '<div class="'.$temp_class.'"><!-- Post Starts -->';
$out .= '<article id="post-'.get_the_ID().'" class="'.implode(" ", get_post_class("blog-post", $post->ID)).'">';
if($show_meta != "false"):
$out .= '<div class="post-details"><!-- Post Details Starts -->';
$out .= '<div class="date"><span>'.get_the_date('d').'</span>'.get_the_date('M').'<br />'.get_the_date('Y').'</div>';
$out .= '<div class="post-comments">';
$commtext = "";
if((wp_count_comments($post->ID)->approved) == 0) $commtext = '0';
else $commtext = wp_count_comments($post->ID)->approved;
$out .= ''.$commtext.' <i class="fa fa-comment"></i>';
$out .= '</div>';
$format = get_post_format();
$out .= '<span class="post-icon-format"> </span>';
$out .= '</div><!-- Post Details ends -->';
endif;
$out .= '<div class="post-content"><!-- Post Content Starts -->';
$out .= '<div class="entry-thumb">';
if(is_sticky()):
$out .= '<div class="featured-post">'.__('Featured','iamd_text_domain').'</div>';
endif;
//POST FORMAT STARTS
if( $format === "image" || empty($format) ):
if( has_post_thumbnail() && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
$out .= '</a>';
elseif($pholder != "true" && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
$out .= '</a>';
endif;
elseif( $format === "gallery" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists("items", $post_meta) ):
$out .= "<ul class='entry-gallery-post-slider'>";
foreach ( $post_meta['items'] as $item ) { $out .= "<li><img src='{$item}' alt='gal-img' /></li>"; }
$out .= "</ul>";
endif;
elseif( $format === "video" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
if( array_key_exists('oembed-url', $post_meta) ):
$out .= "<div class='dt-video-wrap'>".wp_oembed_get($post_meta['oembed-url']).'</div>';
elseif( array_key_exists('self-hosted-url', $post_meta) ):
$out .= "<div class='dt-video-wrap'>".wp_video_shortcode( array('src' => $post_meta['self-hosted-url']) ).'</div>';
endif;
endif;
elseif( $format === "audio" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
if( array_key_exists('oembed-url', $post_meta) ):
$out .= wp_oembed_get($post_meta['oembed-url']);
elseif( array_key_exists('self-hosted-url', $post_meta) ):
$out .= wp_audio_shortcode( array('src' => $post_meta['self-hosted-url']) );
endif;
endif;
else:
if( has_post_thumbnail() && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
$out .= '</a>';
elseif($pholder != "true" && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
$out .= '</a>';
endif;
endif;
//POST FORMAT ENDS
$out .= '</div>';
$out .= '<div class="entry-detail">';
$out .= '<h2>'.get_the_title().'</h2>';
if($excerpt_length != "" || $excerpt_length != 0) $out .= dt_theme_excerpt($excerpt_length);
$out .= '</div>';
if($show_meta != "true"):
$out .= '<div class="post-meta">';
$out .= '<div class="opsirnije">';
$out .= ''.'Opširnije »'.'';
$out .= '<ul>';
$out .= '<li><span class="fa fa-user"></span>'.get_the_author_meta('display_name').'</li>';
$categories = get_the_category();
$thiscats = "";
if($categories) {
foreach($categories as $category) {
$thiscats .= ''.$category->cat_name.', '; }
$thiscats = substr($thiscats,0, (strlen($thiscats)-2));
$out .= '<li><span class="fa fa-thumb-tack"></span>'.$thiscats.'</li>';
}
$out .= get_the_tag_list('<li><span class="fa fa-pencil"></span>', ', ', '</li>');
$out .= '</ul>';
$out .= '</div>';
endif;
$out .= '</div><!-- Post Content Ends -->';
$out .= '</article>';
$out .= '</div><!-- Post Ends -->';
endwhile;
if($wp_query->max_num_pages > 1):
$out .= '<div class="pagination-wrapper">';
if(function_exists("dt_theme_pagination")) $out .= dt_theme_pagination("", $wp_query->max_num_pages, $wp_query);
$out .= '</div>';
endif;
wp_reset_query($wp_query);
else:
$out .= '<h2>'.__('Nothing Found.', 'iamd_text_domain').'</h2>';
$out .= '<p>'.__('Apologies, but no results were found for the requested archive.', 'iamd_text_domain').'</p>';
endif;
return $out;
}
add_shortcode('dt_blog_posts', 'dt_blog_posts');
add_shortcode('dt_sc_blogposts', 'dt_blog_posts');
}
Answer was simple. Just added
'post__not_in' => array( get_the_ID() ))
in array.
Thx all...

Php Array list items with different class

This is my current array that displays list items in a unordered list.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
$output .= '</a></li>';
}
$output .= '</ul>';
}else{
$output = '<label>Bottle Color</label><ul></ul>';
}
echo $output;
die();
}
In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?
Found the solution thanks to Anant. Had to declare the class array like below.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
$i = 0;
$class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$category = 9;
$img_size = getimagesize($image['image']);
$output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'"
data-height="'.$img_size[1].'"';
$output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
$output .= '</a></li>';
$i++;
}
$output .= '</ul>';
}else{
$output = '
<label>Bottle Color</label><ul></ul>'; } echo $output; die(); }

Restore standard Mage_Catalog_Block_Navigation functionality in top navigation for a single website in a multi-store Magento installation

I'm currently working on a Magento installation with multiple websites and store views. I'm attempting to redesign one of the four sub websites.
There has been a custom module added which appears to rewrite/extend the default top navigation menu to automatically include CMS pages and add a banner slot to the menu. The problem is that I want to restore the default Magento top menu (i.e. no CMS pages) for a single website view.
I've tried disabling the module inside System -> Config -> Advanced -> Advanced for that website, however this seems to make the entire top navigation disappear. I believe the function I want to remove is this:
<?php
/**
* extend functions from navigation.php
*
*/
class Fvzzy_Category_Block_Navigation extends Mage_Catalog_Block_Navigation
{
protected $_menus;
protected function _getCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
$isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
if (!$category->getIsActive()) {
return '';
}
$html = array();
// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$children = (array)$category->getChildrenNodes();
$childrenCount = count($children);
} else {
$children = $category->getChildren();
$childrenCount = $children->count();
}
$hasChildren = ($children && $childrenCount);
// select active children
$activeChildren = array();
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
$activeChildrenCount = count($activeChildren);
$hasActiveChildren = ($activeChildrenCount > 0);
// prepare list item html classes
$classes = array();
$classes[] = 'level' . $level;
//$classes[] = 'nav-' . $this->_getItemPosition($level);
$text = preg_replace('/[^A-Za-z0-9]/i', '-', strtolower($category->getName()));
//$classes[] = $category->;
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
//$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
if(strtolower($text) == 'sale') $classes[] = strtolower($text);
// prepare list item attributes
$attributes = array();
if (count($classes) > 0) {
$attributes['class'] = implode(' ', $classes);
}
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
// assemble list item with attributes
$htmlLi = '<li';
foreach ($attributes as $attrName => $attrValue) {
$htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
}
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
// render children
$htmlChildren = '';
$j = 0;
foreach ($activeChildren as $child) {
$htmlChildren .= $this->_getCategoryMenuItemHtml(
$child,
($level + 1),
($j == $activeChildrenCount - 1),
($j == 0),
false,
$outermostItemClass,
$childrenWrapClass,
$noEventAttributes
);
$j++;
}
$promotion = $this->addPromotions($category->getId());
if (!empty($htmlChildren)) {
if ($childrenWrapClass) {
$html[] = '<div class="' . $childrenWrapClass . '">';
}
$html[] = '<ul class="level' . $level . '"><div class="menu-text">';
if($promotion) $html[] = '';
$html[] = $htmlChildren;
if($promotion) $html[] = '';
if($promotion) $html[] = '</div><li class="level1 parent menu-promotion">'.$promotion.'</li>';
$html[] = '</ul>';
if ($childrenWrapClass) {
$html[] = '</div>';
}
}
$html[] = '</li>';
$html = implode("\n", $html);
return $html;
}
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
{
$activeCategories = array();
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive()) {
$activeCategories[] = $child;
}
}
$activeCategoriesCount = count($activeCategories);
$hasActiveCategoriesCount = ($activeCategoriesCount > 0);
if (!$hasActiveCategoriesCount) {
return '';
}
$html = '';
$j = 0;
foreach ($activeCategories as $category) {
$html .= $this->_getCategoryMenuItemHtml(
$category,
$level,
($j == $activeCategoriesCount - 1),
($j == 0),
true,
$outermostItemClass,
$childrenWrapClass,
true
);
$j++;
}
//main store
if(Mage::app()->getStore()) $html .= $this->addMenu($childrenWrapClass,Mage::app()->getStore());
return $html;
}
protected function addPromotions($id = 0){
if($id){
$base = Mage::getBaseUrl('media',true).'promotion_box_images/';
$file_base = Mage::getBaseDir('media').'/promotion_box_images';
$html = ''; $img = '';
$promotion = Mage::getModel('promotion/box')->getCollection()->addFieldToFilter('menu_ids',array(
array('like'=>$id),
array('like'=>$id.',%'),
array('like'=>'%,'.$id.',%'),
array('like'=>'%,'.$id)))->setOrder('position')->getFirstItem(); //default is desc
if($promotion && $promotion->getId()){
$type=$promotion->getDisplayType();
$link = ''; $img = ''; $content = ''; $width = '';
if(file_exists($file_base.'/'.$promotion->getImage())){
list($width) = getimagesize($file_base.'/'.$promotion->getImage());
}
if($type!=null){
switch($type){
case 0:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if(trim($promotion->getLink())!='') $link = $promotion->getLink();
break;
case 1:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getCategoryId()){
$c = Mage::getModel('catalog/category')->load($promotion->getCategoryId());
if($c->getId() && $c->getIsActive()) $link = Mage::helper('catalog/category')->getCategoryUrl($c);
}
break;
case 2:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getProductSku()){
$id = Mage::getModel('catalog/product')->getIdBySku($promotion->getProductSku());
$p = Mage::getModel('catalog/product')->load($id);
if($p->getId()) $link = $p->getProductUrl();
}
break;
case 3:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getPageId()){
$_link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());
if($_link != ''){
$nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
->addFieldToFilter('page_id', array('eq' => $promotion->getPageId()));
foreach($nodes as $_node){
$link = $_node->getRequestUrl();
}
if(!$link){$link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());}
}
}
break;
case 4:
$content = $promotion->getContent();
break;
}
}
if($img && $link){
$html = ''.$img.'';
}
elseif($img){
$html = ''.$img.'';
}
else{
$html = $content;
}
}
return $html;
}else{return false;}
}
//$menu = array( array('label'=>'News','href'=>'blog','module'=>'blog','sub'=>array(array('label'=>'submenu','href'=>'blog2','module'=>'blog'))) );
public function setOtherMenu($menu){
$this->_menus = $menu;
}
public function addMenu( $childrenWrapClass = '', $store ) {
$this->_node_ids = array();
$current_module = $this->getRequest()->getModuleName();
$page_id = $this->getRequest()->getParam('page_id');
// get all the cms page nodes not folder or containers
$nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
->joinMetaData()
->addFieldToFilter( 'level', 1 )
//->addFieldToFilter( 'menu_visibility', 1 )
->addFieldToFilter( 'main_table.page_id', array( 'notnull' => true ))
->setOrder('sort_order','ASC');
if ( $store instanceof Mage_Core_Model_Store ) {
$store = $store->getId();
$storeIds = array( 0, $store );
$nodes->getSelect()
->joinLeft( array( 'cs' => 'cms_page_store' ), 'main_table.page_id=cs.page_id', array( 'cs.store_id' ) )
->where( 'cs.store_id IN ('. implode( ',', $storeIds ) .')' ); // not the best sql here but it works as store id will be one at a time.
}
$active_node = Mage::registry('current_cms_hierarchy_node');
$active_node_ids = array();
if($active_node){
$xpath = $active_node->getXpath();
$all = explode( '/', $xpath );
foreach ( $all as $index ) $active_node_ids[] = $index;
}
$this->_node_ids = $active_node_ids;
if ( $this->_menus ) {
$menus = $this->_menus;
}
else {
$menus = array();
}
$links = '';
foreach ( $nodes as $node ) {
$tree = $node->setCollectActivePagesOnly(true)
->setCollectIncludedPagesOnly(true)
->setTreeMaxDepth(0)
->setTreeIsBrief(1) //this way it won't show the container link
->getTreeSlice(0, 1); //up to tree top and down to one level
$links .= $this->drawCmsMenu($tree,0,1,$childrenWrapClass);
}
foreach ( $menus as $menu ) {
if(isset($menu['sub'])) $has_sub_class = ' parent'; else $has_sub_class = '';
if (isset($menu['module']) && $current_module == $menu['module'] ) {
$links .= '<li class="level0 level-top active'.$has_sub_class.'">';
}
else {
$links .= '<li class="level0 level-top'.$has_sub_class.'">';
}
$html = '';
if(isset($menu['sub']) && is_array($menu['sub'])){
$submenu = $menu['sub'];
$html .= '<div class="'.$childrenWrapClass.'"><ul class="level0">';
foreach($submenu as $sub){
if(is_array($sub) &&isset($sub['href']) && isset($sub['label']))
//2012-11-20 AU TEAM coding for change <h2> -> <span>
$html .= '<li class="level1"><span>'.$sub['label'].'</span></li>';
}
$html .= '</div>';
}
if(isset($menu['href']) && isset($menu['label']))
$links .= '<a class="level-top" href="'.Mage::getBaseUrl().$menu['href'].'"><span>'.$menu['label'].'</span></a>';
//add submenu
if($html != '') $links .= $html;
$links .= '</li>';
}
return $links;
}
/* Add Maximal Depth filter
* 2012.04.11 by Bruce
*/
public function drawCmsMenu( array $tree, $parentNodeId = 0, $level = 0, $childrenWrapClass = '' ) {
$html = '';
if ( !isset($tree[$parentNodeId ])) return $html;
foreach ( $tree[ $parentNodeId ] as $nodeId => $node ) {
/* #var $node Enterprise_Cms_Model_Hierarchy_Node */
$nested = $this->drawCmsMenu( $tree, $nodeId, $node->getLevel()+1, $childrenWrapClass);
$hasChildren = ( $nested != '' );
// set style classes
$class = array();
$class[] = 'level'. ( $level - 1 );
if ( $level - 1 == 0 )
$class[] = 'level-top';
if ( $this->_node_ids && in_array( $node->getNodeId(), $this->_node_ids ) )
$class[] = 'active';
if ( $hasChildren )
$class[] = 'parent';
// li wrapper header
$html .= '<li class="'. implode( ' ', $class ) .'">';
$html .= $this->_getNodeLabel( $node, $level - 1 );
// div wrapper header
if ( $hasChildren ){
if ( $childrenWrapClass ) $html .= '<div class="'. $childrenWrapClass .'">';
$html .= '<ul class="level'. ( $level - 1 ) .'">';
}
// children
$html .= $nested;
// div wrapper footer
if ( $hasChildren ) {
$html.='</ul>';
if ( $childrenWrapClass ) $html .= '</div>';
}
// li wrapper footer
$html .= '</li>';
}
return $html;
}
protected function _getNodeLabel($node,$level)
{
if($level==0) $class = 'level-top'; else $class = '';
if($node->getPageTitle()) return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getPageTitle().'</span></a>';
else return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getLabel().'</span></a>';
}
}
I know that the config file for this module looks like the following:
<?xml version="1.0"?>
<config>
<modules>
<Fvzzy_Category>
<version>0.1.0</version>
</Fvzzy_Category>
</modules>
<global>
<blocks>
<fvzzy_category>
<class>Fvzzy_Category_Block</class>
</fvzzy_category>
<catalog>
<rewrite>
<navigation>Fvzzy_Category_Block_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
<resources>
<fvzzy_category_setup>
<setup>
<module>Fvzzy_Category</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
</fvzzy_category_setup>
</resources>
</global>
</config>
And that if I'm able to remove the following part from that file, I get my intended outcome:
<catalog>
<rewrite>
<navigation>Fvzzy_Category_Block_Navigation</navigation>
</rewrite>
</catalog>
The problem is, I just want to get the top navigation menu function restored back to normal for one website view.
Is there any way to overwrite that navigation rewrite event for one store/website view?
If not, is it possible to disable this module without it disabling my whole top menu for that website view?
I've tried completely duplicating the module, disabling the original one and enabling the new one in my desired website but that also appears to be making the top menu disappear entirely.
I'm really not sure what else to do with this - if anyone is able to help it would be hugely appreciated.
Thanks so much.
if you want this extension still for your Magento application, then disabling the extension will not produce expecting result.
In this case, you can do two things
Overwrite this custom extension with your module and make changes according to your need
comment out the config section that defines that core block overwrite. You have already tried it and worked it
There is another alternative. You can do a condition checking inside the rewrite block class. This condition check somewhat look like this.
if(Mage::app()->getStore()->getCode()== 'website_code_for_sub_site'){
//execute existing code
}
This method ensure that, the method that defined inside the rewrite block class will get executed only when the site with a code website_code_for_sub_site is viewing.
Hope that helps

More efficient way to build dynamic menu

I am creating a dynamic menu whos items appear depending on a set 'mode' (that is passed via ajax). Off this it creates the menu, disabling and hiding icons that are not associated with that mode.
The problem is, in my implementation there are a lot of if conditions. Can anyone show me a cleaner way of doing what I am trying to achieve?
My code is:
public function gridMenu()
{
$mode = Validate::sanitize($_POST['mode']);
$modes = array(
'products' => array('edit', 'delete', 'archive')
);
$output = '<div id="hexContainer">';
for($i = 1; $i < 7; $i++) {
$img = '';
$output .= '<div class="hex hex' . $i;
if($i == 1)
{
if(in_array('edit', $modes[$mode]))
{
$output .= ' hex-selectable';
$img = '<img data-option="Edit" src="' . ROOT . 'images/edit.png">';
} else {
$output .= ' hex-disabled';
}
}
if($i == 2)
{
if(in_array('zzz', $modes[$mode]))
{
$output .= ' hex-selectable';
} else {
$output .= ' hex-disabled';
}
}
if($i == 3)
{
if(in_array('delete', $modes[$mode]))
{
$output .= ' hex-selectable';
$img = '<img data-option="Delete" src="' . ROOT . 'images/delete.png">';
} else {
$output .= ' hex-disabled';
}
}
if($i == 4)
{
if(in_array('xxx', $modes[$mode]))
{
$output .= ' hex-selectable';
} else {
$output .= ' hex-disabled';
}
}
if($i == 5)
{
if(in_array('archive', $modes[$mode]))
{
$output .= ' hex-selectable';
$img = '<img data-option="Archive" src="' . ROOT . 'images/archive.png">';
} else {
$output .= ' hex-disabled';
}
}
if($i == 6)
{
if(in_array('zzz', $modes[$mode]))
{
$output .= ' hex-selectable';
} else {
$output .= ' hex-disabled';
}
}
$output .= '">';
$output .= $img;
$output .= '</div>';
}
$output .= '<div class="hex hex-mid"></div>';
$output .= '</div>';
echo $output;
}
Use the switch function:
switch ($i){
case 1:
//code for $i==1
break;
case 2:
// code for $i==2
break;
//...
}
This hasn't been tested, but you could try something along the lines of this.
Basically from your code I'm assuming that you're trying to insert an image only on the odd rounds. So with that I created a $_modes and $_mode variables which will hold the values for each round.
With these values, we will automatically create the HTML classes using the rules you have set in your code, and on each round if it's odd, and in the array then we also add the image.
public function gridMenu() {
$mode = Validate::sanitize($_POST['mode']);
$modes = array(
'products' => array('edit', 'delete', 'archive')
);
$_modes = array(
false,
array('Edit', 'edit'),
array('ZZZ', 'zzz'),
array('Delete', 'delete'),
array('XXX', 'xxx'),
array('Archive', 'archive'),
array('ZZZ', 'zzz'),
);
$output = '<div id="hexContainer">';
for($i = 1; $i < 7; $i++) {
$_mode = $_modes[$i];
$img = '';
$classnames = array('hex', 'hex' . $i);
if ( ($i % 2) === 0 && in_array($_mode[1], $modes[$mode])) {
$img = '<img data-option="' . $_mode[0] . '" src="' . ROOT . 'images/' . $_mode[1] . '.png">';
}
$classnames[] = (in_array($_mode[1], $modes[$mode]) ? 'hex-selectable' : 'hex-disabled');
$output .= '<div class="' . implode(' ', $classnames) . '">';
$output .= $img;
$output .= '</div>';
}
$output .= '<div class="hex hex-mid"></div>';
$output .= '</div>';
echo $output;
}

Categories