Streamline these if statements and output them with their values - php

Is there a way that I can streamline my process when I'm working with theme options on Wordpress?
Currently I use a setup like this when I'm creating a social links menu for example (With the option value being the link URL);
$twitter = of_get_option('twitter');
$facebook = of_get_option('facebook');
$google-plus = of_get_option('google-plus');
if ($twitter){
echo '<li class="twitter"><i class="fa fa-twitter"></i></li>';
}
if ($facebook){
echo '<li class="facebook"><i class="fa fa-facebook"></i></li>';
}
if ($google-plus){
echo '<li class="google-plus"><i class="fa fa-google-plus"></i></li>';
}
I'm sure there must be an easier, more streamlined, way to go about doing this?
Any help would be greatly appreciated.

I'd wrapp it in a function.
function getOption($op) {
$link = of_get_option($op);
if($link)
echo '<li class="'.$op.'"><i class="fa fa-'.$op.'"></i></li>';
}
getOption('twitter');
getOption('facebook');
getOption('google-plus');

Related

Redirect to controller - Codeigniter

I have many views in a folder in views folder, for example in views/admin folder.
When I code
if (!empty($users->users)):
foreach ($users->users as $user) {
$link = site_url('ContentController/showActivities/' . $user->id);
$what = $user->banned == 0 ? 'b':'u';
$banned_t = $user->banned == 0 ? 'Banned':'Un-banned';
echo '<tr id="user_'.$user->id.'">';
echo '<td><div class="userimg"><img src="' . $user->profile_pic->medium . '"></div></td>';
echo '<td>'.$user->name . '</td>';
echo '<td>';
echo $user->last_login == '' ? 'never' : $user->last_login;
echo '</td>';
echo $user->banned == 0 ? '<td><span class="label label-success">Active</span></td>' : '<td><span class="label label-danger">Banned</span></td>';
echo '<td>'.$user->email.'</td>';
echo '<td><div class="btn-group">
<button type="button" onclick="Buckty.user.edit($(this));" data-toggle="modal" data-target="#editUser" data-id="'.$user->id.'" class="btn btn-danger">Edit</button>
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a onclick="javascript:ref('.$user->id.')">Show Activities</a></li>
<li><a onclick="Buckty.user.ban($(this));" data-id="'.$user->user_hash.'" data-what="'.$what.'">'.$banned_t.'</a></li>
<li><a onclick="Buckty.user.remove($(this));" data-id="'.$user->id.'">Delete</a></li>
</ul>
</div></td>';
echo '</tr>';
}
endif;
And this is the code that i want to redirect to controller
<li><a onclick="javascript:ref('.$user->id.')">Show Activities</a></li>
Javascript:
function ref(id) {
window.open('ContentController/showActivities/'+id, '_blank');
}
But, when i click "Show Activities" menu, it always redirect to http://localhost/file-sharing/admin/ContentController/showActivities/1 and it doesn't redirect to ContentController/showActivities controller. It should redirect to that controller if the url is http://localhost/file-sharing/ContentController/showActivities/1
Anybody know what should I do in this case? Thanks in advance.
Being Owner of this platform. Let me clear you few things. Site url is not being defined by base_url() or site_url() for functions and compatibility reasons.
First of all , i would not recommend you to not use controller names , try to organise and make routes for your controllers inside routes.php but now let's talk about your problem.
site_url() is your current url you are on. But you have very simple access to global settings for everything.
Introduction to global variables
$user || this contains currently logged in user's data , you can use it anywhere inside views
$this->current_user || this contains similar data to above variable , but this should be used for controllers.
$site || this contains site settings , every site settings data you have saved in database (from site_url to ads settings )
$site->site_url || this is how you use your site url inside this platform
$this->site || this should be used inside controllers to fetch site settings
so according to your problem , you should be using $site->site_url and not site_url();
basically change this :
$link = site_url('ContentController/showActivities/' . $user->id);
to this
$link = $site->site_url.'ContentController/showActivities/' . $user->id;
or if we talk about javascript then global variable for javascript is also there. you should be using site_url as site link so edit the below :
function ref(id) {
window.open('ContentController/showActivities/'+id, '_blank');
}
to this
function ref(id) {
window.location.replace(site_url + 'ContentController/showActivities/'+id, '_blank');
}
here is introduction to global variables for javascript :
site_url || this contains your site url (from site settings )
user_data || this contains current user's data (everything only password is not there )
site_info || this contains site settings data.
tran || this contains multi language translations
current_folderĀ || this contains current folder's hash id , on which you are currently
current_folder is 0 if it's on root folder , else it if folderĀ if it's on un-functional view.
Try this :
function ref(id) {
window.open('<?php echo base_url()?>ContentController/showActivities/'+id, '_blank');
}

