how to add multi select category in visual composer - wordpress

How can I add a multi-selector for the Visual composer?
vc_map( array(
"name" => esc_html__("Ajax Posts", '7mag'),
"base" => "YT_ajaxpost",
"icon" => "YT_vc_ico_ajaxpost",
"class" => "YT_vc_sc_ajaxpost",
"category" => esc_html__("7mag", '7mag'),
"params" => array(
array(
"type" => "dropdown",
"heading" => esc_html__("Style", '7mag'),
"param_name" => "multiple",
"value" => $categories_array,
),
) );
For example:
link

You need to register new shortcode attribute.
// Create multi dropdown param type
vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );
function dropdown_multi_settings_field( $param, $value ) {
$param_line = '';
$param_line .= '<select multiple name="'. esc_attr( $param['param_name'] ).'" class="wpb_vc_param_value wpb-input wpb-select '. esc_attr( $param['param_name'] ).' '. esc_attr($param['type']).'">';
foreach ( $param['value'] as $text_val => $val ) {
if ( is_numeric($text_val) && (is_string($val) || is_numeric($val)) ) {
$text_val = $val;
}
$text_val = __($text_val, "js_composer");
$selected = '';
if(!is_array($value)) {
$param_value_arr = explode(',',$value);
} else {
$param_value_arr = $value;
}
if ($value!=='' && in_array($val, $param_value_arr)) {
$selected = ' selected="selected"';
}
$param_line .= '<option class="'.$val.'" value="'.$val.'"'.$selected.'>'.$text_val.'</option>';
}
$param_line .= '</select>';
return $param_line;
}
Then use it:
array(
"type" => "dropdown_multi",
"heading" => esc_html__("Style", '7mag'),
"param_name" => "multiple",
"value" => $categories_array,
),

I've updated the above solution. due to $value!=='' check the values were not being selected after save.
function dropdown_multi_settings_field( $param, $value ) {
if ( ! is_array( $value ) ) {
$param_value_arr = explode( ',', $value );
} else {
$param_value_arr = $value;
}
$param_line = '';
$param_line .= '<select multiple name="' . esc_attr( $param['param_name'] ) . '" class="wpb_vc_param_value wpb-input wpb-select ' . esc_attr( $param['param_name'] ) . ' ' . esc_attr( $param['type'] ) . '">';
foreach ( $param['value'] as $text_val => $val ) {
if ( is_numeric( $text_val ) && ( is_string( $val ) || is_numeric( $val ) ) ) {
$text_val = $val;
}
$selected = '';
if ( ! empty( $param_value_arr ) && in_array( $val, $param_value_arr ) ) {
$selected = ' selected="selected"';
}
$param_line .= '<option class="' . $val . '" value="' . $val . '"' . $selected . '>' . $text_val . '</option>';
}
$param_line .= '</select>';
return $param_line;
}
vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );

Related

WooCommerce: How to add hook for only one product? By ID?

I want to make a drop down list with quantity selection for only one item.
I'm using this code, but it turns on the dropdown on all products.
How can I use this script for only one product?
<?php
function woocommerce_quantity_input() {
global $product;
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product )
);
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 20;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$options .= '<option value="' . $count . '">' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
?>
Wrap everything with an if statement where you check against ID that you have chosen.
function woocommerce_quantity_input() {
global $product;
if($product->get_id() === 14) :
endif;
}

How insert title attribute in a template Wordpress

