add hook in product search query - php

I'm developing a plugin for my shopping site. The plugin works like this. Color variations of the same product are on different product pages. The products are connected together as shown in the photo below.
and on the product page as small photos, it is shown as color variants of the product.
It works flawlessly so far.
but I am using multi-vendor plugin on my website.
. I want it to search only the products belonging to the author, the user, that is the store, while selecting the product.
for this, is it possible to add a filter to the query made in the ajax request and rewrite the query?

add_filter('woocommerce_product_pre_search_products', 'woocommerce_product_pre_search_products', 10, 6);
function woocommerce_product_pre_search_products( $custom_query=false, $term, $type, $include_variations, $all_statuses, $limit) {
global $wpdb;
$post_types = $include_variations ? array( 'product', 'product_variation' ) : array( 'product' );
$join_query = '';
$type_where = '';
$status_where = '';
$limit_query = '';
// When searching variations we should include the parent's meta table for use in searches.
if ( $include_variations ) {
$join_query = " LEFT JOIN {$wpdb->wc_product_meta_lookup} parent_wc_product_meta_lookup
ON posts.post_type = 'product_variation' AND parent_wc_product_meta_lookup.product_id = posts.post_parent ";
}
/**
* Hook woocommerce_search_products_post_statuses.
*
* #since 3.7.0
* #param array $post_statuses List of post statuses.
*/
$post_statuses = apply_filters(
'woocommerce_search_products_post_statuses',
current_user_can( 'edit_private_products' ) ? array( 'private', 'publish' ) : array( 'publish' )
);
// See if search term contains OR keywords.
if ( stristr( $term, ' or ' ) ) {
$term_groups = preg_split( '/\s+or\s+/i', $term );
} else {
$term_groups = array( $term );
}
$search_where = '';
$search_queries = array();
foreach ( $term_groups as $term_group ) {
$search_terms = array( $term_group );
$term_group_query = '';
$searchand = '';
foreach ( $search_terms as $search_term ) {
$like = '%' . $wpdb->esc_like( $search_term ) . '%';
// Variations should also search the parent's meta table for fallback fields.
if ( $include_variations ) {
$variation_query = $wpdb->prepare( " OR ( wc_product_meta_lookup.sku = '' AND parent_wc_product_meta_lookup.sku LIKE %s ) ", $like );
} else {
$variation_query = '';
}
$term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( posts.post_excerpt LIKE %s) OR ( posts.post_content LIKE %s ) OR ( wc_product_meta_lookup.sku LIKE %s ) $variation_query)", $like, $like, $like, $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$searchand = ' AND ';
}
if ( $term_group_query ) {
$search_queries[] = $term_group_query;
}
}
if ( ! empty( $search_queries ) ) {
$search_where = ' AND (' . implode( ') OR (', $search_queries ) . ') ';
}
if ( ! empty( $include ) && is_array( $include ) ) {
$search_where .= ' AND posts.ID IN(' . implode( ',', array_map( 'absint', $include ) ) . ') ';
}
if ( ! empty( $exclude ) && is_array( $exclude ) ) {
$search_where .= ' AND posts.ID NOT IN(' . implode( ',', array_map( 'absint', $exclude ) ) . ') ';
}
$search_where .= ' AND posts.post_author='. get_current_user_id();
if ( 'virtual' === $type ) {
$type_where = ' AND ( wc_product_meta_lookup.virtual = 1 ) ';
} elseif ( 'downloadable' === $type ) {
$type_where = ' AND ( wc_product_meta_lookup.downloadable = 1 ) ';
}
if ( ! $all_statuses ) {
$status_where = " AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "') ";
}
if ( $limit ) {
$limit_query = $wpdb->prepare( ' LIMIT %d ', $limit );
}
// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
$search_results = $wpdb->get_results(
// phpcs:disable
"SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
$join_query
WHERE posts.post_type IN ('" . implode( "','", $post_types ) . "')
$search_where
$status_where
$type_where
ORDER BY posts.post_parent ASC, posts.post_title ASC
$limit_query
"
// phpcs:enable
);
$product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
if ( is_numeric( $term ) ) {
$post_id = absint( $term );
$post_type = get_post_type( $post_id );
if ( 'product_variation' === $post_type && $include_variations ) {
$product_ids[] = $post_id;
} elseif ( 'product' === $post_type ) {
$product_ids[] = $post_id;
}
$product_ids[] = wp_get_post_parent_id( $post_id );
}
return wp_parse_id_list( $product_ids );
}

