function iprm_delete_product($productToDeleteURL)
{
    $podcastArray = iprm_get_option('iprm_podcasts');
    $index = array_search($productToDeleteURL, $podcastArray);
    if ($index !== FALSE) {
        unset($podcastArray[$index]);
    }
    iprm_update_option('iprm_podcasts', $podcastArray);
    $dbSuccess = iprm_delete_option("iprm_review_cache_history{$productToDeleteURL}");
    $dbSuccess = iprm_delete_option("iprm_review_cache{$productToDeleteURL}");
}
function iprm_main_page()
{
    /* LOAD CURRENT */
    if (iprm_get_option('iprm_active_product')) {
        $podcast = new IPRM_Podcast(iprm_get_option('iprm_active_product'));
    } else {
        /* COULD NOT LOAD, USE DEFAULT */
        $podcast = new IPRM_Podcast('http://itunes.apple.com/us/');
    }
    /* DISABLES FOR NON-ADMINISTRATORS */
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    $alert = '';
    $notice = '';
    /* LOAD CURRENT SELECTED PRODUCT */
    if (isset($_POST["iprm_product_select"])) {
        if (isset($_POST["iprm_update_product"])) {
            $podcast = new IPRM_Podcast($_POST["iprm_product_select"]);
        } else {
            if (isset($_POST["iprm_delete_product"])) {
                /* DEV FAILSAFE.. NEED NEW CONSTRUCTOR THAT TAKES 0 */
                $podcast = new IPRM_Podcast('http://itunes.apple.com/us/');
                $productToDeleteURL = $_POST["iprm_product_select"];
                iprm_delete_product($productToDeleteURL);
                $notice = __('Product removed: ' . $_POST["iprm_product_select"], 'iprm_domain');
            }
        }
    } else {
        $podcast = new IPRM_Podcast(iprm_get_option('iprm_active_product'));
    }
    // IF NEW ITUNES URL IS ENTERED, UPDATE ACTIVE
    if (isset($_POST["iprm_update_url"]) && $_POST["iprm_add_url"] != '') {
        if (filter_var($_POST["iprm_add_url"], FILTER_VALIDATE_URL) && $_POST["iprm_add_url"] !== 'http://itunes.apple.com/us/') {
            $itunes_url = esc_url($_POST["iprm_add_url"]);
            iprm_update_option('iprm_active_product', $itunes_url);
            $podcast = new IPRM_Podcast($itunes_url);
            if ($podcast->itunes_id != '') {
                $notice = __('Your iTunes URL has successfully been updated.<br>', 'iprm_domain');
                $notice .= __('Please click CHECK MANUALLY to check for new reviews now. Otherwise, the next auto-check will be in approximately 4 hours.', 'iprm_domain');
            } else {
                $alert = __('iTunes URL could not be updated.  Please check the URL and try again.', 'iprm_domain') . '<br />';
                $alert .= '<i>' . __('Example: http://itunes.apple.com/us/podcast/professional-wordpress-podcast/id885696994.', 'iprm_domain') . '</i>';
            }
        } else {
            $alert = __('Invalid iTunes URL.', 'iprm_domain') . '<br />';
            $alert .= '<i>' . __('Example: http://itunes.apple.com/us/podcast/professional-wordpress-podcast/id885696994.', 'iprm_domain') . '</i>';
        }
    } elseif ($podcast->itunes_id == '') {
        $notice = __('Please enter your iTunes URL.', 'iprm_domain') . '<br />';
        $notice .= '<i>' . __('Example: http://itunes.apple.com/us/podcast/professional-wordpress-podcast/id885696994.', 'iprm_domain') . '</i>';
    }
    /* IF CHECK MANUALLY BUTTON IS PRESSED, CHECK FOR REVIEWS */
    if (isset($_POST["iprm_check_manually"]) && $_POST["iprm_check_manually"] != '') {
        $podcast->get_itunes_feed_contents();
    } elseif (isset($_POST["iprm_reset_all_data"]) && $_POST["iprm_reset_all_data"] != '') {
        $podcastArray = iprm_get_option('iprm_podcasts');
        /* IF ITS EMPTY IT EQUALS 1 FOR SOME REASON */
        if (is_array($podcastArray)) {
            foreach ($podcastArray as $url_str) {
                iprm_delete_product($url_str);
            }
        }
        iprm_delete_option('iprm_active_product');
        $notice = __('All settings and cache have been cleared.', 'iprm_domain');
    }
    /* IF CACHE IS EMPTY, DISPLAY NOTICE */
    if (empty($podcast->reviews) && isset($podcast->itunes_id)) {
        $podcast->get_itunes_feed_contents();
    }
    /* START OUTPUT */
    echo "<div id='iprm_wrapper'>";
    echo iprm_display_alert($alert);
    echo iprm_display_notice($notice);
    if (isset($podcast->itunes_id)) {
        echo $podcast->display_itunes_feed_summary();
    }
    ob_start();
    ?>
	
		<div id="iprm_settings_bar" class="iprm_panel">
		<form action="<?php 
    echo $_SERVER["REQUEST_URI"];
    ?>
" method="POST">
			<div id="iprm_input_settings">
				<h2><?php 
    _e('Settings', 'iprm_domain');
    ?>
</h2>
				<div class="iprm_panel_content">
					<h3><?php 
    _e('Product URL', 'iprm_domain');
    ?>
</h3>	
					<p><?php 
    _e('Adding a new product or manually checking may take a few minutes.', 'iprm_domain');
    ?>
</p>
					<input type="url" id="iprm_add_url" name="iprm_add_url" size="80" value="<?php 
    echo $podcast->itunes_url;
    ?>
">
				
					<input class="iprm_button_small" type="submit" name="iprm_update_url" value="UPDATE">
					
				</div>
			</div>
			<div id="iprm_crawl_settings">
				<h2><?php 
    _e('History', 'iprm_domain');
    ?>
</h2>
				<div class="iprm_panel_content">
					<h3><?php 
    _e('Recent History', 'iprm_domain');
    ?>
</h3>
					<p>
						<?php 
    $i = 1;
    if (is_array($podcast->review_cache_history)) {
        foreach (array_reverse($podcast->review_cache_history) as $item) {
            $i++;
            echo $item['time'] . ' Reviews: ' . $item['count'] . '<br />';
            if ($i > 5) {
                break;
            }
        }
    }
    ?>
					</p>
					<p><?php 
    _e('Reviews update automatically every 4 hours.', 'iprm_domain');
    ?>
</p>
					<input class="iprm_button_small" type="submit" name="iprm_check_manually" id="iprm_check_manually_btn"value="<?php 
    _e('CHECK MANUALLY', 'iprm_domain');
    ?>
">
				</div>
			</div>
		</form>
	</div>
		<?php 
    /* DISPLAY REVIEWS FROM CACHE */
    if (!empty($podcast->reviews)) {
        echo $podcast->display_page_reviews();
    }
    ?>
		
	
	<footer>
	<p>Flag icons by <a href="http://www.icondrawer.com" target="_blank">IconDrawer</a>.</p>
	</footer>
	</div>
	<?php 
    echo ob_get_clean();
}
 function get_itunes_feed_contents()
 {
     $this->get_itunes_metadata($this->itunes_url);
     $new_reviews = array();
     $new_settings = array();
     /* GET ARRAY OF ALL COUNTRY CODES AND COUNTRY NAMES */
     $country_codes = iprm_get_country_data('', '');
     /* CHECKS TO MAKE SURE ITUNES PODCAST URL IS DEFINED */
     if (isset($this->itunes_id)) {
         $urls_to_crawl = array();
         /* CHECK THROUGH THE REVIEW FEEDS FOR EVERY COUNTRY */
         foreach ($country_codes as $item) {
             $country_code = $item['code'];
             $url_xml = 'https://itunes.apple.com/' . $country_code . '/rss/customerreviews/id=' . $this->itunes_id . '/xml';
             $urls_to_crawl[] = $url_xml;
             $itunes_json1 = json_encode(wp_remote_get($url_xml));
             $data1 = json_decode($itunes_json1, TRUE);
             $feed_body1 = $data1['body'];
             $first_review_page_url = iprm_get_contents_inside_tag($feed_body1, '<link rel="first" href="', '"/>');
             $last_review_page_url = iprm_get_contents_inside_tag($feed_body1, '<link rel="last" href="', '"/>');
             $current_review_page_url = iprm_get_contents_inside_tag($feed_body1, '<link rel="self" href="', '"/>');
             $last_review_page_url = trim($last_review_page_url);
             $first_review_page_url = trim($first_review_page_url);
             if (strlen($first_review_page_url) != 0) {
                 $firstPage = iprm_get_contents_inside_tag($first_review_page_url, '/page=', '/id');
             } else {
                 $firstPage = 1;
             }
             $countryCodeOnUrl = iprm_get_contents_inside_tag($last_review_page_url, '.com/', '/rss');
             /* NOTE: WILL GIVE US LINKS AS LAST PAGE, THIS ONLY CONSIDERS LAST PAGE IF IT IS IN THE COUNTRY WE ARE INDEXING */
             if (strlen($last_review_page_url) != 0 && $countryCodeOnUrl == $country_code) {
                 $lastPage = iprm_get_contents_inside_tag($last_review_page_url, '/page=', '/id');
             } else {
                 $lastPage = 1;
             }
             $current_entry = iprm_get_contents_inside_tag($feed_body1, '<entry>', '</entry>');
             /* ONLY CRAWL IF THERE IS AT LEAST ONE REVIEW */
             if ($current_entry != "EMPTYSTR") {
                 $urls_to_crawl[] = $current_review_page_url;
             }
             if ($firstPage != $lastPage) {
                 $i = 1;
                 while ($i <= $lastPage) {
                     $current_review_page_url = 'https://itunes.apple.com/' . $country_code . '/rss/customerreviews/page=' . $i . '/id=' . $this->itunes_id . '/xml';
                     $urls_to_crawl[] = $current_review_page_url;
                     $i++;
                 }
             }
         }
         $urls_to_crawl = array_unique($urls_to_crawl);
         $limiter = 0;
         foreach ($urls_to_crawl as $url) {
             $limiter++;
             if ($limiter > 100) {
                 break;
             }
             $itunes_json = json_encode(wp_remote_get($url));
             $data2 = json_decode($itunes_json, TRUE);
             $feed_body = $data2['body'];
             /* LOOP THROUGH THE RAW CODE */
             while (strpos($feed_body, '<entry>') !== false) {
                 /* LOOK AT CODE IN BETWEEN FIRST INSTANCE OF ENTRY TAGS */
                 $opening_tag = '<entry>';
                 $closing_tag = '</entry>';
                 $pos1 = strpos($feed_body, $opening_tag);
                 $pos2 = strpos($feed_body, $closing_tag);
                 $current_entry = substr($feed_body, $pos1 + strlen($opening_tag), $pos2 - $pos1 - strlen($opening_tag));
                 /* GET REVIEW URL AND REVIEW URL COUNTRY CODE */
                 $review_url = iprm_get_contents_inside_tag($current_entry, '<uri>', '</uri>');
                 $review_url_country_code = substr($review_url, strpos($review_url, 'reviews') - 3, 2);
                 $name = iprm_get_contents_inside_tag($current_entry, '<name>', '</name>');
                 /* ADD NEW REVIEW TO REVIEW ARRAY */
                 if ($current_entry !== '' && $name != '') {
                     $new_review = array('country' => iprm_get_country_data($review_url_country_code, ''), 'review_date' => iprm_get_contents_inside_tag($current_entry, '<updated>', '</updated>'), 'rating' => iprm_get_contents_inside_tag($current_entry, '<im:rating>', '</im:rating>'), 'name' => iprm_get_contents_inside_tag($current_entry, '<name>', '</name>'), 'title' => iprm_get_contents_inside_tag($current_entry, '<title>', '</title>'), 'content' => iprm_get_contents_inside_tag($current_entry, '<content type="text">', '</content>'));
                     /* CHECK TO MAKE SURE THERE IS A RATING AND NAME BEFORE ADDING REVIEW TO ARRAY */
                     if ($new_review['rating'] == '' || $new_review['name'] == '' || $new_review['name'] == 'EMPTYSTR') {
                     } else {
                         array_push($new_reviews, $new_review);
                     }
                 }
                 /* REMOVE CODE AFTER FIRST INSTANCE OF ENTRY TAGS, SO THE NEXT LOOP ITERATION STARTS WITH THE NEXT INSTANCE OF ENTRY TAGS */
                 $feed_body = substr($feed_body, $pos2 + strlen($closing_tag));
             }
         }
         /* DE-DUPE NEW REVIEWS */
         $new_reviews = iprm_remove_duplicates_from_review_array($new_reviews);
         /* ADD CACHED REVIEWS TO NEW REVIEWS */
         if (!is_array($this->reviews)) {
             $this->reviews = array();
         }
         $this->reviews = array_merge($this->reviews, $new_reviews);
         /* REMOVE DUPLICATES FROM COMBINED REVIEW ARRAY */
         $this->reviews = iprm_remove_duplicates_from_review_array($this->reviews);
         /* SORT REVIEWS ARRAY BY DATE */
         foreach ($this->reviews as $key => $row) {
             $review_date[$key] = $row['review_date'];
             $review_country[$key] = $row['country'];
             $review_rating[$key] = $row['rating'];
             $review_name[$key] = $row['name'];
             $review_title[$key] = $row['title'];
             $review_content[$key] = $row['content'];
         }
         array_multisort($review_date, SORT_DESC, $review_name, SORT_ASC, $this->reviews);
         /* ADD TIME AND REVIEW COUNT TO REVIEW CACHE HISTORY */
         $review_count = count($this->reviews);
         $current_time = current_time('mysql');
         if (!is_array($this->review_cache_history)) {
             $this->review_cache_history = array();
         }
         array_push($this->review_cache_history, array('time' => $current_time, 'count' => $review_count));
         /* REPLACE OLD REVIEW CACHE HISTORY WITH NEW REVIEW CACHE HISTORY */
         $serialStr = serialize($this->review_cache_history);
         $dbSuccess = iprm_update_option("iprm_review_cache_history{$this->itunes_id}", $serialStr);
         if (!$dbSuccess) {
             echo "problem writing history cache";
         }
         $serialStr = serialize($this->reviews);
         /* REPLACE OLD CACHED REVIEWS WITH NEW CACHED REVIEWS */
         $file = WP_PLUGIN_DIR . "/itunes-podcast-review-manager/cache/cache_{$this->itunes_id}.cache";
         $fileWrite = file_put_contents($file, $serialStr);
         if ($fileWrite == false) {
             echo "problem writing review cache file";
         }
         /* RETURN COMBINED REVIEW ARRAY */
         return $this->reviews;
     } else {
         //echo "invalid itunes url";
     }
 }
