Last active
December 1, 2019 14:22
-
-
Save iqbalrony/fa5f7ed249b5aabc854b7b31e4b4cc62 to your computer and use it in GitHub Desktop.
The way to get data from rss feed url.
This file contains hidden or 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
$post_per_page = 8; | |
$url = 'https://www.feedforall.com/sample.xml'; | |
// $url = 'https://www.feedforall.com/sample-feed.xml'; | |
// $url = 'https://www.feedforall.com/blog-feed.xml'; | |
// $url = 'http://www.rss-specifications.com/blog-feed.xml'; | |
$url = ! empty( $url ) ? $url : ''; | |
while ( stristr( $url, 'http' ) != $url ) { | |
$url = substr( $url, 1 ); | |
} | |
if ( empty( $url ) ) { | |
return; | |
} | |
// self-url destruction sequence | |
if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) ) { | |
return; | |
} | |
$rss = fetch_feed( $url ); | |
if ( is_string( $rss ) ) { | |
$rss = fetch_feed( $rss ); | |
} | |
elseif ( is_array( $rss ) && isset( $rss['url'] ) ) { | |
//$args = $rss; | |
$rss = fetch_feed( $rss['url'] ); | |
} | |
elseif ( ! is_object( $rss ) ) { | |
return; | |
} | |
if ( is_wp_error( $rss ) ) { | |
if ( is_admin() || current_user_can( 'manage_options' ) ) { | |
echo '<p><strong>' . __( 'RSS Error:', 'happy-elementor-addons' ) . '</strong> ' . $rss->get_error_message() . '</p>'; | |
} | |
return; | |
} | |
if ( ! $rss->get_item_quantity() ) { | |
echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; | |
$rss->__destruct(); | |
unset( $rss ); | |
return; | |
} | |
echo '<ul>'; | |
foreach ( $rss->get_items( 0, $post_per_page ) as $item ) { | |
$link = $item->get_link(); | |
while ( stristr( $link, 'http' ) != $link ) { | |
$link = substr( $link, 1 ); | |
} | |
$link = esc_url( strip_tags( $link ) ); | |
$title = esc_html( trim( strip_tags( $item->get_title() ) ) ); | |
if ( empty( $title ) ) { | |
$title = __( 'Untitled' ); | |
} | |
$desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); | |
$desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) ); | |
$summary = $desc; | |
// Change existing [...] to […]. | |
if ( '[...]' == substr( $summary, -5 ) ) { | |
$summary = substr( $summary, 0, -5 ) . '[…]'; | |
} | |
$summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>'; | |
$date = $item->get_date( 'U' ); | |
if ( $date ) { | |
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>'; | |
} | |
$author = $item->get_author(); | |
if ( is_object( $author ) ) { | |
$author = $author->get_name(); | |
$author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>'; | |
} | |
if ( $link == '' ) { | |
echo "<li>$title{$date}{$summary}{$author}</li>"; | |
} else { | |
echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>"; | |
} | |
} | |
echo '</ul>'; | |
$rss->__destruct(); | |
unset( $rss ); | |
prefix_get_rss_feed( $url, $post_per_page = 10 ) { | |
$rss_feed = []; | |
$url = ! empty( $url ) ? $url : ''; | |
while ( stristr( $url, 'http' ) != $url ) { | |
$url = substr( $url, 1 ); | |
} | |
if ( empty( $url ) ) { | |
return; | |
} | |
// self-url destruction sequence | |
if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) ) { | |
return; | |
} | |
$rss = fetch_feed( $url ); | |
if ( is_string( $rss ) ) { | |
$rss = fetch_feed( $rss ); | |
} elseif ( is_array( $rss ) && isset( $rss['url'] ) ) { | |
$rss = fetch_feed( $rss['url'] ); | |
} elseif ( ! is_object( $rss ) ) { | |
return; | |
} | |
$error = ''; | |
if ( is_wp_error( $rss ) && ( is_admin() || current_user_can( 'manage_options' ) ) ) { | |
$error = __( 'RSS Error:', 'text-domain' ) . '</strong> ' . $rss->get_error_message(); | |
return $error; | |
} | |
if ( ! $rss->get_item_quantity() ) { | |
$error = __( 'An error has occurred, which probably means the feed is down. Try again later.', 'text-domain' ); | |
$rss->__destruct(); | |
unset( $rss ); | |
return $error; | |
} | |
$favicon = $rss->get_image_url(); | |
$post_per_page = (int) $post_per_page; | |
if ( $post_per_page == -1 ) { | |
$post_per_page = $rss->get_item_quantity(); | |
} | |
foreach ( $rss->get_items( 0, $post_per_page ) as $item ) { | |
$link = $item->get_link(); | |
while ( stristr( $link, 'http' ) != $link ) { | |
$link = substr( $link, 1 ); | |
} | |
$link = esc_url( strip_tags( $link ) ); | |
$title = esc_html( trim( strip_tags( $item->get_title() ) ) ); | |
if ( empty( $title ) ) { | |
$title = __( 'Untitled' ); | |
} | |
$rss_feed[$link] = $title; | |
} | |
$rss->__destruct(); | |
unset( $rss ); | |
return $rss_feed; | |
} | |
//add_action('elementor/element/common/_section_happy_effects/after_section_end', 'hasads_push_grid_markup', 10, 1); | |
add_action('elementor/element/section/section_advanced/after_section_end', 'hasads_push_grid_markup', 5, 1); | |
add_action('elementor/element/column/section_advanced/after_section_end', 'hasads_push_grid_markup', 5, 1); | |
add_action('elementor/element/common/_section_style/after_section_end', 'hasads_push_grid_markup', 5, 1); | |
//add_action( 'elementor/element/section/section_layout/before_section_end', 'hasads_push_grid_markup', 10, 1 ); | |
function hasads_push_grid_markup ($element) { | |
// if ($section_id == 'section_custom_css_pro') {} | |
$element->start_controls_section( | |
'_ha_scheduler', | |
[ | |
'label' => __( 'Happy Scheduler', 'pawshop_toolkit' ), | |
'tab' => Controls_Manager::TAB_ADVANCED, | |
] | |
); | |
$element->add_control( | |
'_ha_scheduler_hide_show', | |
[ | |
'label' => __( 'Scheduler', 'happy-addons-pro' ), | |
'type' => Controls_Manager::SWITCHER, | |
'label_on' => __( 'Show', 'happy-addons-pro' ), | |
'label_off' => __( 'Hide', 'happy-addons-pro' ), | |
'return_value' => 'yes', | |
'default' => 'yes', | |
] | |
); | |
$element->add_control( | |
'_ha_scheduler_action', | |
[ | |
'label' => __('Action Type', 'happy-addons-pro'), | |
'type' => Controls_Manager::SELECT, | |
'label_block' => false, | |
'options' => [ | |
'show' => __('Show', 'happy-addons-pro'), | |
'hide' => __('Hide', 'happy-addons-pro'), | |
], | |
'toggle' => false, | |
'default' => 'show', | |
'condition' => [ | |
'_ha_scheduler_hide_show' => 'yes', | |
], | |
] | |
); | |
$element->add_control( | |
'_ha_scheduler_start_time', | |
[ | |
'label' => __('Start Time', 'happy-addons-pro'), | |
'label_block' => false, | |
'type' => Controls_Manager::DATE_TIME, | |
// 'default' => date("Y-m-d"), | |
'condition' => [ | |
'_ha_scheduler_hide_show' => 'yes', | |
], | |
] | |
); | |
$element->add_control( | |
'_ha_scheduler_end_time', | |
[ | |
'label' => __('End Time', 'happy-addons-pro'), | |
'label_block' => false, | |
'type' => Controls_Manager::DATE_TIME, | |
//'default' => date("Y-m-d"), | |
// 'default' => date("Y-m-d", strtotime("+ 1 day")), | |
'condition' => [ | |
'_ha_scheduler_hide_show' => 'yes', | |
], | |
] | |
); | |
$element->end_controls_section(); | |
} | |
add_action( 'elementor/frontend/section/before_render', 'section_hide_test', 10, 1 ); | |
function section_hide_test( $element ) { | |
$settings = $element->get_settings_for_display(); | |
// $element->get_settings( 'my-custom-settings' ) | |
$editor = ha_elementor()->editor->is_edit_mode(); | |
$present_time = time(); | |
$scheduler = $settings[ '_ha_scheduler_hide_show']; | |
$action = $settings[ '_ha_scheduler_action']; | |
$start_time = strtotime( $settings[ '_ha_scheduler_start_time'] ) ; | |
$end_time = strtotime( $settings[ '_ha_scheduler_end_time' ] ); | |
var_dump($present_time,$start_time); | |
echo '<br>'; | |
var_dump($end_time); | |
echo '<br>'; | |
if( !$editor && 'yes' != $scheduler ) { | |
return; | |
} | |
if( empty($start_time) || ( !empty($end_time) && $end_time <= $start_time ) ){ | |
echo 'start'; | |
return; | |
} | |
if( 'show' === $action && $present_time >= $start_time ){ | |
echo 'show'; | |
if( !empty($end_time) && $present_time >= $end_time){ | |
ob_start(); | |
} | |
return; | |
}elseif( 'hide' === $action && $present_time >= $start_time ){ | |
echo 'hide'; | |
if( !empty($end_time) && $present_time > $end_time){ | |
return; | |
} | |
ob_start(); | |
}else{ | |
echo 'nothing'; | |
ob_start(); | |
} | |
} | |
add_action( 'elementor/frontend/section/after_render', 'section_hide_test2', 10, 1 ); | |
function section_hide_test2( $element ) { | |
$settings = $element->get_settings_for_display(); | |
$editor = ha_elementor()->editor->is_edit_mode(); | |
$present_time = time(); | |
$scheduler = $settings[ '_ha_scheduler_hide_show']; | |
$action = $settings[ '_ha_scheduler_action']; | |
$start_time = strtotime( $settings[ '_ha_scheduler_start_time'] ); | |
$end_time = strtotime( $settings[ '_ha_scheduler_end_time' ] ); | |
if( !$editor && 'yes' != $scheduler ) { | |
return; | |
} | |
if( empty($start_time) || ( !empty($end_time) && $end_time <= $start_time ) ){ | |
return; | |
} | |
if( 'show' === $action && $present_time >= $start_time ){ | |
echo 'show'; | |
if( !empty($end_time) && $present_time >= $end_time){ | |
ob_get_clean(); | |
echo 'hello world<br>'; | |
} | |
return; | |
}elseif( 'hide' === $action && $present_time >= $start_time ){ | |
echo 'hide'; | |
if( !empty($end_time) && $present_time >= $end_time){ | |
return; | |
} | |
ob_get_clean(); | |
echo 'hello world<br>'; | |
}else{ | |
ob_get_clean(); | |
echo 'hello world<br>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment