Initial Commit

This commit is contained in:
Rumperuu 2018-03-21 18:19:20 +00:00
parent 4c352bf02e
commit 1ab6e5f0b0
1085 changed files with 195258 additions and 0 deletions

View file

@ -0,0 +1,12 @@
<?php
class WPORG_Platform {
static function get_site( $blog_id ) {
require_once dirname( __FILE__ ) . '/class.json-api-site-jetpack.php';
return new Jetpack_Site( $blog_id );
}
}
function wpcom_get_sal_site( $blog_id ) {
return WPORG_Platform::get_site( $blog_id );
}

View file

@ -0,0 +1,7 @@
<?php
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
require_once dirname( __FILE__ ) . '/class.json-api-platform-wpcom.php';
} else {
require_once dirname( __FILE__ ) . '/class.json-api-platform-jetpack.php';
}

View file

@ -0,0 +1,188 @@
<?php
/**
* Base class for the Site Abstraction Layer (SAL)
**/
abstract class SAL_Site {
public $blog_id;
public function __construct( $blog_id ) {
$this->blog_id = $blog_id;
}
abstract public function has_videopress();
abstract public function upgraded_filetypes_enabled();
abstract public function is_mapped_domain();
abstract public function is_redirect();
abstract public function featured_images_enabled();
abstract public function has_wordads();
abstract public function get_frame_nonce();
abstract public function allowed_file_types();
abstract public function get_post_formats();
abstract public function is_private();
abstract public function is_following();
abstract public function get_subscribers_count();
abstract public function get_locale();
abstract public function is_jetpack();
abstract public function get_jetpack_modules();
abstract public function is_vip();
abstract public function is_multisite();
abstract public function is_single_user_site();
abstract public function get_plan();
abstract public function get_ak_vp_bundle_enabled();
abstract public function before_render();
abstract public function after_render( &$response );
abstract public function after_render_options( &$options );
function user_can_manage() {
current_user_can( 'manage_options' ); // remove this attribute in favor of 'capabilities'
}
function get_registered_date() {
if ( function_exists( 'get_blog_details' ) ) {
$blog_details = get_blog_details();
if ( ! empty( $blog_details->registered ) ) {
return $this->format_date( $blog_details->registered );
}
}
return '0000-00-00T00:00:00+00:00';
}
function get_capabilities() {
return array(
'edit_pages' => current_user_can( 'edit_pages' ),
'edit_posts' => current_user_can( 'edit_posts' ),
'edit_others_posts' => current_user_can( 'edit_others_posts' ),
'edit_others_pages' => current_user_can( 'edit_others_pages' ),
'delete_posts' => current_user_can( 'delete_posts' ),
'delete_others_posts' => current_user_can( 'delete_others_posts' ),
'edit_theme_options' => current_user_can( 'edit_theme_options' ),
'edit_users' => current_user_can( 'edit_users' ),
'list_users' => current_user_can( 'list_users' ),
'manage_categories' => current_user_can( 'manage_categories' ),
'manage_options' => current_user_can( 'manage_options' ),
'promote_users' => current_user_can( 'promote_users' ),
'publish_posts' => current_user_can( 'publish_posts' ),
'upload_files' => current_user_can( 'upload_files' ),
'view_stats' => stats_is_blog_user( $this->blog_id )
);
}
function is_visible() {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$visible = (array) get_user_meta( $current_user->ID, 'blog_visibility', true );
$is_visible = true;
if ( isset( $visible[ $this->blog_id ] ) ) {
$is_visible = (bool) $visible[ $this->blog_id ];
}
// null and true are visible
return $is_visible;
}
return null;
}
function get_logo() {
// Set an empty response array.
$logo_setting = array(
'id' => (int) 0,
'sizes' => array(),
'url' => '',
);
// Get current site logo values.
$logo = get_option( 'site_logo' );
// Update the response array if there's a site logo currenty active.
if ( $logo && 0 != $logo['id'] ) {
$logo_setting['id'] = $logo['id'];
$logo_setting['url'] = $logo['url'];
foreach ( $logo['sizes'] as $size => $properties ) {
$logo_setting['sizes'][ $size ] = $properties;
}
}
return $logo_setting;
}
/**
* Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00
*
* @param $date_gmt (string) GMT datetime string.
* @param $date (string) Optional. Used to calculate the offset from GMT.
*
* @return string
*/
function format_date( $date_gmt, $date = null ) {
$timestamp_gmt = strtotime( "$date_gmt+0000" );
if ( null === $date ) {
$timestamp = $timestamp_gmt;
$hours = $minutes = $west = 0;
} else {
$date_time = date_create( "$date+0000" );
if ( $date_time ) {
$timestamp = date_format( $date_time, 'U' );
} else {
$timestamp = 0;
}
// "0000-00-00 00:00:00" == -62169984000
if ( - 62169984000 == $timestamp_gmt ) {
// WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts
// WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts
// Try to guess the correct offset from the blog's options.
$timezone_string = get_option( 'timezone_string' );
if ( $timezone_string && $date_time ) {
$timezone = timezone_open( $timezone_string );
if ( $timezone ) {
$offset = $timezone->getOffset( $date_time );
}
} else {
$offset = 3600 * get_option( 'gmt_offset' );
}
} else {
$offset = $timestamp - $timestamp_gmt;
}
$west = $offset < 0;
$offset = abs( $offset );
$hours = (int) floor( $offset / 3600 );
$offset -= $hours * 3600;
$minutes = (int) floor( $offset / 60 );
}
return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes );
}
}

