Created
October 5, 2023 13:41
-
-
Save jonathanbossenger/3322c247bdd2b2143bb7755325b0bd14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Learn Compatibility | |
* Description: Learn to test a plugin for PHP Version Compatibility | |
* Version: 1.0.1 | |
*/ | |
/** | |
* Posts fetcher class | |
*/ | |
class post_fetcher { | |
protected $posts; | |
public function post_fetcher() { | |
$this->posts = get_posts(); | |
} | |
public function fetch_posts() { | |
$post_html = '<div class="post">'; | |
foreach ( $this->posts as $post ) { | |
if ( property_exists( $post, 'post_title' ) ) { | |
$post_html .= sprintf( | |
'<h4><a href="%s">%s</a></h4>', | |
get_permalink( $post->ID ), | |
$post->post_title | |
); | |
} | |
} | |
$post_html .= '</div>'; | |
return $post_html; | |
} | |
} | |
/** | |
* Shortcode to render posts | |
* Uses the post_fetcher class | |
*/ | |
add_shortcode( 'wp_learn_php_compatibility', 'wp_learn_php_compatibility_shortcode_render' ); | |
function wp_learn_php_compatibility_shortcode_render() { | |
$post_fetcher = new post_fetcher(); | |
$post_html = $post_fetcher->fetch_posts(); | |
return $post_html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment