Repeat publish/unpublish slides based on date with PHP

We made it possible to call in your functions for the cache refreshing and for the publishing/unpublishing of the slides, which will make it possible for example to have some number of slides, and show a different one in every week until there are no more, and start over again.

Important: These codes will only work, if after you put the php codes where you want them, press Refresh Cache at the slider or at the global settings.

Cache refreshing

Our slides aren't refreshing their caches, unless you save on them in the backend (or if you are using dynamic slide generator), so once the code runs, which decides what should be in the slider, it won't run again, which isn't good for you, if you want to change it in every week. This is why you will need to trigger a cache refreshing code:

function someFunctionName($time, $slider) {    
    if ($slider->sliderId == 183) {
        $time = strtotime("+1 week");    
    }    
    return $time; 
}
N2Pluggable::addAction('SSNextCacheRefresh', 'someFunctionName');

The someFunctionName can be anything, and you should to check your slider's ID. You can find the ID above to the name of your slider

In this example the ID is 183. This way only that slider's cache will be refreshed after the given time, and the other ones could stay as they were.

The "+1 week" means, that the new slider cache refreshing time will be 1 week from now. So if you want your slider to change it's slide at exactly every week, this won't be good, because it's 1 week from the current time, not from the previous cache refreshing time, and if your slider isn't visited for example for 1 week + 6 days, the next cache refreshing won't start at 2 weeks, but at 2 weeks + 6 days. In that case either write a more complex code, like in the example you will see below, or just use "now" instead, and your slider will refresh the cache every time you go to their pages.

Slide publishing/unpublishing

Next to the cache refreshing code, you can also change which slide do you want to be visible in your slider:

function xyzcallback($slide) {
    if ($slide->id == 379) {
        $slide->setVisibility(false);
    }
}
N2Pluggable::addAction('ssSlide', xyzcallback);

The xyzcallback can be anything, and the code works that way, that you check the id of the slide, and if it's equal to your slide's id, which in this example is 379. You can find your slide's ID in the url, when you are in our backend, at that slide's editor: http://example.com/wp-admin/admin.php?page=nextend-smart-slider-3&nextendcontroller=slides&nextendaction=edit&sliderid=183&slideid=379

Then you can either choose to hide it by setting the setVisibility to false, or to show it by setting the setVisibility to true.
To make a system out of this you will need to use php date/time functions, check the current date, give a starting point, and calculate, if the current week is odd or even, or check what the mod operand gives back. The example below could help with this.

Example

First what you should do is to check, if our file has already loaded, just in case, if you would put it into the code earlier. To do this, check if the N2Pluggable class exists, and if not, then call in the file. The path I used is the Joomlapath, if you are editing the index.php file from your template's folder:
templates/[yourtemplate]/index.php

if(!class_exists('N2Pluggable')){
    require_once( realpath( dirname( __FILE__ ) ).'/../../libraries/nextend2/nextend/library/libraries/plugin/plugin.php' );
}

as you see the two /.. is going higher to the root folder, and then the path from there is written, so in WordPress, where you could edit the functions.php file of your theme:
wp-content/themes/[yourtheme]/functions.php 

you should write this code instead:

if(!class_exists('N2Pluggable')){
    require_once( realpath( dirname( __FILE__ ) ).'/../../plugins/nextend-smart-slider3-pro/nextend/library/libraries/plugin/plugin.php' );
}

After this the cache refreshing can come. In this example I will have 4 slides, and I want to show only one of them in every week. As you can read the problem at it's documentation, this number can't be 1 week for me, but I don't need it to be too accurate, that is why it's enough to check it once in every day.

function someFunctionName($time, $slider) {
    if ($slider->sliderId == 183) {
        $time = strtotime("+1 day");
    }
    return $time;
}
N2Pluggable::addAction('SSNextCacheRefresh', 'someFunctionName');