Instagram URL --> JSON

How can I go about convering a public instagram URL into JSON using PHP? Ex: https://www.instagram.com/explore/tags/brindle/
I can't use the API as I need public hashtag content and my use case won't qualify for their app review process :-(.
Here is what I have so far but it does not pull all images. Also, I'd like to be able to load the "load more" images as well. Any help would be much appreciated!
$instagram_source = file_get_contents("https://www.instagram.com/explore/tags/brindle/");
$instagram_data = explode("window._sharedData = ", $instagram_source);
$instagram_json = explode(';</script>', $instagram_data[1]);
$instagram_array = json_decode($instagram_json[0], TRUE);
$instagram_media = $instagram_array['entry_data']['TagPage'][0]['tag']['media']['nodes'];
if(!empty($instagram_media)) {
echo '<ul>';
foreach($instagram_media as $im) {
echo '<li>';
echo '<a href="https://www.instagram.com/p/'.$im['code'].'/" target="_blank">';
echo '<img src="'.$im["display_src"].'" alt="" width="'.$im["dimensions"]["width"].'" height="'.$im["dimensions"]["height"].'" />';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
Take a look at this solution here: https://github.com/Bolandish/Instagram-Grabber
Thats the best one i know until now.

Wordpress Codeless Slider Responsive Issue

If you visit my website so far, http://trulydesigns.com/ you will notice that everything looks in order.
If you visit the web page via mobile such as an iPhone 5s the slider looks out of place. http://quirktools.com/screenfly/#u=http%3A//trulydesigns.com&w=320&h=568&a=37&s=1
function createSlider(){
global $cl_redata;
if(isset($cl_redata['codeless_slider_height']) && $cl_redata['codeless_slider_height'] != '100%')
$height = $cl_redata['codeless_slider_height'];
elseif(! isset($cl_redata['codeless_slider_height']))
$height = '450';
else
$height = 'fullscreen';
$this->height = $height;
$extra_class = '';
if($cl_redata['slider_parallax'])
$extra_class .= ' parallax_slider';
$output = '<div class="codeless_slider_swiper '.esc_attr($extra_class).'" style="'.(($height == 'fullscreen')?'':'height:'.$height.'px').'">';
$output .= '<div class="loading"><i class="moon-spinner icon-spin"></i></div>';
$output .= '<div class="codeless_slider_wrapper" data-start="transform: translateY(0px);" data-'.(($height == 'fullscreen')?'1440':$height).'="transform: translateY(-500px);">';
$output .= '<div class="codeless-slider-container swiper-parent swiper_slider codeless_slider" data-slidenumber="1" data-height="'.esc_attr($height).'">';
$output .= '<div class="pagination-parent nav-thumbflip nav-slider">
<a class="prev" href="">
<span class="icon-wrap"><i class="icon-angle-left"></i></span>
<div class="text">'.__('PREV','codeless').'</div>
</a>
<a class="next" href="">
<span class="icon-wrap"><i class="icon-angle-right"></i></span>
<div class="text">'.__('NEXT','codeless').'</div>
</a>
</div>';
$output .= '<div class="swiper-wrapper">';
$this->output[] = $output;
}
This is a snippet of the "codeless_slider.php", somewhere in my files it's pulling "min-height: 236.97522816166884px;height: 236.97522816166884px;" and I can't figure where from, so any help is greatly appreciated.
It can be tricky because you use a plugin to not have to build it yourself, but sometimes plugins inject code and you have to learn how it is built in order to make it work the way you want anyway.
While learning Wordpress development, I learned to be best friends with the DOM inspector in Chrome and to use the !important declaration to overwrite style rules. It looks like you could do that here with success.
Good luck!
This code is from a Premium WordPress theme, so in any case you can ask their support forum on
https://codeless.co/support/

Fail to using if/else to hover image by dynamic url

I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.

Sanitize and secure XML import

I am fairly new to XML, and have started using simplexml_load_file to import content of an XML-file.
I have the following working code, but I know that it is potentially dangerous.
I need help with securing the code, content and URL, and also the opportunity to limit characters in $doc->content
<ul id="feed">
<?php
ob_start();
$xml = simplexml_load_file('wip4/xmlfeed.epl');
foreach ($xml->document as $doc)
{
if($num++ < 10) {
echo '<li class="jobb-entry"><h4>'. $doc->title . '</h4>';
echo '<p>'. $doc->content . '</p>';
echo '<p class="apply-link clearfix"><span>Apply</span></p></li>';
}
}
ob_end_flush();
?>
</ul>
Also, if there is other methods of importing XML-documents, that are both faster and more secure, I appreciate any tips.

Categories