Related

How to move part of text to another line in PHP

Here is the code I am using:
// Display the product variation price inside the variations dropdown. add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) {
return $term;
}
if ( empty( $product->id ) ) {
return $term;
}
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( ! empty( $result ) ) ? $result[0] : $term;
$query = "
SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return '' . $term . '' . ' - (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ' )';
}
return $term;
}
And this is how it's in front: Screenshot
Could anybody tell me how can I move everything starting from "-" to the next line?
It has to look this way:
38 Euro
20 0000
You can change the return part to this:
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return '' . $term . '' . ' <br/> ' . wp_kses( woocommerce_price( $_product->get_price(), array() ) . ' )';
}
return $term;
Also, edit your code in the part that returns the HTML to unescape the $name variable:
return sprintf( '<li class="swatch swatch-label swatch-%s %s %s %s %s" title="%s" data-value="%s" data-tooltip_type="%s" data-tooltip_value="%s"><span class="swatch-inner swatch-label-inner">%s</span></li>', esc_attr( $slug ), esc_attr( $selected ), esc_attr( $style ), esc_attr( $flex_mode_class ), esc_attr( $tooltip_class ), $name, esc_attr( $slug ), esc_attr( $tooltip ), esc_attr( $tooltip_value ), $name );
In addition, to not break your grid, you will need to add this css to your stylesheet, or in the theme setting, in Custom CSS:
.xt_woovs-single-product .xt_woovs-swatches .swatch.swatch-label {
height: auto !important;
text-align: left !important;
}

Custom wp_query function - search post content

I have a php function that extends the default WP default search abilities (title & inside post content), it can now look in taxonomies / attributes too.
However, using the function results in the search query not looking inside post content, which is a feature I would like to retain. Would anyone be able to help tweak the below so the search still looks inside post content? Thank you
function and_extend_search( $search, &$wp_query ) {
global $wpdb;
if ( empty( $search ) ) {
return $search;
}
$terms = $wp_query->query_vars['s'];
$exploded = explode( ' ', $terms );
if ( $exploded === false || count( $exploded ) == 0 ) {
$exploded = array( 0 => $terms );
}
$search = '';
foreach ( $exploded as $tag ) {
$search .= " AND ( ($wpdb->posts.post_title LIKE '%$tag%') OR EXISTS ( SELECT * FROM $wpdb->term_relationships LEFT JOIN $wpdb->terms ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id WHERE $wpdb->terms.name LIKE '%$tag%' AND $wpdb->term_relationships.object_id = $wpdb->posts.ID ) )";
}
return $search;
}
You have three errors here.
You put the parameter in your file as &$wp_query but you don't use this syntax in this case.
You clear out search $search = ''; before you try to concatenate it.
Your condition is AND instead of OR - Unless for some reason you're trying to limit it?
function and_extend_search( $search, $wp_query ) {
global $wpdb;
if ( empty( $search ) ) {
return $search;
}
$terms = $wp_query->query_vars['s'];
$exploded = explode( ' ', $terms );
if ( false === $exploded || 0 === count( $exploded ) ) {
$exploded = array( 0 => $terms );
}
foreach ( $exploded as $tag ) {
$search .= " OR ( ($wpdb->posts.post_title LIKE '%$tag%') OR EXISTS ( SELECT * FROM $wpdb->term_relationships LEFT JOIN $wpdb->terms ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id WHERE $wpdb->terms.name LIKE '%$tag%' AND $wpdb->term_relationships.object_id = $wpdb->posts.ID ) )";
}
return $search;
}
add_filter( 'posts_search', 'and_extend_search', 500, 2 );

How to display all custom product attributes with variations in WooCommerce

This code shows how to display custom product attributes for without variations. I wish to display all the variations too in the typical dropdown select menu. How do I do that?
P/S: I can't post the code here because this site says I have too much code and too little details.
UPDATED:
/**
* Show all product attributes on the product page
*/
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ($tax_object->labels->singular_name) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= $tax_label . ': ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
array_push( $tax_terms, $single_term );
}
$out .= implode(', ', $tax_terms) . '<br />';
} else {
$out .= $name . ': ';
$out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
}
}
echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);

Woocommerce sort columns by Event Time