The next function called " weeks" is how I used the date/time functions to find out which slide should be published. The $your_date variable contains the starting point from where you want the calculation. The $number_of_weeks is the number of weeks, how often the slides will repeat themselves, in this case I have 4 slides, which will show 1 slide in 1 week, so they will rotate in every 4 weeks.

function weeks(){
    $now = time();
    $your_date = strtotime("2015-10-08");
    $number_of_weeks = 4;
    $datediff = $now - $your_date;
    $week_count = floor($datediff/(60*60*24*7));
    $week = (($week_count % $number_of_weeks) + 1);
    return $week;
}

And lastly the publishing code comes. You should call the function calculating which week is it, and put it into a variable. Then with a switch/case statement separate them, that if the 1st week comes and if the slide's id is your first slide's id, then that should be published, and the other ones shouldn't. After this if the 2nd, 3rd and 4th week comes, one of the other slides should be published, and the remaining ones shouldn't.

function xyzcallback($slide) {
    $weeks = weeks();
    switch($weeks){
        case 1:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(true);
                    break;
                case 380:
                    $slide->setVisibility(false);
                    break;
                case 381:
                    $slide->setVisibility(false);
                    break;
                case 382:
                    $slide->setVisibility(false);
                    break;
            }
            break;
        case 2:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(false);
                    break;
                case 380:
                    $slide->setVisibility(true);
                    break;
                case 381:
                    $slide->setVisibility(false);
                    break;
                case 382:
                    $slide->setVisibility(false);
                    break;
                }
                break;
        case 3:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(false);
                    break;
                case 380:
                    $slide->setVisibility(false);
                    break;
                case 381:
                    $slide->setVisibility(true);
                    break;
                case 382:
                    $slide->setVisibility(false);
                    break;
            }
            break;
        case 4:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(false);
                    break;
                case 380:
                        $slide->setVisibility(false);
                    break;
                case 381:
                    $slide->setVisibility(false);
                    break;
                case 382:
                    $slide->setVisibility(true);
                    break;
            }
    }
}
N2Pluggable::addAction('ssSlide', 'xyzcallback');

The whole code together:

if(!class_exists('N2Pluggable')){
    require_once( realpath( dirname( __FILE__ ) ).'/../../libraries/nextend2/nextend/library/libraries/plugin/plugin.php' );
}

function someFunctionName($time, $slider) {
    if ($slider->sliderId == 183) {
        $time = strtotime("+1 day");
    }
    return $time;
}
N2Pluggable::addAction('SSNextCacheRefresh', 'someFunctionName');

function weeks(){
    $now = time();
    $your_date = strtotime("2015-10-08");
    $number_of_weeks = 4;
    $datediff = $now - $your_date;
    $week_count = floor($datediff/(60*60*24*7));
    $week = (($week_count % $number_of_weeks) + 1);
    return $week;
}

function xyzcallback($slide) {
    $weeks = weeks();
    switch($weeks){
        case 1:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(true);
                    break;
                case 380:
                    $slide->setVisibility(false);
                    break;
                case 381:
                    $slide->setVisibility(false);
                    break;
                case 382:
                    $slide->setVisibility(false);
                    break;
            }
            break;
        case 2:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(false);
                    break;
                case 380:
                    $slide->setVisibility(true);
                    break;
                case 381:
                    $slide->setVisibility(false);
                    break;
                case 382:
                    $slide->setVisibility(false);
                    break;
                }
                break;
        case 3:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(false);
                    break;
                case 380:
                    $slide->setVisibility(false);
                    break;
                case 381:
                    $slide->setVisibility(true);
                    break;
                case 382:
                    $slide->setVisibility(false);
                    break;
            }
            break;
        case 4:
            switch($slide->id){
                case 379:
                    $slide->setVisibility(false);
                    break;
                case 380:
                        $slide->setVisibility(false);
                    break;
                case 381:
                    $slide->setVisibility(false);
                    break;
                case 382:
                    $slide->setVisibility(true);
                    break;
            }
    }
}
N2Pluggable::addAction('ssSlide', 'xyzcallback');