Best approach to preventing array_rand from generating the same link? - php

This is a follow-up question from here.
The code is working great, it randoms the link to a blog post. The downside is that I may fall into the same post twice or too often.
header("Location: ".$posts[array_rand ($posts)][1]);
I need it to not fall on the same post more than once every 20 minutes, if runs out of posts then say: "Out of options. Come back in 20 minutes.". I tried doing it with cookies like so:
$rlink = $posts[array_rand ($posts)][1];
setcookie("rlink", "$rlink", time()+1200);
if ($rlink == $_COOKIE["rlink"]) {
header('Location: http://localhost/randommyblog.php');
} else
{
header("Location: ".$rlink);
}
It might be obvious that the problem here is that I'm replacing the cookie "rlink" every time, rendering the previous one useless.
Little help, please?

Try something like this, worked when I tested it as is:
$posts = array("hello", "world", "it's", "me" );
$len_posts = count( $posts );
$set_indices = #$_COOKIE['rlink'];
$rand = mt_rand( 0, $len_posts - 1 ); //Select a random index from the post
if( !empty( $set_indices ) )
{
$set_indices = array_map( "intval", explode( ",", $set_indices ) );
$len_indices = count( $set_indices );
if( $len_indices >= $len_posts )
{
die("no posts for you");
}
else
{
while( in_array( $rand, $set_indices, TRUE ) ) //Calculate a new index that has not been shown.
{
$rand = mt_rand( 0, $len_posts - 1 );
}
}
}
else
{
$set_indices = array();
}
array_push( $set_indices, $rand );
setcookie( "rlink", implode( ",", $set_indices ), time()+1200 ); //Set cookie of the shown indices like "3,0,1" for example.
echo $posts[$rand];

Related

Frequent Logouts in Mediawiki project

Working on mediawiki project facing the issue of frequent logouts where user when login and try some edit and update operation then after spending few minutes users gets automatically signout and redirects it to login page.
I have tried the following approaches added in LocalSettings.php but none of them works-
APPROACH-1
$wgMainCacheType = CACHE_ACCEL; $wgSessionCacheType = CACHE_DB;
APPROACH-2
$wgSessionTimeout = 604800; $wgCachePages = false;
APPROACH-3
ini_set('session.gc_maxlifetime', 604800); session_set_cookie_params(604800); session_start();
APPROACH-4
$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', time() ) ); header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 3600 ) . ' GMT' ); header( 'Cache-Control: public, max-age=3600' );
APPROACH-5
$wgParserCacheType = CACHE_NONE; $wgCachePages = false;
I have also used one approach where i have go inside the docker and commented out the following code in mediawiki/includes/Setup.php which is somehow works but triggered one more issue.
if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
// If session.auto_start is there, we can't touch session name
if ( $wgPHPSessionHandling !== 'disable' && !wfIniGetBool( 'session.auto_start' ) ){
session_name($wgSessionName ?: $wgCookiePrefix . '_session');
HeaderCallback::warnIfHeadersSent();
}
//Create the SessionManager singleton and set up our session handler,
// unless we're specifically asked not to.
if ( !defined( 'MW_NO_SESSION_HANDLER' ) ) {
MediaWiki\Session\PHPSessionHandler::install(
MediaWiki\Session\SessionManager::singleton()
);
}
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
// Initialize the session
try {
$session = MediaWiki\Session\SessionManager::getGlobalSession();
} catch ( MediaWiki\Session\SessionOverflowException $ex ) {
// The exception is because the request had multiple possible
// sessions tied for top priority. Report this to the user.
$list = [];
foreach ( $ex->getSessionInfos() as $info ) {
$list[] = $info->getProvider()->describe( $contLang );
}
$list = $contLang->listToText( $list );
throw new HttpError( 400,
Message::newFromKey( 'sessionmanager-tie', $list )->inLanguage( $contLang )
);
}
unset( $contLang );
if ( $session->isPersistent() ) {
$wgInitialSessionId = $session->getSessionId();
}
$session->renew();
if ( MediaWiki\Session\PHPSessionHandler::isEnabled() &&
( $session->isPersistent() || $session->shouldRememberUser() ) &&
session_id() !== $session->getId()
) {
// Start the PHP-session for backwards compatibility
if ( session_id() !== '' ) {
wfDebugLog( 'session', 'PHP session {old_id} was already started, changing to {new_id}', 'all', [
'old_id' => session_id(),
'new_id' => $session->getId(),
] );
session_write_close();
}
session_id( $session->getId() );
session_start();
}
unset( $session );
} else {
// Even if we didn't set up a global Session, still install our session
// handler unless specifically requested not to.
if ( !defined( 'MW_NO_SESSION_HANDLER' ) ) {
MediaWiki\Session\PHPSessionHandler::install(
MediaWiki\Session\SessionManager::singleton()
);
}
}
After removing the above code frequent logouts problem is solved in mediawiki project.
after removing the above code inside Setup.php
Can anyone assist here?

Custom trim my post title item combine two if

based on this example
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_excerpt' == $item['source'] ) {
$value = substr( $value, 0, 120 );
}
return $value;
}, 10, 2 );
I would like to trim post_title to a maximum limit of 120 with the (...) at the end.
This objective was not achieved with my modification and I failed several times with several combinations
// my actual attemp to trim post_title item
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_title' == $item['source'] ) {
$raw_value = $item['source'];
if ( 120 < strlen( $raw_value ) ) {
$value['source'] = substr( $raw_value, 0, 120 ) . "...";
}
}
return $value;
}, 10, 2 );
following the logic of the things based on this exemple mentioned here
// Add the following to your theme's functions.php
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'aufsichtsbehoerden' == $params['facet_name'] ) {
$raw_value = $params['facet_value'];
if ( 50 < strlen( $raw_value ) ) {
$params['facet_value'] = substr( $raw_value, 0, 50 ); // cut off some of the value
}
}
return $params;
}, 10, 2 );
Experts in PHP and use filters are requested to join me to tell me what I missed as a modification ..
Nest your if statements, and add some way of tracking the bug.
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_title' == $item['source']) {
$maxLength = 120;
echo "POST TITLE == ITEM SOURCE";
if (strlen($value) > $maxLength) {
echo "POST TITLE EXCEEDED MAX LENGTH";
$value = substr( $value, 0, $maxLength) . '…';
}
}
return $value;
}, 10, 2 );
I have added two echo functions to allow you to understand where and when the conditions are met.
Your first if is doing nothing in the first code. Notice that there are nothing between its brackets { }.
Your second code is combining both conditions correctly. If it doesn't work, 'post_title' is probably not equal to $item['source'].

Improving the speed of a custom infinite scroll

