2626 */
2727
2828// If this file is called directly, abort.
29- if ( ! defined ( 'WPINC ' ) ) {
29+ if (! defined ('WPINC ' ) ) {
3030 die;
3131}
3232
3535 * This action is documented in includes/class-display-medium-posts-activator.php
3636 */
3737function activate_display_medium_posts () {
38- require_once plugin_dir_path ( __FILE__ ) . 'includes/class-display-medium-posts-activator.php ' ;
38+ require_once plugin_dir_path (__FILE__ ) . 'includes/class-display-medium-posts-activator.php ' ;
3939 Display_Medium_Posts_Activator::activate ();
4040}
4141
@@ -44,18 +44,18 @@ function activate_display_medium_posts() {
4444 * This action is documented in includes/class-display-medium-posts-deactivator.php
4545 */
4646function deactivate_display_medium_posts () {
47- require_once plugin_dir_path ( __FILE__ ) . 'includes/class-display-medium-posts-deactivator.php ' ;
47+ require_once plugin_dir_path (__FILE__ ) . 'includes/class-display-medium-posts-deactivator.php ' ;
4848 Display_Medium_Posts_Deactivator::deactivate ();
4949}
5050
51- register_activation_hook ( __FILE__ , 'activate_display_medium_posts ' );
52- register_deactivation_hook ( __FILE__ , 'deactivate_display_medium_posts ' );
51+ register_activation_hook (__FILE__ , 'activate_display_medium_posts ' );
52+ register_deactivation_hook (__FILE__ , 'deactivate_display_medium_posts ' );
5353
5454/**
5555 * The core plugin class that is used to define internationalization,
5656 * admin-specific hooks, and public-facing site hooks.
5757 */
58- require plugin_dir_path ( __FILE__ ) . 'includes/class-display-medium-posts.php ' ;
58+ require plugin_dir_path (__FILE__ ) . 'includes/class-display-medium-posts.php ' ;
5959
6060/**
6161 * Begins execution of the plugin.
@@ -66,146 +66,141 @@ function deactivate_display_medium_posts() {
6666 *
6767 * @since 1.0.0
6868 */
69- function run_display_medium_posts () {
69+ function run_display_medium_posts ()
70+ {
7071
7172 $ plugin = new Display_Medium_Posts ();
7273 $ plugin ->run ();
73-
7474}
7575run_display_medium_posts ();
7676
77- // Example 1 : WP Shortcode to display form on any page or post.
78- function posts_display ($ atts ){
79- ob_start ();
80- $ a = shortcode_atts (array ('handle ' => '-1 ' , 'default_image ' => '//i.imgur.com/p4juyuT.png ' , 'display ' => 3 , 'offset ' => 0 , 'total ' => 10 , 'list ' => false , 'title_tag ' => 'p ' , 'date_format ' => 'M d, Y ' ), $ atts );
81- // No ID value
82- if (strcmp ($ a ['handle ' ], '-1 ' ) == 0 ){
83- return "" ;
84- }
85- $ handle = $ a ['handle ' ];
86- $ default_image = $ a ['default_image ' ];
87- $ display = $ a ['display ' ];
88- $ offset = $ a ['offset ' ];
89- $ total = $ a ['total ' ];
90- $ list = $ a ['list ' ] == 'false ' ? false : $ a ['list ' ];
91- $ title_tag = $ a ['title_tag ' ];
92- $ date_format = $ a ['date_format ' ];
77+ // Example 1 : WP Shortcode to display form on any page or post.
78+ function posts_display ($ atts )
79+ {
80+ ob_start ();
81+ $ a = shortcode_atts (array ('handle ' => '-1 ' , 'default_image ' => '//i.imgur.com/p4juyuT.png ' , 'display ' => 3 , 'offset ' => 0 , 'total ' => 10 , 'list ' => false , 'title_tag ' => 'p ' , 'date_format ' => 'M d, Y ' ), $ atts );
82+ // No ID value
83+ if (strcmp ($ a ['handle ' ], '-1 ' ) == 0 ) {
84+ return "" ;
85+ }
86+ $ handle = $ a ['handle ' ];
87+ $ default_image = $ a ['default_image ' ];
88+ $ display = $ a ['display ' ];
89+ $ offset = $ a ['offset ' ];
90+ $ total = $ a ['total ' ];
91+ $ list = $ a ['list ' ] == 'false ' ? false : $ a ['list ' ];
92+ $ title_tag = $ a ['title_tag ' ];
93+ $ date_format = $ a ['date_format ' ];
9394
94- $ content = null ;
95+ $ content = null ;
9596
96- $ medium_url = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/ " . $ handle ;
97+ $ medium_url = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/ " . $ handle ;
9798
9899
99- try {
100- $ ch = curl_init ();
100+ try {
101+ $ ch = curl_init ();
101102
102- if (false === $ ch )
103- throw new Exception ('failed to initialize ' );
103+ if (false === $ ch )
104+ throw new Exception ('failed to initialize ' );
104105
105- curl_setopt ($ ch , CURLOPT_URL , $ medium_url );
106- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
107- curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 120 );
106+ curl_setopt ($ ch , CURLOPT_URL , $ medium_url );
107+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
108+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 120 );
108109
109- $ content = curl_exec ($ ch );
110+ $ content = curl_exec ($ ch );
110111
111- if (false === $ content )
112- throw new Exception (curl_error ($ ch ), curl_errno ($ ch ));
112+ if (false === $ content )
113+ throw new Exception (curl_error ($ ch ), curl_errno ($ ch ));
113114
114115 // ...process $content now
115- } catch (Exception $ e ) {
116+ } catch (Exception $ e ) {
117+
118+ trigger_error (
119+ sprintf (
120+ 'Curl failed with error #%d: %s ' ,
121+ $ e ->getCode (),
122+ $ e ->getMessage ()
123+ ),
124+ E_USER_ERROR
125+ );
126+ }
127+
128+ $ json = json_decode ($ content );
129+ $ items = array ();
130+ $ count = 0 ;
131+ if (isset ($ json ->items )) {
132+ $ posts = $ json ->items ;
133+ foreach ($ posts as $ post ) {
134+ $ items [$ count ]['title ' ] = $ post ->title ;
135+ $ items [$ count ]['url ' ] = $ post ->link ;
136+
137+ $ start = strpos ($ post ->description , '<p> ' );
138+ $ end = strpos ($ post ->description , '</p> ' , $ start );
139+ $ paragraph = substr ($ post ->description , $ start , $ end - $ start + 4 );
140+ $ items [$ count ]['subtitle ' ] = mb_strimwidth (html_entity_decode (strip_tags ($ paragraph )), 0 , 140 , "... " );
141+
142+ if (!empty ($ post ->thumbnail )) {
143+ $ image = $ post ->thumbnail ;
144+ } else {
145+ $ image = $ default_image ;
146+ }
147+ $ items [$ count ]['image ' ] = $ image ;
116148
117- trigger_error (
118- sprintf (
119- 'Curl failed with error #%d: %s ' ,
120- $ e ->getCode (),
121- $ e ->getMessage ()
122- ),
123- E_USER_ERROR
124- );
149+ $ items [$ count ]['date ' ] = date ($ date_format , strtotime ($ post ->pubDate ));
125150
151+ $ count ++;
126152 }
127-
128- $ json = json_decode ($ content );
129- $ items = array ();
130- $ count = 0 ;
131- if (isset ($ json ->items )) {
132- $ posts = $ json ->items ;
133- foreach ($ posts as $ post ) {
134- $ items [$ count ]['title ' ] = $ post ->title ;
135- $ items [$ count ]['url ' ] = $ post ->link ;
136-
137- $ start = strpos ($ post ->description , '<p> ' );
138- $ end = strpos ($ post ->description , '</p> ' , $ start );
139- $ paragraph = substr ($ post ->description , $ start , $ end - $ start + 4 );
140- $ items [$ count ]['subtitle ' ] = mb_strimwidth (html_entity_decode (strip_tags ($ paragraph )), 0 , 140 , "... " );
141-
142- if (!empty ($ post ->thumbnail )) {
143- $ image = $ post ->thumbnail ;
144- } else {
145- $ image = $ default_image ;
146- }
147- $ items [$ count ]['image ' ] = $ image ;
148-
149- $ items [$ count ]['date ' ] = date ("M j, Y " , strtotime ($ post ->pubDate ));
150-
151- $ count ++;
152- }
153- if ($ offset ) {
154- $ items = array_slice ($ items , $ offset );
155- }
156-
157- if (count ($ items ) > $ total ) {
158- $ items = array_slice ($ items , 0 , $ total );
159- }
153+ if ($ offset ) {
154+ $ items = array_slice ($ items , $ offset );
160155 }
161156
162- ?>
163- <div id="display-medium-owl-demo" class="display-medium-owl-carousel">
164- <?php foreach ( $ items as $ item ) { ?>
165- <div class="display-medium-item">
166- <a href=" <?php echo $ item [ ' url ' ]; ?> " target="_blank" >
167-
168- <?php
169- if ( $ list )
170- {
171- echo ' <img src=" ' . $ item [ ' image ' ]. ' " class="display-medium-img"> ' ;
172- }
173- else
174- {
175- echo ' <div data-src=" ' . $ item [ ' image ' ]. ' " class="lazyOwl medium-image"></div> ' ;
176- }
177- ?>
178- < <?php echo $ title_tag ; ?> class="display-medium-title details-title"> <?php echo $ item [ ' title ' ]; ?> </ <?php echo $ title_tag ; ?> >
179- </a >
180- <p class="display-medium-subtitle" >
181- <?php echo $ item [ ' subtitle ' ]; ? >
182- </p >
183- <p class="display-medium-date-read" >
184- <?php echo " <span class=' display-medium-date'> " . $ item [ ' date ' ]. " </span> " ; ?> / <?php echo " <span class='display-medium-readtime'> " . $ item [ ' duration ' ]. " min read</span> " ; ?> .
185- <a href=" <?php echo $ item ['url ' ]; ?> " target="_blank" class="text-right display-medium-readmore">Read More</a>
186- </p >
187- </div >
188-
189- <?php } ?>
190- </div >
191- <?php
192- if ( empty ( $ items )) echo " <div class='display-medium-no-post'>No posts found!</div> " ;
193- ?>
194- <script type="text/javascript" >
195- function initializeOwl(count) {
196- jQuery(".display-medium-owl-carousel").owlCarousel( {
197- items: count,
198- lazyLoad : true ,
199- });
200- }
201- </script>
202- <?php
203- if (! $ list )
204- {
205- echo '<script>initializeOwl( ' . $ display. ');</script> ' ;
206- }
207- ?>
208- <?php
209- return ob_get_clean ();
210- }
211- add_shortcode ('display_medium_posts ' , 'posts_display ' );
157+ if ( count ( $ items ) > $ total ) {
158+ $ items = array_slice ( $ items , 0 , $ total );
159+ }
160+ }
161+ ? >
162+ <div id="display-medium-owl-demo" class="display-medium-owl-carousel">
163+ <?php foreach ( $ items as $ item ) { ?>
164+ <div class="display-medium-item">
165+ <a href=" <?php echo $ item [ ' url ' ]; ?> " target="_blank">
166+
167+ <?php
168+ if ( $ list ) {
169+ echo ' <img src=" ' . $ item [ ' image ' ] . ' " class="display-medium-img"> ' ;
170+ } else {
171+ echo ' <div data-src=" ' . $ item [ ' image ' ] . ' " class="lazyOwl medium-image"></div> ' ;
172+ }
173+ ? >
174+ < <?php echo $ title_tag ; ?> class="display-medium-title details-title"> <?php echo $ item [ ' title ' ]; ?> </ <?php echo $ title_tag ; ?> >
175+ </a >
176+ <p class="display-medium- subtitle" >
177+ <?php echo $ item [ ' subtitle ' ]; ? >
178+ </p >
179+ <p class=" display-medium-date- read">
180+ <?php echo " <span class='display-medium-date'> " . $ item ['date ' ] . " </span> " ; ?> /
181+ <a href=" <?php echo $ item [ ' url ' ]; ?> " target="_blank" class="text-right display-medium-readmore">Read More</a >
182+ </p >
183+ </div>
184+
185+ <?php } ? >
186+ </div>
187+ <?php
188+ if ( empty ( $ items )) echo " <div class='display-medium-no-post'>No posts found!</div> " ;
189+ ? >
190+ <script type="text/javascript">
191+ function initializeOwl(count) {
192+ jQuery(".display-medium-owl-carousel").owlCarousel({
193+ items: count ,
194+ lazyLoad: true,
195+ });
196+ }
197+ </script>
198+ <?php
199+ if (! $ list ) {
200+ echo '<script>initializeOwl( ' . $ display . ');</script> ' ;
201+ }
202+ ?>
203+ <?php
204+ return ob_get_clean ();
205+ }
206+ add_shortcode ('display_medium_posts ' , 'posts_display ' );
0 commit comments