View file

@ -0,0 +1,124 @@
<?php
require_once dirname( __FILE__ ) . '/class.json-api-site-base.php';
abstract class Abstract_Jetpack_Site extends SAL_Site {
abstract protected function get_constant( $name );
abstract protected function current_theme_supports( $feature_name );
abstract protected function get_theme_support( $feature_name );
abstract protected function get_mock_option( $name );
abstract protected function get_jetpack_version();
abstract protected function get_updates();
function before_render() {
}
function after_render( &$response ) {
// Add the updates only make them visible if the user has manage options permission and the site is the main site of the network
if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) {
$jetpack_update = $this->get_updates();
if ( ! empty( $jetpack_update ) ) {
// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
unset( $jetpack_update['wp_version'] );
// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
unset( $jetpack_update['site_is_version_controlled'] );
$response['updates'] = $jetpack_update;
}
}
}
function after_render_options( &$options ) {
$options['jetpack_version'] = $this->get_jetpack_version();
if ( $main_network_site = $this->get_mock_option( 'main_network_site' ) ) {
$options['main_network_site'] = (string) rtrim( $main_network_site, '/' );
}
if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) {
$options['active_modules'] = (array) array_values( $active_modules );
}
$options['software_version'] = (string) $this->get_mock_option( 'wp_version' );
$options['max_upload_size'] = $this->get_mock_option( 'max_upload_size', false );
// Sites have to prove that they are not main_network site.
// If the sync happends right then we should be able to see that we are not dealing with a network site
$options['is_multi_network'] = (bool) $this->get_mock_option( 'is_main_network', true );
$options['is_multi_site'] = (bool) $this->get_mock_option( 'is_multi_site', true );
$file_mod_disabled_reasons = array_keys( array_filter( array(
'automatic_updater_disabled' => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ),
// WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
'wp_auto_update_core_disabled' => ! ( (bool) $this->get_constant( 'WP_AUTO_UPDATE_CORE' ) ),
'is_version_controlled' => (bool) $this->get_mock_option( 'is_version_controlled' ),
// By default we assume that site does have system write access if the value is not set yet.
'has_no_file_system_write_access' => ! (bool) ( $this->get_mock_option( 'has_file_system_write_access' ) ),
'disallow_file_mods' => (bool) $this->get_constant( 'DISALLOW_FILE_MODS' ),
) ) );
$options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
}
function get_jetpack_modules() {
if ( is_user_member_of_blog() ) {
return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
}
return null;
}
function is_vip() {
return false; // this may change for VIP Go sites, which sync using Jetpack
}
function is_multisite() {
return (bool) $this->get_mock_option( 'is_multi_site' );
}
function is_single_user_site() {
return (bool) $this->get_mock_option( 'single_user_site' );
}
function featured_images_enabled() {
return $this->current_theme_supports( 'post-thumbnails' );
}
function get_post_formats() {
// deprecated - see separate endpoint. get a list of supported post formats
$all_formats = get_post_format_strings();
$supported = $this->get_theme_support( 'post-formats' );
$supported_formats = array();
if ( isset( $supported[0] ) ) {
foreach ( $supported[0] as $format ) {
$supported_formats[ $format ] = $all_formats[ $format ];
}
}
return $supported_formats;
}
/**
* Private methods
**/
private function is_main_site( $response ) {
if ( isset( $response['options']['main_network_site'], $response['options']['unmapped_url'] ) ) {
$main_network_site_url = set_url_scheme( $response['options']['main_network_site'], 'http' );
$unmapped_url = set_url_scheme( $response['options']['unmapped_url'], 'http' );
if ( $unmapped_url === $main_network_site_url ) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,126 @@
<?php
require_once dirname( __FILE__ ) . '/class.json-api-site-jetpack-base.php';
// this code runs on Jetpack (.org) sites
class Jetpack_Site extends Abstract_Jetpack_Site {
protected function get_mock_option( $name ) {
return get_option( 'jetpack_'.$name );
}
protected function get_constant( $name ) {
if ( defined( $name) ) {
return constant( $name );
}
return null;
}
protected function current_theme_supports( $feature_name ) {
return current_theme_supports( $feature_name );
}
protected function get_theme_support( $feature_name ) {
return get_theme_support( $feature_name );
}
protected function get_updates() {
return (array) Jetpack::get_updates();
}
function get_id() {
return $this->platform->token->blog_id;
}
function has_videopress() {
// TODO - this only works on wporg site - need to detect videopress option for remote Jetpack site on WPCOM
$videopress = Jetpack_Options::get_option( 'videopress', array() );
if ( isset( $videopress['blog_id'] ) && $videopress['blog_id'] > 0 ) {
return true;
}
return false;
}
function upgraded_filetypes_enabled() {
return true;
}
function is_mapped_domain() {
return true;
}
function is_redirect() {
return false;
}
function is_following() {
return false;
}
function has_wordads() {
// TODO: any way to detect wordads on the site, or does it need to be modified on the way through?
return false;
}
function get_frame_nonce() {
return false;
}
function allowed_file_types() {
$allowed_file_types = array();
// http://codex.wordpress.org/Uploading_Files
$mime_types = get_allowed_mime_types();
foreach ( $mime_types as $type => $mime_type ) {
$extras = explode( '|', $type );
foreach ( $extras as $extra ) {
$allowed_file_types[] = $extra;
}
}
return $allowed_file_types;
}
function is_private() {
return false;
}
function get_plan() {
return false;
}
function get_subscribers_count() {
return 0; // special magic fills this in on the WPCOM side
}
function get_capabilities() {
return false;
}
function get_locale() {
return get_bloginfo( 'language' );
}
function get_icon() {
if ( function_exists( 'jetpack_site_icon_url' ) && function_exists( 'jetpack_photon_url' ) ) {
return array(
'img' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 80 ), array( 'w' => 80 ), 'https' ),
'ico' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 16 ), array( 'w' => 16 ), 'https' ),
);
}
return null;
}
function is_jetpack() {
return true;
}
protected function get_jetpack_version() {
return JETPACK__VERSION;
}
function get_ak_vp_bundle_enabled() {}
}