| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Name: Related Posts |
| 4 |
* Module Description: Display links to your related content under posts and pages. |
| 5 |
* First Introduced: 2.9 |
| 6 |
* Sort Order: 29 |
| 7 |
* Requires Connection: Yes |
| 8 |
* Auto Activate: No |
| 9 |
*/ |
| 10 |
class Jetpack_RelatedPosts_Module { |
| 11 |
/** |
| 12 |
* Class variables |
| 13 |
*/ |
| 14 |
private static $__instance = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Singleton implementation |
| 18 |
* |
| 19 |
* @return object |
| 20 |
*/ |
| 21 |
public static function instance() { |
| 22 |
if ( ! is_a( self::$__instance, 'Jetpack_RelatedPosts_Module' ) ) |
| 23 |
self::$__instance = new Jetpack_RelatedPosts_Module(); |
| 24 |
|
| 25 |
return self::$__instance; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register actions and filters |
| 30 |
* |
| 31 |
* @uses add_action, add_filter |
| 32 |
* @return null |
| 33 |
*/ |
| 34 |
private function __construct() { |
| 35 |
add_action( 'jetpack_module_loaded_related-posts', array( $this, 'action_on_load' ) ); |
| 36 |
add_action( 'jetpack_activate_module_related-posts', array( $this, 'action_on_activate' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* This action triggers when module is activated. |
| 41 |
* |
| 42 |
* @uses self::_get_post_count_local, self::_get_post_count_cloud, Jetpack::init, Jetpack::sync_reindex_trigger |
| 43 |
* @return null |
| 44 |
*/ |
| 45 |
public function action_on_activate() { |
| 46 |
// Trigger reindex if we have a post count mismatch |
| 47 |
if ( $this->_get_post_count_local() != $this->_get_post_count_cloud() ) { |
| 48 |
Jetpack::init()->sync->reindex_trigger(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* This action triggers if the module is in an active state, load related posts and options. |
| 54 |
* |
| 55 |
* @uses Jetpack_RelatedPosts::init, is_admin, Jetpack::enable_module_configurable, Jetpack::module_configuration_load, Jetpack_Sync::sync_posts |
| 56 |
* @return null |
| 57 |
*/ |
| 58 |
public function action_on_load() { |
| 59 |
require_once 'related-posts/jetpack-related-posts.php'; |
| 60 |
Jetpack_RelatedPosts::init(); |
| 61 |
|
| 62 |
if ( is_admin() ) { |
| 63 |
// Enable "Configure" button on module card |
| 64 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 65 |
Jetpack::module_configuration_load( __FILE__, array( $this, 'module_configuration_load' ) ); |
| 66 |
|
| 67 |
// Sync new posts |
| 68 |
Jetpack_Sync::sync_posts( __FILE__ ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Redirect configure button to Settings > Reading |
| 74 |
* |
| 75 |
* @uses wp_safe_redirect, admin_url |
| 76 |
* @return null |
| 77 |
*/ |
| 78 |
public function module_configuration_load() { |
| 79 |
wp_safe_redirect( admin_url( 'options-reading.php#jetpack_relatedposts' ) ); |
| 80 |
exit; |
| 81 |
} |
| 82 |
|
| 83 |
private function _get_post_count_local() { |
| 84 |
global $wpdb; |
| 85 |
return (int) $wpdb->get_var( |
| 86 |
"SELECT count(*) |
| 87 |
FROM {$wpdb->posts} |
| 88 |
WHERE post_status = 'publish' AND post_password = ''" |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
private function _get_post_count_cloud() { |
| 93 |
$blog_id = Jetpack::init()->get_option( 'id' ); |
| 94 |
|
| 95 |
$body = array( |
| 96 |
'size' => 1, |
| 97 |
); |
| 98 |
|
| 99 |
$response = wp_remote_post( |
| 100 |
"https://public-api.wordpress.com/rest/v1/sites/$blog_id/search", |
| 101 |
array( |
| 102 |
'timeout' => 10, |
| 103 |
'user-agent' => 'jetpack_related_posts', |
| 104 |
'sslverify' => true, |
| 105 |
'body' => $body, |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
if ( is_wp_error( $response ) ) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
$results = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 114 |
|
| 115 |
return (int) $results['results']['total']; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Do it. |
| 120 |
Jetpack_RelatedPosts_Module::instance(); |
| 121 |
|