Version: 3.4
Author: Doug Yuen
Author URI: http://efficientwp.com
License: GPLv2
*/
/*****************************
* GLOBAL VARIABLES
*****************************/
$iprm_current_version = '3.4';
/*****************************
* INCLUDES
*****************************/
require_once dirname(__FILE__) . '/includes/podcast.php';
require_once dirname(__FILE__) . '/includes/data-processing-functions.php';
require_once dirname(__FILE__) . '/includes/display-functions.php';
require_once dirname(__FILE__) . '/includes/main-page.php';
require_once dirname(__FILE__) . '/includes/premium-page.php';
require_once dirname(__FILE__) . '/includes/script-functions.php';
require_once dirname(__FILE__) . '/includes/upgrade-functions.php';
require_once dirname(__FILE__) . '/includes/utility-functions.php';
/* CHECK FOR UPGRADE CHANGES */
iprm_update_option('iprm_current_version', $iprm_current_version);
iprm_upgrade_check();
add_filter('cron_schedules', 'iprm_cron_add_every_four_hours');
add_action('iprm_schedule', 'iprm_automatic_check');
add_shortcode('iprm', 'iprm_display_as_shortcode');
register_deactivation_hook(__FILE__, 'iprm_deactivate');
/* SCHEDULE A CRON JOB TO CHECK FOR REVIEWS EVERY 4 HOURS */
if (!wp_next_scheduled('iprm_schedule')) {
    wp_schedule_event(time(), 'four_hours', 'iprm_schedule');
}