my wordpress template does not display the title attribute of the menu (but if I change the template it is okay), how could i solve it? the website in www.imsdesign.eu
I am assuming that you added title attributes to you menu by doing below steps:
From your WordPress dashboard, go to your Menus page (Appearance > Menus)
Select the Menu that you want to edit from the list of your Menus.
Click on the configuration arrow on the right side of the Menu Item title, that when clicked opens the configuration box.
Click on the “Screen Options” on top of the page to reveal additional advanced menu properties.
Activate the option for Title Attribute by selecting the Title Attribute box to expose the settings box under the Menu item instantly.
Add your Title Attribute in the text box just below the Navigation label option.
If it is still not working that's mean that you WP theme does not has title attributes. To change it please find the correct WP template (probably header.php) and add title attributes to nav elements by using method the_title_attribute()
<?php
class FastwpAltiusMenu {
private static $nav_template = '';
private static $nav_overlay = '';
function __construct() {
global $fastwp_altius_data, $fastwp_altius_menu_type;
$templates = array();
$templates[1] = array( 'nav' => 'fastwp-menu-default', 'overlay' => '' );
$templates[2] = array( 'nav' => 'fastwp-menu-overlay', 'overlay' => 'fastwp-menu-overlay2' );
if( is_page() ) {
$fastwp_altius_meta_type = get_post_meta( get_the_ID(), 'p_nav_style', true );
if( $fastwp_altius_meta_type == 1 || $fastwp_altius_meta_type == 2 ) {
$fastwp_altius_data['nav_style'] = $fastwp_altius_meta_type;
}
}
$menu = !empty( $fastwp_altius_data['nav_style'] ) && in_array( (int) $fastwp_altius_data['nav_style'], array_keys( $templates ) ) ? (int) $fastwp_altius_data['nav_style'] : 1;
$fastwp_altius_menu_type= $menu;
self::$nav_template = $templates[$menu]['nav'];
self::$nav_overlay = $templates[$menu]['overlay'];
}
public static function nav( $echo = true ) {
if( (boolean) $echo ) {
echo get_template_part( 'fastwp/menu/' . self::$nav_template );
}
ob_start();
get_template_part( 'fastwp/menu/' . self::$nav_template );
$content = ob_get_contents();
ob_end_clean();
return $content;
}
public static function overlay( $echo = true ) {
$nav_overlay = self::$nav_overlay;
if( !empty( $nav_overlay ) ) {
if( (boolean) $echo ) {
echo get_template_part( 'fastwp/menu/' . $nav_overlay );
}
ob_start();
get_template_part( 'fastwp/menu/' . $nav_overlay );
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
}
class FastwpAltiusWalker {
var $one_page = false;
function __construct( $menu_id, $menu = 0 ) {
$this->override_menu = $menu;
$elements = $this->nice_elements( $menu_id );
$this->main_elements = isset( $elements['main'] ) ? $elements['main'] : array();
$this->subnav_elements = isset( $elements['subnav'] ) ? $elements['subnav'] : array();
}
function set_onePage() {
$this->one_page = true;
}
function add_one_page_section( $item ) {
if( $this->one_page ) {
global $fastwp_altius_one_page_sections;
if( $item->object == 'page' ) {
if( empty( $fastwp_altius_one_page_sections ) ) $fastwp_altius_one_page_sections = array();
$section_name = preg_replace( '/[^a-z0-9_]/i', '', str_replace( ' ', '_', $item->title ) );
if( in_array( $section_name, array_keys( $fastwp_altius_one_page_sections ) ) ) {
$section_name = $section_name . '_' . $item->ID;
}
$fastwp_altius_one_page_sections[$section_name] = $item->object_id;
return '#section-' . $section_name;
}
}
return $item->url;
}
function get_menu() {
$output = '';
if( !empty( $this->main_elements ) ) {
/* Add Visual Composer meta */
global $fastwp_altius_one_page_css_vc;
$fastwp_altius_one_page_css_vc = array();
foreach( $this->main_elements as $item ) {
$item = $item[0];
$typeparent = get_post_meta( $item->ID, '_menu_item_menutype', true );
/* Add Visual Composer meta */
if( isset( $item->object_id ) ) {
$fastwp_altius_one_page_css_vc[$item->object_id] = get_post_meta( $item->object_id, '_wpb_shortcodes_custom_css', true );
}
$a_classes = $li_classes = $a_atts = $li_atts = array();
if( get_the_ID() == $item->object_id ) {
$li_classes[] = 'current';
}
if( isset( $item->hasChildren ) && $item->hasChildren ) {
$a_classes[] = 'sf-with-ul';
}
if( isset( $this->subnav_elements[$item->ID] ) ) {
$li_classes[] = 'parentMenuItem';
}
if( !empty( $item->classes ) ) {
$a_classes[] = implode( ' ', $item->classes );
}
$childrens = '';
if( $typeparent == 'mega2' ) {
$li_classes[] = 'dropDown megaMenu';
$items_array = $this->levels_recursive_array( $item->ID );
unset( $items_array['subnav'] );
$items_per_col = ceil( count( $items_array ) / 2 );
$col1_items = $col2_items = '';
foreach( $items_array as $k => $mitem ) {
$typechild = get_post_meta( $mitem->ID, '_menu_item_menutype2', true );
if( $k >= $items_per_col ) {
$col2_items .= $this->link_type( $mitem, $typechild );
} else {
$col1_items .= $this->link_type( $mitem, $typechild );
}
}
$childrens = '<div class="megaMenuContent cols2 clearfix">';
$childrens .= '<ul>' . $col1_items . '</ul>';
$childrens .= '<ul>' . $col2_items . '</ul>';
$childrens .= '</div>';
} else if( $typeparent == 'mega3' ) {
$li_classes[] = 'dropDown megaMenu';
$items_array = $this->levels_recursive_array( $item->ID );
unset( $items_array['subnav'] );
$items_per_col = ceil( count( $items_array ) / 3 );
$col1_items = $col2_items = $col3_items = '';
foreach( $items_array as $k => $mitem ) {
$typechild = get_post_meta( $mitem->ID, '_menu_item_menutype2', true );
if( $k < $items_per_col ) {
$col1_items .= $this->link_type( $mitem, $typechild );
} else if( $k/2 >= $items_per_col ) {
$col3_items .= $this->link_type( $mitem, $typechild );
} else {
$col2_items .= $this->link_type( $mitem, $typechild );
}
}
$childrens = '<div class="megaMenuContent cols3 clearfix">';
$childrens .= '<ul>' . $col1_items . '</ul>';
$childrens .= '<ul>' . $col2_items . '</ul>';
$childrens .= '<ul>' . $col3_items . '</ul>';
$childrens .= '</div>';
} else {
$childrens = $this->levels_recursive( $item->ID );
}
$output .= sprintf( "\n" . '<li%s%s><a href="%s"%s%s>%s</a>%s</li>',
( !empty( $li_classes ) ? ' class="' . implode( $li_classes, ' ' ) . '"' : '' ),
( !empty( $li_atts ) ? ' ' . implode( ' ', $li_atts ) : '' ),
$this->add_one_page_section( $item ),
( !empty( $a_classes ) ? ' class="' . implode( $a_classes, ' ' ) . '"' : ' ' ),
( !empty( $a_atts ) ? ' ' . implode( ' ', $a_atts ) : '' ),
$item->title,
$childrens );
}
}
return $output;
}
function link_type( $item = '', $type = '' ) {
$li_classes = array();
if( get_the_ID() == $item->object_id ) {
$li_classes[] = 'current';
}
if( $type == 'title' ) {
return '<li class="megaMenuTitle' . ( !empty( $li_classes ) ? ' ' . implode( ' ', $li_classes ) : '' ) . '">' . $item->title . '</li>';
} else {
return '<li' . ( !empty( $li_classes ) ? ' class="' . implode( ' ', $li_classes ) . '"' : '' ) . '>' . $item->title . '</li>';
}
}
function levels_recursive( $parent = 0, $level = 1 ) {
if( empty( $parent ) ) return ;
$output = '';
if( isset( $this->subnav_elements[$parent] ) ) {
$output .= '<ul class="fastwp-menu-level-' . $level . '">';
foreach( $this->subnav_elements[$parent] as $item ) {
$output .= '<li' . ( $this->item_has_children( $item->ID ) ? ' class="dropDown"' : '' ) . '>' . $item->title . '';
$output .= $this->levels_recursive( $item->ID, ($level+1) );
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
function item_has_children( $item = '' ) {
if( in_array( $item, array_keys( $this->subnav_elements ) ) ) {
return true;
}
return false;
}
function levels_recursive_array( $parent = 0 ) {
if( empty( $parent ) ) return array();
$output = array();
if( isset( $this->subnav_elements[$parent] ) ) {
foreach( $this->subnav_elements[$parent] as $item ) {
$output[] = $item;
$output['subnav'] = $this->levels_recursive( $item->ID );
}
}
return $output;
}
function nice_elements( $menu_id ) {
$elements = array();
foreach( $this->get_elements( $menu_id ) as $element ) {
if( !empty( $element->menu_item_parent ) ) {
$elements['subnav'][$element->menu_item_parent][] = $element;
} else {
$elements['main'][$element->ID][] = $element;
}
}
return $elements;
}
function get_elements( $menu_id = 'primary' ) {
$locations = get_nav_menu_locations();
if( !empty( $this->override_menu ) ) {
$menu = $this->override_menu;
} else {
$menu = $locations[$menu_id];
}
$elems = wp_get_nav_menu_items( get_term( $menu, 'nav_menu' ), array( 'depth' => 1 ) );
$return_elems = array();
foreach( $elems as $elem ) {
$return_elems[] = $elem;
}
return $return_elems;
}
}

dont allow PO BOX shipping woocommerce not working

add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array(" ", ".", ",");
$address = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );
if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
$woocommerce->add_error( "Sorry, we don't ship to PO BOX addresses." );
}
}
i am getting:
fatal error :call to undefined add_error
when i am pasting on my function .php
add_error() has been renamed to wc_add_notice():
$woocommerce->wc_add_notice( "Sorry, we don't ship to PO BOX addresses." );
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ?
$posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ?
$posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array(" ", ".", ",");
$address = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );
if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
$notice = sprintf( __( '%1$sSorry, we dont ship to PO BOX addresses..' , 'error' ) , '<strong>' , '</strong>' );
if ( version_compare( WC_VERSION, '2.3', '<' ) ) {
$woocommerce->add_error( $notice );
} else {
wc_add_notice( $notice, 'error' );
}
}
}
<?php
add_filter('woocommerce_package_rates',
'shomaris_hide_fedex_for_po_box_shipment',
10, 2);
function shomaris_hide_fedex_for_po_box_shipment($available_shipping_methods,
$package){
$shipping_method_to_hide = 'flat_rate:4';
// $shipping_method_to_hides = 'flat_rate:5';
global $woocommerce;
$address = ( !empty( $woocommerce->customer->get_shipping_address_1() ) ) ?
$woocommerce->customer->get_shipping_address_1() : $woocommerce->customer-
>get_billing_address_1();
$postcode = ( !empty( $woocommerce->customer->get_shipping_postcode() ) ) ?
$woocommerce->customer->get_shipping_postcode() : $woocommerce->customer-
>get_billing_postcode();
$replace = array(" ", ".", ",");
$address2 = strtolower( str_replace( $replace, '', $address ) );
if ( strstr( $address2, 'pobox' ) ) {
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $shipping_method_to_hide,
$shipping_method_to_hides ) !== false ) {
unset($available_shipping_methods[$shipping_method]);
}
}
}
return $available_shipping_methods;
}
?>

