I'm using a very simple plugin called Semi Private Comments which does almost everything I need, it hides comments from other users and allows only the author of the comment and the admin to view the comment. My problem is the plugin allows any admin comment to be viewed by anyone. I would like it to keep the comment between the admin and any user a one on one conversation.
I really don't know PHP well enough to modify the plugins logic and was hopping for some help.
Here's the code.
if (current_user_can('edit_users') || // user is admin, or
$user_matched==1 || // user is original author, or
$comment->user_id == 1) // comment author is admin
{
return $content;
}
else
{
$hidden_comment_text = get_option('spc_hidden_comment_text');
return $hidden_comment_text;
}
}
else
{
return $content;
}
I think just removing the $comment->user_id == 1 should do the trick
if (current_user_can('edit_users') || // user is admin, or
$user_matched==1) // user is original author
{
return $content; // Only admins and authors of the comment can read
}
else
{
$hidden_comment_text = get_option('spc_hidden_comment_text');
return $hidden_comment_text;
}
Btw, the code snipped you posted is incomplete the if statement of the following part is missing
}
else
{
return $content;
}
Related
This functionality is used to delete an image. The first if redirects back if the user who is trying to delete the image is not the author of the image or an admin, the else-if checks if the user has admin role and if he does, the image gets deleted. And finally if the user is not an admin and is the author of the image, the last bit of code deletes the image as well.
Now my question is, do I even need the else if part of the code? Can I just have 1 small if statement that redirects if the user is neither an admin nor the author and then delete the image since if the user passes the first if, he's definitely either admin or an author.
public function deleteImage($imageId){
$image = Image::where('id', $imageId)->first();
if (!Auth::user()->hasRole('Admin')) {
if (Auth::user() != $image->user){
return redirect()->back();
}
} else if (Auth::user()->hasRole('Admin')) {
$imageName = $image->image_file_name;
$image->tags()->detach();
$image->delete();
Storage::delete('public/uploads/images/'.$imageName);
Storage::delete('public/uploads/images/thumbnails/'.$imageName);
Storage::delete('public/uploads/images/specificImages/'.$imageName);
Storage::delete('public/uploads/images/miniImages/'.$imageName);
$imageChildren = Image::where('parent_id', $image->id)->get();
foreach ($imageChildren as $imageChild) {
Storage::delete('public/uploads/images/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/thumbnails/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/specificImages/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/miniImages/'.$imageChild->image_file_name);
$imageChild->delete();
}
return redirect()->route('home');
}
$imageName = $image->image_file_name;
$image->tags()->detach();
$image->delete();
Storage::delete('public/uploads/images/'.$imageName);
Storage::delete('public/uploads/images/thumbnails/'.$imageName);
Storage::delete('public/uploads/images/specificImages/'.$imageName);
Storage::delete('public/uploads/images/miniImages/'.$imageName);
$imageChildren = Image::where('parent_id', $image->id)->get();
foreach ($imageChildren as $imageChild) {
Storage::delete('public/uploads/images/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/thumbnails/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/specificImages/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/miniImages/'.$imageChild->image_file_name);
$imageChild->delete();
}
return redirect()->route('home');
}
The below should do the same thing. You did not need the else if since it will always performs the same execution once the first condition is met. I also concatenated the first condition to include the check for author vs image user
public function deleteImage($imageId){
$image = Image::where('id', $imageId)->first();
if (!Auth::user()->hasRole('Admin') && Auth::user() != $image->user) {
return redirect()->back();
}
$imageName = $image->image_file_name;
$image->tags()->detach();
$image->delete();
Storage::delete('public/uploads/images/'.$imageName);
Storage::delete('public/uploads/images/thumbnails/'.$imageName);
Storage::delete('public/uploads/images/specificImages/'.$imageName);
Storage::delete('public/uploads/images/miniImages/'.$imageName);
$imageChildren = Image::where('parent_id', $image->id)->get();
foreach ($imageChildren as $imageChild) {
Storage::delete('public/uploads/images/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/thumbnails/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/specificImages/'.$imageChild->image_file_name);
Storage::delete('public/uploads/images/miniImages/'.$imageChild->image_file_name);
$imageChild->delete();
}
return redirect()->route('home');
}
I have a site that has a menu for non members and one for members which is shown depending on if the user is logged in or logged out but would like to know how to have an additional menu that is only show to a specific user upon their login by using the user email address to determine that user is shown the third menu.
Thanks in advance
Try this out!
<?php
if (isset($_SESSION['Logged']) && $email == "email#example.com") {
//show the special menu example: admin menu
} else if (isset($_SESSION['Logged'])) {
//show the members menu here
} else {
//show the normal menu code
}
?>
Happy to help!
try this !!!
if ( user logged in )
{
getloggedmenu();
}
else
{
getUnloggedmenu()
}
public function getloogedmenu()
{
return set you menu here;
}
public function getUnloogedmenu()
{ return set you menu here;
}
So I am experimenting and creating my first Wordpress site, and this is my first stack overflow question :)
My end goal is to hide the Personal Options for subscribers/contributors in their profile page. It's just clutter that gets in the way.
I found this code written by someone who had a similar problem, and slightly edited it to make it work for me:
function remove_opt_start($adddiv) {
$gettitle = array('#<h2>Personal Options</h2>#');
$adddiv = preg_replace($gettitle, '<div class="hidden">', $adddiv,1);
return $adddiv;
}
function start_remove_opt_start() { ob_start("remove_opt_start"); }
function end_remove_opt_start() { ob_end_flush(); }
function remove_opt_end($addend) {
$getname = array('#<h2>Name</h2>#');
$addend = preg_replace($getname, '</div><h2>Name</h2>',$addend,1);
return $addend;
}
function start_remove_opt_end() { ob_start("remove_opt_end"); }
function end_remove_opt_end() { ob_end_flush(); }
add_action('admin_head','start_remove_opt_start');
add_action('admin_head','start_remove_opt_end');
add_action('admin_footer','end_remove_opt_start');
add_action('admin_footer','end_remove_opt_end');
?>
And it's great! But it also removes the options for admins, editors, and authors, which I do not want. So I found this code where someone was attempting to distinguish between users:
global $current_user;
get_currentuserinfo();
switch (true) {
case ( user_can( $current_user, "subscriber") ):
// Show Role
// Show Subscriber Image
break;
case ( user_can( $current_user, "contributor") ):
// Show Role
// Show Contributor Image
break;
case ( user_can( $current_user, "administrator") ):
// Show Role
// Show Administrator Image
break;
}
So basically, my question is thus:
How do I have combine the two codes so that the first code block is only called when the second code block detects that a user is a contributor or subscriber.
I was hoping it could be as simple as coping and pasting it inside the commented area, but it never is quite that easy. Unfortunately, I have very little knowledge of php, but I'm excited to learn :)
I have put a "Global:link" in the header of a view in Drupal 7. I suppose I could put the link as html in a Global:text area as well.
If the user is not admin I don't want them to see this link. So I have tried to put this code in my themes template.php:
// hide global text area in view header if user is not admin
function mytheme_views_pre_render(&$view) {
if ($view->name == 'taxonomy_term') {
dpm($view->name);
global $user;
// Check to see if $user has the administrator role or not.
if (!in_array('administrator', array_values($user->roles))) {
$header_item = $view->display_handler->get_option('header');
dpm($header_item['link']);
unset($header_item['link']);
}
}
}
}
.. but how do I unset a global field in the header of this specific view?
My code above does not do the trick.
Any help would be much appreciated!
SOLVED. I finally solved it. Here's the snippet. Hope it helps somebody having the same issue. Change "link" to whatever item you use in the header:
function mytheme_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'taxonomy_term') {
global $user;
$new_item = $view->get_item('page', 'header', 'link');
$new_item['text'] = "";
// Check to see if $user has the administrator role or not.
if (!in_array('administrator', array_values($user->roles))) {
$view->set_item('page', 'header', 'link', $new_item);
}
}
I have a problem with updating database table in cakephp...
So I have a profile page where the logged in user can see and update his personal information. Here is my solution for it (yes I know it's not the best...)
if($this->request->is('post')) {
$data = $this->request->data['user-profile'];
// $uid = $this->Session->read("WebUser.id");
$uid = 1;
$user_data = $this->WebUser->find('first', array('conditions'=>array('id'=>$uid)));
$updated_fields = '';
foreach($data as $key => $value) {
if($key != 'state'){
if(empty($value)) {
$this->Session->setFlash("Please fill in all the required fields");
return;
}
}
if($user_data['WebUser'][$key] != $value) {
if($key == 'email') {
$mail_check = $this->WebUser->find('first', array('conditions'=>array('email'=>$value, 'id !='=>$uid)));
if($mail_check) {
$this->Session->setFlash("This e-mail is already registered");
return;
}
}
$updated_fields .= "'".$key."'=>'".$value."',";
}
}
if($updated_fields != '') {
$updated_fields .= "'modified'=>'".date("Y-m-d H:i:s")."'";
$this->WebUser->read(null, $uid);
$this->WebUser->set(array('first_name'=>'John','modified'=>'2014-12-30 10:53:00'));
if($this->WebUser->save()) {
$this->printr($this->data);
$this->WebUser->save();
$this->Session->setFlash("Success : Profile data is now modified", array("class"=>"success_msg"));
} else {
$this->Session->setFlash("Error : Data modifying isn't complete. Please try again!");
}
}
return;
}
So this code fetches the user info from the database and looks for those fields which are edited on profile page. Problem is when I want to save the data it give me back false and didn't save it... Does someone have a solution for this?
Try putting
$this->WebUser->validationErrors;
in else part and check whether there are any validation errors or not.
Like this:
if($this->WebUser->save()) {
$this->printr($this->data);
$this->WebUser->save();
$this->Session->setFlash("Success : Profile data is now modified", array("class"=>"success_msg"));
} else {
echo "<pre>";print_r($this->WebUser->validationErrors);die();
$this->Session->setFlash("Error : Data modifying isn't complete. Please try agin!");
}
Did you tried to set id directly?
$this->WebUser->id = $uid;
Also, CakePHP is automatically update modified field in your table after any transactions. So you don't have to set it directly (but you can if you want any other date value).
About validation. I recommend you to read about Data Validation in CakePHP, because that validation is not good, it's can be handled by models.
Also. You can get any validation errors from models using Model::$validationErrors.
debug($this->Recipe->validationErrors);
The problem with this code was that didn't wanted to save the posted data ( debug($this->WebUser->save) has give me back false). At the end I didn't found the solution why this code not worked but the conclusion is that you never need to use more users table than one. So now I have a Users and Groups tables, and different type of users are connected to their groups(Admin, User, ...).