I'm working with Wordpress, Woocommerce & Eventon plugin in order to create a selling tickets for events site. Users can order tickets for different dates.
I'm trying to implement a sortable column wich shows for every ticket sold the date for the event (stored in woocommerce_order_itemmeta table as meta_value of meta_key 'Event-Time'). So first of all I created and populated that column like this:
add_filter( 'manage_edit-shop_order_columns', 'wooc_set_custom_column_order_columns');
//create custom woocommerce columns
function wooc_set_custom_column_order_columns($columns) {#
$nieuwearray = array();
foreach($columns as $key => $title) {
if ($key=='billing_address') { // in front of the Billing column
$nieuwearray['order_product'] = __( 'Products', 'woocommerce' );
$nieuwearray['Event-Time'] = __( 'Reservation Date', 'woocommerce' );
}
$nieuwearray[$key] = $title;
}
return $nieuwearray ;
}
//populate custom woocommerce columns
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column ) {
global $post, $woocommerce, $the_order;
switch ( $column ) {
case 'order_product' :
$terms = $the_order->get_items();
if ( is_array( $terms ) ) {
foreach($terms as $term)
{
echo $term['item_meta']['_qty'][0] .' x ' . $term['name'] .'<br />';
}
} else {
_e( 'Unable get the product', 'woocommerce' );
}
break;
case 'Event-Time' :
$terms = $the_order->get_items();
if ( is_array( $terms ) ) {
foreach($terms as $term) {
if(array_key_exists( 'Event-Time', $term ) ){
$date_event = substr($term['item_meta']['Event-Time'][0], 0, 10);
echo $date_event . '<br />';
} else {
echo '<b>' . __('No existe fecha reserva?', 'woocommerce') . '</b>';
}
}
} else {
_e( 'Unable get the product', 'woocommerce' );
}
break;
}
}
In order to make Event-Time column sortable, I added this code:
add_filter( "manage_edit-shop_order_sortable_columns", 'sort_columns_woocommerce' );
function sort_columns_woocommerce( $columns ) {
$custom = array(
//'order_producten' => 'MY_COLUMN_1_POST_META_ID',
'Event-Time' => 'Event-Time'
);
return wp_parse_args( $custom, $columns );
}
but nothing happened, so after a few hours looking it up on Google, I found similar solution that I tried to adapt to my case:
function order_by_event_time_join($query) {
global $wpdb;
if ( is_admin() && (isset($_GET['post_type']) && $_GET['post_type'] === 'shop_order') && (isset($_GET['orderby']) && $_GET['orderby'] === 'Event-Time') ) {
$first_item_in_order = "SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = $wpdb->posts.ID AND order_item_type = 'line_item' LIMIT 0, 1";
$query .= "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS items ON $wpdb->posts.ID = items.order_id AND items.order_item_id = ($first_item_in_order) ";
$query .= "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id AND (itemmeta.meta_key = 'Event-Time') ";
}
return $query;
}
add_filter('posts_join', 'order_by_event_time_join');
function order_by_event_time_where($where) {
global $wpdb;
if( is_admin() && $_GET['post_type'] === 'shop_order' && (isset($_GET['orderby']) && $_GET['orderby'] === 'Event-Time') ) {
if(strpos($where, 'shop_webhook')) {
return " AND $wpdb->posts.post_type = 'shop_order' AND itemmeta.meta_key = 'Event-Time'";
}
}
return $where;
}
add_filter('posts_where', 'order_by_event_time_where');
But no luck... What I understand the problem is I'm not able to sort columns by the meta_value 'Event-Time' in the ticket-orders section of woocommerce...
I can show Event-Time data but impossible to sort/filter it.

Modify code to change timestamp timezone in sitemap