Woocommerce Breadcrumbs not displaying full path

Working on a woocommerce cart with many categories / sub categories / sub-sub cat's etc.
Bread crumbs default to only displaying the Home page, and whatever page your on... its kind of stupid.
So if I navigate to home/catalog/cat1/subcat1/product, the bread crumbs only display; home/catalog/product... What's the point of bread crumbs if you can't navigate your path backwards?
Has anyone else run into this problem?
If anyone else is looking, putting this in fuctions.php helped.
// Breadcrumbs Display Category Name //
function get_breadcrumb_category( $cat ) {
$post = get_post( $post->ID );
$post_type = $post->post_type;
$taxonomy = $cat;
$f_categories = wp_get_post_terms( $post->ID, $taxonomy );
$f_category = $f_categories[0];
if ( $f_category->parent != 0 ) {
$f_category_id = $f_category->parent;
$parent_array = get_term_by('id', $f_category_id, $taxonomy, 'ARRAY_A');
$f_category_name = $parent_array["name"];
$term_link = get_term_link( $f_category_id, $taxonomy );
} else {
$f_category_id = $f_category->term_id;
$f_category_name = $f_category->name;
$term_link = get_term_link( $f_category_id, $taxonomy );
}
if ( $f_categories && ! is_wp_error($f_categories) ) {
return '' . $f_category_name . '';
} else {
return '';
}
}
function x_breadcrumbs() {
if ( x_get_option( 'x_breadcrumb_display', '1' ) ) {
GLOBAL $post;
$is_ltr = ! is_rtl();
$stack = x_get_stack();
$delimiter = x_get_breadcrumb_delimiter();
$home_text = x_get_breadcrumb_home_text();
$home_link = home_url();
$current_before = x_get_breadcrumb_current_before();
$current_after = x_get_breadcrumb_current_after();
$page_title = get_the_title();
$blog_title = get_the_title( get_option( 'page_for_posts', true ) );
$post_parent = $post->post_parent;
if ( X_WOOCOMMERCE_IS_ACTIVE ) {
$shop_url = x_get_shop_link();
$shop_title = x_get_option( 'x_' . $stack . '_shop_title', __( 'The Shop', '__x__' ) );
$shop_link = '' . $shop_title . '';
}
echo '<div class="x-breadcrumbs">' . $home_text . '' . $delimiter;
if ( is_home() ) {
echo $current_before . $blog_title . $current_after;
} elseif ( is_category() ) {
$the_cat = get_category( get_query_var( 'cat' ), false );
if ( $the_cat->parent != 0 ) echo ''.get_the_title(102) .'';
echo $current_before . single_cat_title( '', false ) . $current_after;
} elseif ( x_is_product_category() ) {
if ( $is_ltr ) {
echo $shop_link . $delimiter . $current_before . single_cat_title( '', false ) . $current_after;
} else {
echo $current_before . single_cat_title( '', false ) . $current_after . $delimiter . $shop_link;
}
} elseif ( x_is_product_tag() ) {
if ( $is_ltr ) {
echo $shop_link . $delimiter . $current_before . single_tag_title( '', false ) . $current_after;
} else {
echo $current_before . single_tag_title( '', false ) . $current_after . $delimiter . $shop_link;
}
} elseif ( is_search() ) {
echo $current_before . __( 'Search Results for ', '__x__' ) . '“' . get_search_query() . '”' . $current_after;
} elseif ( is_singular( 'post' ) ) {
if ( get_option( 'page_for_posts' ) == is_front_page() ) {
echo $current_before . $page_title . $current_after;
} else {
if ( $is_ltr ) {
$f_category = get_the_category();
if ( $f_category[0]->parent != 0 ) {
$f_category_id = $f_category[0]->parent;
$f_category_name = get_cat_name( $f_category_id );
} else {
$f_category_id = $f_category[0]->term_id;
$f_category_name = $f_category[0]->name;
}
echo '' . $f_category_name . '' . $delimiter . $current_before . $page_title . $current_after;
} else {
echo $current_before . $page_title . $current_after . $delimiter . '' . $blog_title . '';
}
}
} elseif ( x_is_portfolio() ) {
echo $current_before . get_the_title() . $current_after;
} elseif ( x_is_portfolio_item() ) {
$link = x_get_parent_portfolio_link();
$title = x_get_parent_portfolio_title();
if ( $v = get_breadcrumb_category('portfolio-category') ) {
$portfolio_category = $delimiter . $v;
} else {
$portfolio_category = '';
}
if ( $is_ltr ) {
echo '' . $title . '' . $portfolio_category . $delimiter . $current_before . $page_title . $current_after;
} else {
echo $current_before . $page_title . $current_after . $portfolio_category . $delimiter . '' . $title . '';
}
} elseif ( x_is_product() ) {
if ( $v = get_breadcrumb_category('product_cat') ) {
$product_category = $delimiter . $v;
} else {
$product_category = '';
}
if ( $is_ltr ) {
echo $shop_link . $product_category . $delimiter . $current_before . $page_title . $current_after;
} else {
echo $current_before . $page_title . $current_after . $product_category . $delimiter . $shop_link;
}
} elseif ( x_is_buddypress() ) {
if ( bp_is_group() ) {
echo '' . x_get_option( 'x_buddypress_groups_title', __( 'Groups', '__x__' ) ) . '' . $delimiter . $current_before . x_buddypress_get_the_title() . $current_after;
} elseif ( bp_is_user() ) {
echo '' . x_get_option( 'x_buddypress_members_title', __( 'Members', '__x__' ) ) . '' . $delimiter . $current_before . x_buddypress_get_the_title() . $current_after;
} else {
echo $current_before . x_buddypress_get_the_title() . $current_after;
}
} elseif ( x_is_bbpress() ) {
remove_filter( 'bbp_no_breadcrumb', '__return_true' );
if ( bbp_is_forum_archive() ) {
echo $current_before . bbp_get_forum_archive_title() . $current_after;
} else {
echo bbp_get_breadcrumb();
}
add_filter( 'bbp_no_breadcrumb', '__return_true' );
} elseif ( is_page() && ! $post_parent ) {
echo $current_before . $page_title . $current_after;
} elseif ( is_page() && $post_parent ) {
$parent_id = $post_parent;
$breadcrumbs = array();
if ( is_rtl() ) {
echo $current_before . $page_title . $current_after . $delimiter;
}
while ( $parent_id ) {
$page = get_page( $parent_id );
$breadcrumbs[] = '' . get_the_title( $page->ID ) . '';
$parent_id = $page->post_parent;
}
if ( $is_ltr ) {
$breadcrumbs = array_reverse( $breadcrumbs );
}
for ( $i = 0; $i < count( $breadcrumbs ); $i++ ) {
echo $breadcrumbs[$i];
if ( $i != count( $breadcrumbs ) -1 ) echo $delimiter;
}
if ( $is_ltr ) {
echo $delimiter . $current_before . $page_title . $current_after;
}
} elseif ( is_tag() ) {
echo $current_before . single_tag_title( '', false ) . $current_after;
} elseif ( is_author() ) {
GLOBAL $author;
$userdata = get_userdata( $author );
echo $current_before . __( 'Posts by ', '__x__' ) . '“' . $userdata->display_name . $current_after . '”';
} elseif ( is_404() ) {
echo $current_before . __( '404 (Page Not Found)', '__x__' ) . $current_after;
} elseif ( is_archive() ) {
if ( x_is_shop() ) {
echo $current_before . $shop_title . $current_after;
} else {
echo $current_before . __( 'Archives ', '__x__' ) . $current_after;
}
}
echo '</div>';
}
}

