Jump to content

Newbie needs help


MMOearn

Recommended Posts

So I am literally a newborn here when it comes to php. I have been working on editing someone elses open code from github as practice, but can't seem to get what

I'd like done. The code takes information from a plugin from WordPress, and spits it out in raw form for people to see publicly. What I am trying to figure out, is how

to take the same information, and wrap it inside an index table so it doesn't look like just plain text. Was hoping someone could help me out and explain how

they done it so I can practice myself. The code is below.

/**
 * Custom myCRED Widget: This weeks leaderboard
 * This widget will show this weeks leaderbaord with the option to set
 * a title, the number of users to include and if it should be visible for
 * non-members.
 * @install Paste into your theme or child-themes functions.php file or custom plguin.
 * @author Gabriel S Merovingi
 * @version 1.3
 */
add_action( 'mycred_widgets_init', 'mycred_load_this_weeks_leaderboard_widget' );
function mycred_load_this_weeks_leaderboard_widget() {

	if ( ! class_exists( 'myCRED_Widget_This_Weeks_Leaderboard' ) ) {
		class myCRED_Widget_This_Weeks_Leaderboard extends WP_Widget {

			// Constructor
			public function __construct() {

				parent::__construct(
					'mycred_widget_this_weeks_leaderboard',
					sprintf( __( '(%s) This weeks Leaderboard', 'mycred' ), mycred_label() ),
					array(
						'classname'   => 'widget-mycred-this-weeks-leaderboard',
						'description' => __( 'Show the leaderboard for a given timeframe.', 'mycred' )
					)
				);

			}

			// Widget Output (what users see)
			public function widget( $args, $instance ) {

				extract( $args, EXTR_SKIP );

				// Check if we want to show this to visitors
				if ( ! $instance['show_visitors'] && ! is_user_logged_in() ) return;
			
				// Get the leaderboard
				$leaderboard = $this->get_leaderboard( $instance['number'], $widget_id );
			
				// Load myCRED
				$mycred = mycred();

				// Start constructing Widget
				echo $before_widget;
			
				// Title (if not empty)
				if ( ! empty( $instance['title'] ) ) {
					echo $before_title;
					// Allow general tempalte tags in the title
					echo $mycred->template_tags_general( $instance['title'] );
					echo $after_title;
				}

				// Construct unorganized list for each row
				echo '<ul class="mycred-this-weeks-leaderboard">';
				foreach ( $leaderboard as $position => $data ) {
					$avatar = get_avatar( $data->user_id, 32 );
					echo '<li>' . $avatar . $data->display_name . ' with ' . $mycred->format_creds( $data->total ) . '</li>';
				}
				echo '</ul>';

				echo $after_widget;

			}

			// Widget Settings (when editing / setting up widget)
		 	public function form( $instance ) {

				$title         = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'This Months Leaderboard', 'mycred' );
				$number        = isset( $instance['number'] ) ? abs( $instance['number'] ) : 5;
				$show_visitors = isset( $instance['show_visitors'] ) ? 1 : 0;

?>
<p class="myCRED-widget-field">
	<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mycred' ); ?>:</label>
	<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p class="myCRED-widget-field">
	<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of users', 'mycred' ); ?>:</label>
	<input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo $number; ?>" size="3" class="align-right" />
</p>
<p class="myCRED-widget-field">
	<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_visitors' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>" value="1"<?php checked( $show_visitors, 1 ); ?> class="checkbox" /> 
	<label for="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>"><?php _e( 'Visible to non-members', 'mycred' ); ?></label>
</p>
<?php

			}

			// Save Widget Settings
			public function update( $new_instance, $old_instance ) {

				$instance                  = $old_instance;
				$instance['number']        = absint( $new_instance['number'] );
				$instance['title']         = sanitize_text_field( $new_instance['title'] );
				$instance['show_visitors'] = ( isset( $new_instance['show_visitors'] ) ) ? $new_instance['show_visitors'] : 0;

				return $instance;

			}

			// Grabs the leaderboard
			public function get_leaderboard( $number = 5, $widget_id = '' ) {

				// Get transient
				$leaderboard = get_transient( 'mycred_twl_' . $widget_id );
				$now         = current_time( 'timestamp' );
				$this_week   = date( 'W', $now );

				// If a transient is set, check if it is time for an update
				if ( $leaderboard !== false ) {
					reset( $leaderboard );
					$saved_week = key( $leaderboard );
					// Compare the first array key (which holds the week number) to this weeks
					// If they do not match, it is a new week and we need to make a new query.
					// Same goes for empty results (new sites)
					if ( $this_week != $saved_week || empty( $leaderboard[ $saved_week ] ) )
						$leaderboard = false;
					// Else return the results
					else
						$leaderboard = $leaderboard[ $this_week ];
				}

				// If transient does not exist or a new number is set, do a new DB Query	
				if ( $leaderboard === false || $number != 5 ) {

					$leaderboard = array();

					// Load the wpdb class
					global $wpdb;
				
					$mycred         = mycred();
					$start_of_week  = strtotime( "-1 week" );

					$leaderboard[ $this_week ] = $wpdb->get_results( $wpdb->prepare( "
						SELECT m.user_id, u.display_name, sum( m.creds ) AS total 
						FROM {$mycred->log_table} m 
						LEFT JOIN {$wpdb->users} u 
							ON u.ID = m.user_id 
						WHERE ( m.time >= %d AND m.time <= %d AND m.creds > 0 ) 
						GROUP BY m.user_id 
						ORDER BY total DESC LIMIT 0,%d;", $start_of_week, $now, $number ) );

					// Save results for a week
					set_transient( 'mycred_twl_' . $widget_id, $leaderboard, WEEK_IN_SECONDS );
					$leaderboard = $leaderboard[ $this_week ];

				}
			
				return $leaderboard;

			}

		}

	}

	register_widget( 'myCRED_Widget_This_Weeks_Leaderboard' );

}
Link to comment
Share on other sites

How much of the code do you truly understand? Do you understand the individual statements and what each of them contributes? Have you done any reading on how to write PHP and what the basics of assignments, concatenation, quoting (BIG topic!) and such are? It would behoove you to do so before diving into the effort you are describing above. That is - if you truly want to learn PHP.

Edited by ginerjm
Link to comment
Share on other sites

Also, it looks like the widget already outputs an unordered list with the class 'mycred-this-weeks-leaderboard' - you can style that. It's safer than trying to modify a plugin file, because any time the author of the plugin releases an update, your changes will be overwritten.

Link to comment
Share on other sites

How much of the code do you truly understand? Do you understand the individual statements and what each of them contributes? Have you done any reading on how to write PHP and what the basics of assignments, concatenation, quoting (BIG topic!) and such are? It would behoove you to do so before diving into the effort you are describing above. That is - if you truly want to learn PHP.

 

Like I said in the post, I am literally just starting to learn. I've tried watching videos, reading books but they don't help. I've always been a on-hand learner and learn better if I just mess with it myself.

 

Also, it looks like the widget already outputs an unordered list with the class 'mycred-this-weeks-leaderboard' - you can style that. It's safer than trying to modify a plugin file, because any time the author of the plugin releases an update, your changes will be overwritten.

 

The author doesn't actually update this, its just a custom code that can be inserted into the functions.php file, if there WAS an update to the code, I would have to manually change the code myself. All it does is add a custom widget to the WordPress dashboard that you can add to a sidebar to output the information. Problem is I can only get it to spit out raw text. I would love to get it to show in a table instead, but everything I try gives me syntax errors. Was hoping someone could point me in the right direction.

Link to comment
Share on other sites

So many people say the same thing that they can't learn from reading. They refuse to understand that the whole business of IT is based upon highly technical principles that need to be explained by manuals. Without manuals nobody would ever program. You just keep plugging away and asking for help and having people point you to references that will involve reading and maybe, just maybe, you will realize that this business doesn't come easy. As I've said before - it ain't rocket science, but it is a science.

 

That said - good luck with your learning process newbie.

Link to comment
Share on other sites

Do me a favor and explain what you mean by 'raw text'.

 

The widget() function is what renders in the widget area in WordPress. The following part of the code outputs an unordered list of the data.

// Construct unorganized list for each row
echo '<ul class="mycred-this-weeks-leaderboard">';
foreach ( $leaderboard as $position => $data ) {
	$avatar = get_avatar( $data->user_id, 32 );
	echo '<li>' . $avatar . $data->display_name . ' with ' . $mycred->format_creds( $data->total ) . '</li>';
}
echo '</ul>';

If it's not spitting out a list as described, it's possible there's escaping on the content. Look for a line in your functions.php file that starts with the following:

add_filter('the_content', ...

After the comma is a function name. Find that function in the functions.php file and see what it does. An escape function in WordPress (like esc_attr(), for instance) will convert special characters into HTML entities, so your output would actually be the HTML markup itself. Now, understand that escaping is important and very much needs to be there, but there are several different escape functions in WordPress, and some are more appropriate than others for some situations.

Link to comment
Share on other sites

So many people say the same thing that they can't learn from reading. They refuse to understand that the whole business of IT is based upon highly technical principles that need to be explained by manuals. Without manuals nobody would ever program. You just keep plugging away and asking for help and having people point you to references that will involve reading and maybe, just maybe, you will realize that this business doesn't come easy. As I've said before - it ain't rocket science, but it is a science.

 

That said - good luck with your learning process newbie.

 

It's pretty obvious that you dislike people who don't like learning the way you may find the "proper" way of learning. That's fine. I never expected, nor insinuated that it was an easy business. What people like you refuse to understand is that everyone learns differently. I've done research, enough to know that modern day coders don't buy/read "manuals" anymore, they just roll up their sleeves and start writing. The market for said manuals has been dropping since 2008 because of this, and many blogs have talked about it. That's why I thought I had a snowballs chance in hell at teaching myself by just playing with code. Please don't try to insult me or anyone for that matter just because they learn differently than you. You might know what you're doing, but you didn't invent PHP or deputize the way its learned. Thanks.

 

Do me a favor and explain what you mean by 'raw text'.

 

The widget() function is what renders in the widget area in WordPress. The following part of the code outputs an unordered list of the data.

// Construct unorganized list for each row
echo '<ul class="mycred-this-weeks-leaderboard">';
foreach ( $leaderboard as $position => $data ) {
	$avatar = get_avatar( $data->user_id, 32 );
	echo '<li>' . $avatar . $data->display_name . ' with ' . $mycred->format_creds( $data->total ) . '</li>';
}
echo '</ul>';

If it's not spitting out a list as described, it's possible there's escaping on the content. Look for a line in your functions.php file that starts with the following:

add_filter('the_content', ...

After the comma is a function name. Find that function in the functions.php file and see what it does. An escape function in WordPress (like esc_attr(), for instance) will convert special characters into HTML entities, so your output would actually be the HTML markup itself. Now, understand that escaping is important and very much needs to be there, but there are several different escape functions in WordPress, and some are more appropriate than others for some situations.

 

When I say "raw text" I mean the information from that echo actually shows up, and in the order it says. But its just plain as in:

Avatar Name Credits

Avatar Name Credits

 

What I'd like is to toss in some html tables so they are wrapped inside something instead of just showing up as a list of text, if that makes sense.

I appreciate your ACTUAL help on this, but I won't be returning to this topic for further discussion, or this forum. I'm going to try to find

another community that would rather take the time to help me learn instead of complain about the way I learn.

 

Thanks again Maxxd!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.