Below is the code from a plugin I use for sitemaps.
I would like to know if there's a way to "enforce" a different timezone on all date variable and functions in it.
If not, how do I modify the code to change the timestamp to reflect a different timezone? Say for example, to America/New_York timezone.
Please look for date to quickly get to the appropriate code blocks. Code on Pastebin.
<?php
/**
* #package XML_Sitemaps
*/
class WPSEO_Sitemaps {
.....
/**
* Build the root sitemap -- example.com/sitemap_index.xml -- which lists sub-sitemaps
* for other content types.
*
* #todo lastmod for sitemaps?
*/
function build_root_map() {
global $wpdb;
$options = get_wpseo_options();
$this->sitemap = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$base = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '';
// reference post type specific sitemaps
foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) {
if ( $post_type == 'attachment' )
continue;
if ( isset( $options['post_types-' . $post_type . '-not_in_sitemap'] ) && $options['post_types-' . $post_type . '-not_in_sitemap'] )
continue;
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type ) );
// don't include post types with no posts
if ( !$count )
continue;
$n = ( $count > 1000 ) ? (int) ceil( $count / 1000 ) : 1;
for ( $i = 0; $i < $n; $i++ ) {
$count = ( $n > 1 ) ? $i + 1 : '';
if ( empty( $count ) || $count == $n ) {
$date = $this->get_last_modified( $post_type );
} else {
$date = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s ORDER BY post_modified_gmt ASC LIMIT 1 OFFSET %d", $post_type, $i * 1000 + 999 ) );
$date = date( 'c', strtotime( $date ) );
}
$this->sitemap .= '<sitemap>' . "\n";
$this->sitemap .= '<loc>' . home_url( $base . $post_type . '-sitemap' . $count . '.xml' ) . '</loc>' . "\n";
$this->sitemap .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
$this->sitemap .= '</sitemap>' . "\n";
}
}
// reference taxonomy specific sitemaps
foreach ( get_taxonomies( array( 'public' => true ) ) as $tax ) {
if ( in_array( $tax, array( 'link_category', 'nav_menu', 'post_format' ) ) )
continue;
if ( isset( $options['taxonomies-' . $tax . '-not_in_sitemap'] ) && $options['taxonomies-' . $tax . '-not_in_sitemap'] )
continue;
// don't include taxonomies with no terms
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count != 0 LIMIT 1", $tax ) ) )
continue;
// Retrieve the post_types that are registered to this taxonomy and then retrieve last modified date for all of those combined.
$taxobj = get_taxonomy( $tax );
$date = $this->get_last_modified( $taxobj->object_type );
$this->sitemap .= '<sitemap>' . "\n";
$this->sitemap .= '<loc>' . home_url( $base . $tax . '-sitemap.xml' ) . '</loc>' . "\n";
$this->sitemap .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
$this->sitemap .= '</sitemap>' . "\n";
}
// allow other plugins to add their sitemaps to the index
$this->sitemap .= apply_filters( 'wpseo_sitemap_index', '' );
$this->sitemap .= '</sitemapindex>';
}
/**
* Build a sub-sitemap for a specific post type -- example.com/post_type-sitemap.xml
*
* #param string $post_type Registered post type's slug
*/
function build_post_type_map( $post_type ) {
$options = get_wpseo_options();
............
// We grab post_date, post_name, post_author and post_status too so we can throw these objects into get_permalink, which saves a get_post call for each permalink.
while ( $total > $offset ) {
$join_filter = apply_filters( 'wpseo_posts_join', '', $post_type );
$where_filter = apply_filters( 'wpseo_posts_where', '', $post_type );
// Optimized query per this thread: http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-performance-suggestion
// Also see http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
$posts = $wpdb->get_results( "SELECT l.ID, post_content, post_name, post_author, post_parent, post_modified_gmt, post_date, post_date_gmt
FROM (
SELECT ID FROM $wpdb->posts {$join_filter}
WHERE post_status = 'publish'
AND post_password = ''
AND post_type = '$post_type'
{$where_filter}
ORDER BY post_modified ASC
LIMIT $steps OFFSET $offset ) o
JOIN $wpdb->posts l
ON l.ID = o.ID
ORDER BY l.ID" );
/* $posts = $wpdb->get_results("SELECT ID, post_content, post_name, post_author, post_parent, post_modified_gmt, post_date, post_date_gmt
FROM $wpdb->posts {$join_filter}
WHERE post_status = 'publish'
AND post_password = ''
AND post_type = '$post_type'
{$where_filter}
ORDER BY post_modified ASC
LIMIT $steps OFFSET $offset"); */
$offset = $offset + $steps;
foreach ( $posts as $p ) {
$p->post_type = $post_type;
$p->post_status = 'publish';
$p->filter = 'sample';
if ( wpseo_get_value( 'meta-robots-noindex', $p->ID ) && wpseo_get_value( 'sitemap-include', $p->ID ) != 'always' )
continue;
if ( wpseo_get_value( 'sitemap-include', $p->ID ) == 'never' )
continue;
if ( wpseo_get_value( 'redirect', $p->ID ) && strlen( wpseo_get_value( 'redirect', $p->ID ) ) > 0 )
continue;
$url = array();
$url['mod'] = ( isset( $p->post_modified_gmt ) && $p->post_modified_gmt != '0000-00-00 00:00:00' ) ? $p->post_modified_gmt : $p->post_date_gmt;
$url['chf'] = 'weekly';
$url['loc'] = get_permalink( $p );
.............
}
/**
* Build a sub-sitemap for a specific taxonomy -- example.com/tax-sitemap.xml
*
* #param string $taxonomy Registered taxonomy's slug
*/
function build_tax_map( $taxonomy ) {
$options = get_wpseo_options();
..........
// Grab last modified date
$sql = "SELECT MAX(p.post_date) AS lastmod
FROM $wpdb->posts AS p
INNER JOIN $wpdb->term_relationships AS term_rel
ON term_rel.object_id = p.ID
INNER JOIN $wpdb->term_taxonomy AS term_tax
ON term_tax.term_taxonomy_id = term_rel.term_taxonomy_id
AND term_tax.taxonomy = '$c->taxonomy'
AND term_tax.term_id = $c->term_id
WHERE p.post_status = 'publish'
AND p.post_password = ''";
$url['mod'] = $wpdb->get_var( $sql );
$url['chf'] = 'weekly';
$output .= $this->sitemap_url( $url );
}
}
/**
* Build the <url> tag for a given URL.
*
* #param array $url Array of parts that make up this entry
* #return string
*/
function sitemap_url( $url ) {
if ( isset( $url['mod'] ) )
$date = mysql2date( "Y-m-d\TH:i:s+00:00", $url['mod'] );
else
$date = date( 'c' );
$output = "\t<url>\n";
$output .= "\t\t<loc>" . $url['loc'] . "</loc>\n";
$output .= "\t\t<lastmod>" . $date . "</lastmod>\n";
$output .= "\t\t<changefreq>" . $url['chf'] . "</changefreq>\n";
$output .= "\t\t<priority>" . str_replace( ',', '.', $url['pri'] ) . "</priority>\n";
if ( isset( $url['images'] ) && count( $url['images'] ) > 0 ) {
foreach ( $url['images'] as $img ) {
$output .= "\t\t<image:image>\n";
$output .= "\t\t\t<image:loc>" . esc_html( $img['src'] ) . "</image:loc>\n";
if ( isset( $img['title'] ) )
$output .= "\t\t\t<image:title>" . _wp_specialchars( html_entity_decode( $img['title'], ENT_QUOTES, get_bloginfo('charset') ) ) . "</image:title>\n";
if ( isset( $img['alt'] ) )
$output .= "\t\t\t<image:caption>" . _wp_specialchars( html_entity_decode( $img['alt'], ENT_QUOTES, get_bloginfo('charset') ) ) . "</image:caption>\n";
$output .= "\t\t</image:image>\n";
}
}
$output .= "\t</url>\n";
return $output;
}
/**
* Get the modification date for the last modified post in the post type:
*
* #param array $post_types Post types to get the last modification date for
* #return string
*/
function get_last_modified( $post_types ) {
global $wpdb;
if ( !is_array( $post_types ) )
$post_types = array( $post_types );
$result = 0;
foreach ( $post_types as $post_type ) {
$key = 'lastpostmodified:gmt:' . $post_type;
$date = wp_cache_get( $key, 'timeinfo' );
if ( !$date ) {
$date = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s ORDER BY post_modified_gmt DESC LIMIT 1", $post_type ) );
if ( $date )
wp_cache_set( $key, $date, 'timeinfo' );
}
if ( strtotime( $date ) > $result )
$result = strtotime( $date );
}
// Transform to W3C Date format.
$result = date( 'c', $result );
return $result;
}
}
global $wpseo_sitemaps;
$wpseo_sitemaps = new WPSEO_Sitemaps();
I believe you're looking for the date_default_timezone_set() function, which will force all date functions, including date(), to use the specified time zone.
date_default_timezone_set( 'America/New_York');
For your specific case to only have changes apply to the plugin, you should save the current timezone with date_default_timezone_get(), then set your own timezone, and revert it when you're done. So, you could do something like this:
class WPSEO_Sitemaps {
private $old_timezone = '';
private $new_timezone = 'America/New_York';
function setTimezone() {
$this->old_timezone = date_default_timezone_get();
date_default_timezone_set( $this->new_timezone);
}
function revertTimezone() {
date_default_timezone_set( $this->old_timezone);
}
function foo() {
$this->setTimezone();
date();
$tihs->revertTimezone();
}
}

Categories