simple html dom - cannot redeclair str_get_html()

Im trying to make a class to help with SEO and also compare google API with actual results
class:
<?php
class true_seo {
public $string, $amount;
private $arr;
public function __construct(){}
public function set_g_key( $key ) {
$this->g_key = $key;
}
public function set_phrase( $string ){
if( is_string ( $string ) ) {
$string = array( $string );
}
if( is_array ( $string ) ) {
$this->phrases = $string;
}else{
Throw new exception("incorect input for phrase, string or array");
}
}
public function get_sites_use_spider( $amount ) {
require "simple_html_dom.php";
$main_result = array();
foreach( $this->phrases as $phrase ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => 0, "maxResults" => $amount, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
$data = true_seo::google_search_api( $APIparams, 'http://www.google.co.uk/search', false );
new simple_html_dom();
$html = str_get_html( $data );
$result = array();
foreach( $html->find('li.g h3 a') as $g ) {
$data = $g->parent()->nextSibling();
$other = $data->find('span a');
$x = 0;
foreach( $other as $d ) {
( $x == 0 ? $cache = $d->href : $simular = $d->href );
$x++;
}
$excess_span = $data->find('span',0)->outertext;
if( isset( $data->find('div',0)->tag ) ) {
$excess_div = $data->find('div',0)->outertext;
$title = str_replace( array( $excess_span, $excess_div, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','','' ), $data->outertext );
}else{
$title = str_replace( array( $excess_span, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','' ), $data->outertext );
}
$result[] = array( 'link' => $g->href, 'title' => strip_tags( $title ), 'cache' => $cache, 'simular' => 'http://www.google.co.uk' . $simular );
}
$main_result[$phrase] = $result;
$html->clear();
}
$this->non_api_data = $main_result;
}
public function get_sites_use_api( $amount ) {
$arr = array();
foreach( $this->phrases as $phrase ) {
if( $amount > 4 ) {
$times = $amount / 4;
}else{
$times = 1;
}
$arg = array();
for($x = 0; $x < $times; $x++ ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => ($x * 4), "maxResults" => 4, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
if( $data = true_seo::google_search_api( $APIparams, 'http://ajax.googleapis.com/ajax/services/search/web' ) ) {
$arg = array_merge($arg, $data->responseData->results);
}else{
Throw new exception("Request error: no results returned from Google.");
}
}
$arg = array_reverse( $arg );
$remove = $amount % 4;
if( $amount < 4 ) {
$remove = 4 - $amount;
}
for( $x=0; $x < $remove; $x++ ) {
unset( $arg[$x] );
}
$arg = array_reverse( $arg );
foreach( $arg as $g ) {
$result = array( 'link' => $g->url, 'title' => strip_tags( $g->content ), 'cache' => $g->cacheUrl, 'simular' => 'na' );
$arr[$phrase][] = $result;
}
}
$this->api_data = $arr;
}
public function google_search_api($args, $url, $api = true){
if ( !array_key_exists('v', $args) ) {
$args['v'] = '1.0';
}
$url .= '?'.http_build_query($args, '', '&');
if( $result = #file_get_contents($url) ) {
if( $api == true ) {
return json_decode($result);
}else{
return $result;
}
}else{
Throw new exception("No data returned from url: $url");
}
}
public function set_get_actual( $string ) {
$this->actual->name = $string;
$this->actual->data = file_get_contents( $string );
}
public function get_actual_description(){
require_once "simple_html_dom.php";
new simple_html_dom();
$html = str_get_html( $this->actual->data );
return $html->find('head meta[name=description]',0)->content;
$html->clear();
}
}
?>
called by :
<?php
try{
require "./classes/class_true_seo.php";
$seo = new true_seo();
$seo->set_g_key('ABQIAAAAsWzmZ4RXdIk0a-LqpqKCBRSl_WmKnmsXGmN0kkjN2wkrfEOY-hT2sL-_x5v4NtT3DgElKNsR7FDJDQ');
$seo->set_phrase(array("web design mansfield"));
$seo->get_sites_use_api(10);
ob_start();
foreach( $seo->api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
$seo->set_get_actual( $results['link'] );
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
#echo "<p>" . $seo->get_actual_title() . "</p>";
echo "<p>" . $seo->get_actual_description() . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('p') . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('h2') . "</p>";
}
}
$api_return = ob_get_clean();
ob_start();
$seo->get_sites_use_spider(10);
foreach( $seo->non_api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
}
}
$non_api_return = ob_get_clean();
}catch(Exception $err){
$error = $err->getMessage();
}
?>
My problem being that I keep getting the error:
Fatal error: Cannot redeclare file_get_html() (previously declared in C:\wamp\www\seo\classes\simple_html_dom.php:37) in C:\wamp\www\seo\classes\simple_html_dom.php on line 41
which is due to the last function in the class get_actual_description().
Can anyone see where im cocking up?
regards,
Phil
put require_once "simple_html_dom.php" outside of function

Resources