I have a custom infinite scroll that is working perfectly but it's really slow. Here is the script that handles the ajax request:-
function ga_infinite_scroll() {//trigger this on infinite scroll
add_filter( 'woocommerce_get_price_html', 'ga_show_price' );//filter to fix price range
if(empty($_POST['search_term'] )){
$params = json_decode( stripslashes( $_POST['query'] ), true );
$params['post_status'] = 'publish';
$params['posts_per_page'] = get_option('posts_per_page');
$params['post_type'] = 'product';
$params['paged'] = $_POST['page'] + 1; // we need next page to be loaded
}
else{//search logic here
$search_query = json_decode( stripslashes( $_POST['search_posts'] ), true );
$search_query['post_status'] = 'publish';
$search_query['posts_per_page'] = get_option('posts_per_page');
$search_query['paged'] = $_POST['page'] + 1;
wc_set_loop_prop( 'total', $_POST['search_count'] );
$params = $search_query;
}
ob_start();
query_posts( $params);
if ( have_posts() ) {//product loop
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
wc_get_template_part( 'content', 'product' );
}
}
}
$data = ob_get_clean();
die($data);
exit;
}
add_action( 'wp_ajax_ga_infinite_scroll', 'ga_infinite_scroll' );
add_action( 'wp_ajax_nopriv_ga_infinite_scroll', 'ga_infinite_scroll' );
Here's another post with my brief description of the problem Improving the performance of a custom developed scroll. Here is the code for ga_show_price.
function ga_show_price( $price ) {
global $post, $product, $reg_price_field_slug, $sale_price_field_slug, $user_currency, $wp_query,$wp_object_cache;
if( count($product->get_children()) !== 0 ) {
$variations = $product->get_children();
$regularPriceList = [];
$salePriceList = [];
$lowestPrice;
$salePrice;
// die("here");
if( $product->is_on_sale() ) {
// NOTE: ADD caching HERE!!
if( false === get_transient( 'sales_price' ) ) {
foreach( $variations as $variation ) {
array_push($salePriceList, get_post_meta( $variation, $reg_price_field_slug, true ) );
}
set_transient( 'sales_price', $salePriceList, 12 * HOUR_IN_SECONDS );
}
else{
$salePriceList = get_transient( 'sales_price');
}
$salePrice = min($salePriceList);
$price = add_proper_decimal($salePrice);
return get_woocommerce_currency_symbol() . $price . ' ' . $user_currency;
} else {
// NOTE: ADD caching HERE!!
if( false === get_transient( 'reg_price' ) ) {
foreach( $variations as $variation ) {
array_push($regularPriceList, get_post_meta( $variation, $reg_price_field_slug, true ) );
}
set_transient( 'reg_price', $regularPriceList, 12 * HOUR_IN_SECONDS );
}
else{
$regularPriceList = get_transient( 'reg_price');
}
$lowestPrice = min($regularPriceList);
$price = add_proper_decimal($lowestPrice);
return get_woocommerce_currency_symbol() . $price . ' ' . $user_currency;
}
} else {
$price = get_post_meta( $post->ID, $reg_price_field_slug, true );
$price = add_proper_decimal($price); // pr( $price );
if ( $price == '0.00' ) {
return 'Call for Price';
}
return get_woocommerce_currency_symbol() . $price . ' ' . $user_currency;
}
}
My javascript is here:-
jQuery(document).ready( function($) {
var url = window.location.origin + '/wp-admin/admin-ajax.php',
canBeLoaded=true,
bottomOffset = 2000; // the distance (in px) from the page bottom when you want to load more posts
$(window).scroll(function(){
var data = {
'action': 'ga_infinite_scroll',
'query': my_ajax_object.posts,
'page' : my_ajax_object.current_page,
//'search_results' : my_ajax_object.ga_search_results,
'search_count' : my_ajax_object.ga_search_count,
'search_posts': my_ajax_object.ga_search_posts,
'search_term' : my_ajax_object.ga_search_term,
'user_currency': my_ajax_object.user_currency,
'reg_price_slug': my_ajax_object.reg_price_field_slug
};
if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){
$.ajax({//limit the ajax calls
url : url,
data:data,
type:'POST',
beforeSend: function( xhr ){
// you can also add your own preloader here
// you see, the AJAX call is in process, we shouldn't run it again until complete
//console.log(data.search_term);
$('#ajax-loader').show();
canBeLoaded = false;
},
success:function(data){
if( data ) {
$('#multiple-products .columns-3 .products ').find('li:last-of-type').after( data ); // where to insert posts
//console.log(url);
canBeLoaded = true; // the ajax is completed, now we can run it again
my_ajax_object.current_page++;
$('#ajax-loader').hide();
}
else{
$('#ajax-loader').html('End of products...').delay(1000).fadeOut();
return;
}
}
});
}
});
//setting if it's a search
});
Is there a way that i can use this woocommerce_get_price_html filter outside of the ajax request handling script(ga_infinite_scroll) as it's really costly in terms of speed to use it inside the ajax handling script? I tried using transients at the ga_show_price(). How to implement other types of caching here to increase the speed of the infinite scroll?
So using transients is probably the best "simple" answer here without doing some major rework. However there's a couple issues with your ga_show_price() function.
So you want to always minimise the amount of database calls or long lengthy functions you call from your code to make things faster.
Transients have GLOBAL names. So if you use something called sales_price for one product, as soon as you use it for another product it will still hold the value of the previous product. What you'll probably have to do is generate a unique name for all your transients. Something like: set_transient('price_'.$product->getSKU(), ...).
$variations = $product->get_children(); - You're loading the $variations variable with all the children of the product, which probably takes quite a while and involves quite a few db calls, then if you already have a transient for this product, the variations are never used!. Only run this line if you dont already have a cached value for the product.
A smaller issue, but you are calling get_transient twice every time you have a cached value. Once to check that it's not false, and then again to actually retrieve the value. Might seem like a small thing, but if you have 100+ products loading in, it adds up.
I like to do this with my transients:
$value = get_transient('something');
if ($value === false)
{
$value = some_long_calculation();
set_transient('something', $value, ...);
}
//Now use $value here.
Get even more aggressive with your caching. How often do items change from being on sale to not being on sale? Not more than once a day? Then just cache the entire function's calculation instead of first checking if it has a sales price or regular price.
A word of warning: Transients have a maximum length for their names and values so you cant store too much in them. Also they're designed for only storing a few values, not a price for every single product in your system.
If that is the case you're probably better off thinking about caching the value in a custom field for each product? You could attach hooks so that every time the product is updated, it updates the calculated price custom field automatically.
#Mikepote's suggestions for ga_price increased the speed but editing the main product loop based on unique transient increased speed more. I hereby attach my code:-
if( empty(get_transient('ga_loop_products_'.md5(serialize($params))))){ //using md5 and serialize(for 32digit) to assign a unique name to the given set of params
query_posts( $params);
ob_start();
add_filter( 'woocommerce_get_price_html', 'ga_show_price' );//filter to fix price range
if ( have_posts() ) {//product loop
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
wc_get_template_part( 'content', 'product' );
}
}
}
$data = ob_get_clean();
// $ga_loop = get_transient('ga_loop_products_'.md5(serialize($params)));
set_transient( 'ga_loop_products_'.md5(serialize($params)), $data, 24 * 60 ); // 1 day cache
}
else{
$data= get_transient('ga_loop_products_'.md5(serialize($params)));
}
wp_reset_query();

