'Catalog Sidebar',
'id' => 'ac_catalog_sidebar',
'before_widget' => '
',
'after_widget' => '
',
'before_title' => '',
'after_title' => '
',
) );
register_sidebar( array(
'name' => 'Single Product Sidebar',
'id' => 'ac_single_product_sidebar',
'before_widget' => '',
'after_widget' => '
',
'before_title' => '',
'after_title' => '
',
) );
}
/**
* Create pagination function for archives
*
* @param string $pages
* @param int $range
*/
function accp_product_pagination( $pages = '', $range = 2 ) {
$showitems = ( $range * 2 ) + 1;
global $paged;
if ( empty( $paged ) ) {
$paged = 1;
}
if ( $pages == '' ) {
global $wp_query;
$pages = $wp_query->max_num_pages;
if ( ! $pages ) {
$pages = 1;
}
}
if ( 1 != $pages ) {
echo "\n";
}
}
add_filter( 'admin_post_thumbnail_html', 'accp_change_featured_image_text' );
/*
* Change the featured image metabox link text
*/
function accp_change_featured_image_text( $content ) {
if ( 'ac_product' === get_post_type() ) {
$content = str_replace( 'Set featured image', __( 'Set product image', 'ac' ), $content );
$content = str_replace( 'Remove featured image', __( 'Remove product image', 'ac' ), $content );
}
return $content;
}
add_filter( 'gettext', 'accp_change_image_button', 10, 3 );
/**
* Change button text in Media uploader
*
* @param $translation
* @param $text
* @param $domain
*
* @return string
*/
function accp_change_image_button( $translation, $text, $domain ) {
if ( $text == 'Insert into post' ) {
// Once is enough.
remove_filter( 'gettext', 'accp_change_image_button' );
return 'Use this';
}
return $translation;
}
add_filter( 'views_edit-ac_product', 'accp_add_import_button', 10 );
/**
* Add import button in main products page
*
* @param $default
*
* @return mixed
*/
function accp_add_import_button( $default ) {
if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'ac_product' ) {
$string = '';
$string .= '';
echo $string;
}
return $default;
}
/**
* Add search ability to custom columns
*
* @param $query
*/
function accp_search_meta_products_in_lists( $query ) {
if ( is_admin() && ! empty( $query->get( 's' ) ) ) {
$variable_to_send = isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
wp_redirect( get_bloginfo( 'url' ) . '/wp-admin/options.php?page=ac-search&s=' . $variable_to_send );
exit;
}
}
add_action( 'pre_get_posts', 'accp_search_meta_products_in_lists' );
/**
* Parse complex CSV fields for import procedure
*
* @param $field
* @param string $title
*
* @return array
*/
function accp_parce_complex_cvs( $field, $title = '' ) {
preg_match_all( '/[^,]*[=>][^,]*/', $field, $matches );
$field_arr = array();
$brochures_names = array();
if ( sizeof( $matches[0] ) > 0 ) {
foreach ( $matches[0] as $el ) {
preg_match( '/^[^=>]*/', trim( $el ), $el_key );
preg_match( '/[^=>]*$/', trim( $el ), $el_value );
if ( isset( $el_key[0] ) && ! empty( $el_key[0] ) ) {
$field_arr[ trim( $el_key[0] ) ] = isset( $el_value[0] ) ? trim( $el_value[0] ) : '';
}
}
} else {
preg_match_all( '/[^,]*[^,]*/', $field, $matches2 );
foreach ( $matches2[0] as $el2 ) {
$el2 = trim( $el2 );
if ( isset( $el2 ) && ! empty( $el2 ) ) {
$field_arr[ $title ] = $el2;
}
}
}
return $field_arr;
}
/**
* Get Attachement id from a src value
*
* @param $image_src
*
* @return bool|string|null
*/
function get_attachment_id_from_src( $image_src ) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var( $query );
return ! empty( $id ) ? $id : false;
}
/**
* Get modify date for post by post id
*
* @param $id
*
* @return bool|string|null
*/
function get_post_modify_date_by_id( $id ) {
global $wpdb;
$query = "SELECT post_modified FROM {$wpdb->posts} WHERE ID='$id'";
$modify_date = $wpdb->get_var( $query );
return ! empty( $modify_date ) ? $modify_date : false;
}
/**
* Encode Urls Rules
*
* @param $string
*
* @return mixed
*/
function myUrlEncode( $string ) {
$entities = array(
'%21',
'%2A',
'%27',
'%28',
'%29',
'%3B',
'%3A',
'%40',
'%26',
'%3D',
'%2B',
'%24',
'%2C',
'%2F',
'%3F',
'%25',
'%23',
'%5B',
'%5D'
);
$replacements = array(
'!',
'*',
"'",
"(",
")",
";",
":",
"@",
"&",
"=",
"+",
"$",
",",
"/",
"?",
"%",
"#",
"[",
"]"
);
return str_replace( $entities, $replacements, urlencode( $string ) );
}
/**
* Change dinamically secondary images order if order 0 for all
*
* @param $image_order
* @param $arr
*
* @return int
*/
function accp_recursive_cheking( $image_order, $arr ) {
if ( array_key_exists( $image_order, $arr ) ) {
$image_order += 1;
return accp_recursive_cheking( $image_order, $arr );
} else {
return $image_order;
}
}
add_action( 'init', 'create_new_url_querystring' );
/**
*
*/
function create_new_url_querystring() {
add_rewrite_tag( '%sku-postname%', '([^/]*)' );
add_rewrite_rule(
'^products/([^/]*)_([^/]*)_([^/]*)$',
'index.php?sku-postname=$matches[1]=$matches[2]&products=$matches[3]',
'top'
);
add_rewrite_rule(
'^products/([^/]*)$',
'index.php?products=$matches[1]',
'top'
);
flush_rewrite_rules();
global $wp_query;
}
add_action( 'save_post', 'accp_add_query_vars' );
/**
* @param $id
*/
function accp_add_query_vars( $id ) {
global $wp_query;
$ac_product_details = get_post_meta( $id, 'ac-product-details', true );
if ( isset( $ac_product_details['sku'] ) && ! empty( $ac_product_details['sku'] ) ) {
$sku = $ac_product_details['sku'];
set_query_var( 'sku', $sku );
} else {
set_query_var( 'sku', '' );
}
}
add_filter( 'post_type_link', 'accp_get_perm_html', 1, 3 );
add_filter( 'post_link', 'accp_get_perm_html', 1, 3 );
/**
* Refactor the permalink for products
*
* @param $permalink
* @param $post
* @param $leavename
*
* @return mixed|string|string[]|void|null
*/
function accp_get_perm_html( $permalink, $post, $leavename ) {
if ( $post->post_type === 'ac_product' ) {
$ac_product_details = get_post_meta( $post->ID, 'ac-product-details', true );
if ( isset( $ac_product_details['sku'] ) && ! empty( $ac_product_details['sku'] ) ) {
$sku = $ac_product_details['sku'];
} else {
$sku = false;
}
$permalink = str_replace( '%sku-postname%', $post->post_name, $permalink );
if ( ! $sku ) {
if ( $post->post_type === 'ac_product' ) {
return site_url( 'products/' . $post->post_name );
}
$permalink = preg_replace( '/\/__/', '/', $permalink );
return site_url( $post->post_name );
}
return site_url( 'products/' . $post->post_name );
}
return $permalink;
}
/**
* Template files for archive and single views
*
* @param $template
*
* @return string
*/
function accp_plugin_templates( $template ) {
$taxonomies = array( 'ac_product_cat', 'ac_product_tag' );
if ( is_post_type_archive( 'ac_product' ) || is_tax( $taxonomies ) && file_exists( plugin_dir_path( __FILE__ ) . '../templates/archive-ac_product.php' ) ) {
$template = plugin_dir_path( __FILE__ ) . '../templates/archive-ac_product.php';
}
if ( is_singular( 'ac_product' ) && file_exists( plugin_dir_path( __FILE__ ) . '../templates/single-ac_product.php' ) ) {
$template = plugin_dir_path( __FILE__ ) . '../templates/single-ac_product.php';
}
return $template;
}
add_filter( 'template_include', 'accp_plugin_templates' );
/**
* Add mera description to products when available
*/
function accp_catalog_add_meta_description() {
global $post;
if ( $post != null ) {
$ac_product_details = get_post_meta( $post->ID, 'ac-product-details', true );
if ( isset( $ac_product_details['meta-description'] ) && ! empty( $ac_product_details['meta-description'] ) ) {
?>