need help on a piece of code from PHP 5 Social Networking

I'm trying to get more advanced with php and I pick up the book PHP 5 Social Networking by Michael Peacock. While the book seemed to be interesting it didn't however get to involved in the details of the code. The function I'm trying to figure out is,
public function getURLData()
{
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
$data = explode( '/', $urldata );
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
$this->urlBits = $this->array_trim( $data );
}
}
This a part of a larger class and the $_GET['page'] is something like this: relationships/mutual/3. My main question is what is happening in the else section. I think what is happening that it's removing any empty array indexes but I also question that.
Any help would be appreciated.
EDIT: added array_trim function that is also part of the class
private function array_trim( $array )
{
while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
{
array_shift( $array );
}
while ( !empty( $array ) && strlen( end( $array ) ) === 0)
{
array_pop( $array );
}
return $array;
}
public function getURLData()
{
Gets the 'page', this data can be obtained by $_GET from the url: for instance: http://mysite.com/?page=contact
If 'page' has been set, is assigned to $urldata, else $urldata=''
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
Now is creating an array with all the substrings from $urldata splited by '/'
$data = explode( '/', $urldata );
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the first element is equal to 0, then removes the first element from the array.
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the last element is equal to 0, then removes the last element from the array.
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
array_trim is a custom function, not sure what does but probably will do some kind of trimming too
$this->urlBits = $this->array_trim( $data );
}
}

Array Splice does not preserve "trail_end"

I am a newbie and have a piece of Wordpress code which has a glitch in it.
Sorry if my question is silly, though the support from where I bought the theme seems to answer with a great delay.
I will paste it and maybe someone can help. If no one can figure out the problem I will delete the question.
The code adds a few changes to the breadcrumb navigation on my site.
The problem I think is with this:
// Add the trail back on to the end.
$links[] = $trail['trail_end'];
// Add the new links, and the original trail's end, back into the trail.
array_splice( $trail, 1, count( $trail ) - 1, $links );
These two line of code should add a <span class="trail-end"></span> to the end of the breadcrumbs around the words "Xpand Xtreme Pump" (in my link example)
Here is the code:
function woo_custom_breadcrumbs_trail_add_product_categories ( $trail ) {
if ( ( get_post_type() == 'product' ) && is_singular() ) {
global $post;
$taxonomy = 'product_cat';
$terms = get_the_terms( $post->ID, $taxonomy );
$links = array();
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $c ) {
$parents = woo_get_term_parents( $c->term_id, $taxonomy, true, ', ', $c->name, array() );
if ( $parents != '' && ! is_wp_error( $parents ) ) {
$parents_arr = explode( ', ', $parents );
foreach ( $parents_arr as $p ) {
if ( $p != '' ) { $links[] = $p; }
}
}
}
// Add the trail back on to the end.
$links[] = $trail['trail_end'];
// Add the new links, and the original trail's end, back into the trail.
array_splice( $trail, 1, count( $trail ) - 1, $links );
}
}
return $trail;
} // End woo_custom_breadcrumbs_trail_add_product_categories()
Try this:
...
// Wrap the trail_end with your span tag
$trail['trail_end'] = '<span class="trail-end">' . end($trail) . '</span>';
// Add the trail back on to the end.
$links[] = $trail['trail_end'];
...
Remember, that this is a hack, I'd recommend doing this in the template rendering the bread-crumbs.

Categories