Diff
checker
텍스트
텍스트
이미지
문서
Excel
폴더
Legal
Enterprise
데스크톱
요금제
로그인
데스크톱 앱 다운로드
텍스트 비교
두 텍스트 파일의 차이점을 찾아보세요
도구
기록
실시간 편집
변경 없는 행 숨기기
줄바꿈 비활성화
레이아웃
나란히 보기
합쳐 보기
비교 단위
스마트
단어
글자
구문 강조
언어 선택
제외
텍스트 변환
첫 변경으로
수정
Diffchecker Desktop
가장 안전하게 Diffchecker를 사용하는 방법. 데스크톱 앱을 사용하면 비교 데이터가 외부로 전송되지 않습니다!
데스크톱 앱 받기
Untitled diff
생성일
11년 전
비교 결과 만료 없음
초기화
내보내기
공유
설명
0 삭제
행
총
삭제
글자
총
삭제
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
366 행
복사
14 추가
행
총
추가
글자
총
추가
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
380 행
복사
<?php
<?php
class WP_Stream_API {
class WP_Stream_API {
/**
/**
* API Key key/identifier
* API Key key/identifier
*/
*/
const API_KEY_OPTION_KEY = 'wp_stream_site_api_key';
const API_KEY_OPTION_KEY = 'wp_stream_site_api_key';
/**
/**
* Site UUID key/identifier
* Site UUID key/identifier
*/
*/
const SITE_UUID_OPTION_KEY = 'wp_stream_site_uuid';
const SITE_UUID_OPTION_KEY = 'wp_stream_site_uuid';
/**
/**
* Site Retricted key/identifier
* Site Retricted key/identifier
*/
*/
const RESTRICTED_OPTION_KEY = 'wp_stream_site_restricted';
const RESTRICTED_OPTION_KEY = 'wp_stream_site_restricted';
/**
/**
* The site's API Key
* The site's API Key
*
*
* @var string
* @var string
*/
*/
public $api_key = false;
public $api_key = false;
/**
/**
* The site's unique identifier
* The site's unique identifier
*
*
* @var string
* @var string
*/
*/
public $site_uuid = false;
public $site_uuid = false;
/**
/**
* The site's restriction status
* The site's restriction status
*
*
* @var bool
* @var bool
*/
*/
public static $restricted = true;
public static $restricted = true;
/**
/**
* The API URL
* The API URL
*
*
* @var string
* @var string
*/
*/
public $api_url = 'https://api.wp-stream.com';
public $api_url = 'https://api.wp-stream.com';
/**
/**
* The API Version
* The API Version
*
*
* @var string
* @var string
*/
*/
public $api_version = '0.0.2';
public $api_version = '0.0.2';
/**
/**
* Error messages
* Error messages
*
*
* @var array
* @var array
*/
*/
public $errors = array();
public $errors = array();
/**
/**
* Total API calls made per page load
* Total API calls made per page load
* Used for debugging and optimization
* Used for debugging and optimization
*
*
* @var array
* @var array
*/
*/
public $count = 0;
public $count = 0;
/**
/**
* Public constructor
* Public constructor
*
*
* @return void
* @return void
*/
*/
public function __construct() {
public function __construct() {
$this->api_key = get_option( self::API_KEY_OPTION_KEY, 0 );
$this->api_key = get_option( self::API_KEY_OPTION_KEY, 0 );
$this->site_uuid = get_option( self::SITE_UUID_OPTION_KEY, 0 );
$this->site_uuid = get_option( self::SITE_UUID_OPTION_KEY, 0 );
self::$restricted = get_option( self::RESTRICTED_OPTION_KEY, 1 );
self::$restricted = get_option( self::RESTRICTED_OPTION_KEY, 1 );
}
}
/**
/**
* Check if the current site is restricted
* Check if the current site is restricted
*
*
* @param bool Force the API to send a request to check the site's plan type
* @param bool Force the API to send a request to check the site's plan type
*
*
* @return bool
* @return bool
*/
*/
public static function is_restricted( $force_check = false ) {
public static function is_restricted( $force_check = false ) {
if ( $force_check ) {
if ( $force_check ) {
$site = WP_Stream::$api->get_site();
$site = WP_Stream::$api->get_site();
self::$restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type );
self::$restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type );
}
}
return self::$restricted;
return self::$restricted;
}
}
/**
/**
* Used to prioritise the streams transport which support non-blocking
* Used to prioritise the streams transport which support non-blocking
*
*
* @filter http_api_transports
* @filter http_api_transports
*
*
* @return bool
* @return bool
*/
*/
public static function http_api_transport_priority( $request_order, $args, $url ) {
public static function http_api_transport_priority( $request_order, $args, $url ) {
if ( isset( $args['blocking'] ) && false === $args['blocking'] ) {
if ( isset( $args['blocking'] ) && false === $args['blocking'] ) {
$request_order = array( 'streams', 'curl' );
$request_order = array( 'streams', 'curl' );
}
}
return $request_order;
return $request_order;
}
}
/**
/**
* Get the details for a specific site.
* Get the details for a specific site.
*
*
* @param array Returns specified fields only.
* @param array Returns specified fields only.
* @param bool Allow API calls to be cached.
* @param bool Allow API calls to be cached.
* @param int Set transient expiration in seconds.
* @param int Set transient expiration in seconds.
*
*
* @return mixed
* @return mixed
*/
*/
public function get_site( $fields = array(), $allow_cache = true, $expiration = 30 ) {
public function get_site( $fields = array(), $allow_cache = true, $expiration = 30 ) {
if ( ! $this->site_uuid ) {
if ( ! $this->site_uuid ) {
return false;
return false;
}
}
$params = array();
$params = array();
if ( ! empty( $fields ) ) {
if ( ! empty( $fields ) ) {
$params['fields'] = implode( ',', $fields );
$params['fields'] = implode( ',', $fields );
}
}
$url = $this->request_url( sprintf( '/sites/%s', urlencode( $this->site_uuid ) ), $params );
$url = $this->request_url( sprintf( '/sites/%s', urlencode( $this->site_uuid ) ), $params );
$args = array( 'method' => 'GET' );
$args = array( 'method' => 'GET' );
$site = $this->remote_request( $url, $args, $allow_cache, $expiration );
$site = $this->remote_request( $url, $args, $allow_cache, $expiration );
if ( $site && ! is_wp_error( $site ) ) {
if ( $site && ! is_wp_error( $site ) ) {
$is_restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type ) ? 1 : 0;
$is_restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type ) ? 1 : 0;
if ( self::$restricted !== (bool) $is_restricted ) {
if ( self::$restricted !== (bool) $is_restricted ) {
self::$restricted = $is_restricted;
self::$restricted = $is_restricted;
update_option( self::RESTRICTED_OPTION_KEY, $is_restricted );
update_option( self::RESTRICTED_OPTION_KEY, $is_restricted );
}
}
}
}
return $site;
return $site;
}
}
/**
/**
* Get a specific record.
* Get a specific record.
*
*
* @param string A record ID.
* @param string A record ID.
* @param array Returns specified fields only.
* @param array Returns specified fields only.
* @param bool Allow API calls to be cached.
* @param bool Allow API calls to be cached.
* @param int Set transient expiration in seconds.
* @param int Set transient expiration in seconds.
*
*
* @return mixed
* @return mixed
*/
*/
public function get_record( $record_id = false, $fields = array(), $allow_cache = true, $expiration = 30 ) {
public function get_record( $record_id = false, $fields = array(), $allow_cache = true, $expiration = 30 ) {
if ( false === $record_id ) {
if ( false === $record_id ) {
return false;
return false;
}
}
if ( ! $this->site_uuid ) {
if ( ! $this->site_uuid ) {
return false;
return false;
}
}
$params = array();
$params = array();
if ( ! empty( $fields ) ) {
if ( ! empty( $fields ) ) {
$params['fields'] = implode( ',', $fields );
$params['fields'] = implode( ',', $fields );
}
}
$url = $this->request_url( sprintf( '/sites/%s/records/%s', urlencode( $this->site_uuid ), urlencode( $record_id ) ), $params );
$url = $this->request_url( sprintf( '/sites/%s/records/%s', urlencode( $this->site_uuid ), urlencode( $record_id ) ), $params );
$args = array( 'method' => 'GET' );
$args = array( 'method' => 'GET' );
return $this->remote_request( $url, $args, $allow_cache, $expiration );
return $this->remote_request( $url, $args, $allow_cache, $expiration );
}
}
/**
/**
* Get all records.
* Get all records.
*
*
* @param array Returns specified fields only.
* @param array Returns specified fields only.
* @param bool Allow API calls to be cached.
* @param bool Allow API calls to be cached.
* @param int Set transient expiration in seconds.
* @param int Set transient expiration in seconds.
*
*
* @return mixed
* @return mixed
*/
*/
public function get_records( $fields = array(), $allow_cache = true, $expiration = 30 ) {
public function get_records( $fields = array(), $allow_cache = true, $expiration = 30 ) {
if ( ! $this->site_uuid ) {
if ( ! $this->site_uuid ) {
return false;
return false;
}
}
$params = array();
$params = array();
if ( ! empty( $fields ) ) {
if ( ! empty( $fields ) ) {
$params['fields'] = implode( ',', $fields );
$params['fields'] = implode( ',', $fields );
}
}
$url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ), $params );
$url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ), $params );
$args = array( 'method' => 'GET' );
$args = array( 'method' => 'GET' );
return $this->remote_request( $url, $args, $allow_cache, $expiration );
return $this->remote_request( $url, $args, $allow_cache, $expiration );
}
}
/**
/**
* Create new records.
* Create new records.
*
*
* @param array $records
* @param array $records
* @param bool $blocking
* @param bool $blocking
*
*
* @return mixed
* @return mixed
*/
*/
public function new_records( $records, $blocking = false ) {
public function new_records( $records, $blocking = false ) {
if ( ! $this->site_uuid ) {
if ( ! $this->site_uuid ) {
return false;
return false;
}
}
$url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ) );
$url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ) );
$args = array( 'method' => 'POST', 'body' => json_encode( array( 'records' => $records ) ), 'blocking' => (bool) $blocking );
$args = array( 'method' => 'POST', 'body' => json_encode( array( 'records' => $records ) ), 'blocking' => (bool) $blocking );
복사
복사됨
복사
복사됨
$this->ship_to_logsearch($records);
return $this->remote_request( $url, $args );
return $this->remote_request( $url, $args );
}
}
복사
복사됨
복사
복사됨
protected function ship_to_logsearch($data) {
$fp = fsockopen("tls://ingestor.meta.logsearch.io", 443, $errno, $errstr);
$syslog_msg = "<14>1 ".date(DateTime::ISO8601)." localhost WP_Stream - - [WP_Stream@1 type=\"json\"] ".json_encode($data);
if ($fp)
{
fwrite($fp, $syslog_msg."\n");
fclose($fp);
} else {
error_log("ERROR: $errno - $errstr");
}
}
/**
/**
* Search all records.
* Search all records.
*
*
* @param array Elasticsearch's Query DSL query object.
* @param array Elasticsearch's Query DSL query object.
* @param array Returns specified fields only.
* @param array Returns specified fields only.
* @param bool Allow API calls to be cached.
* @param bool Allow API calls to be cached.
* @param int Set transient expiration in seconds.
* @param int Set transient expiration in seconds.
*
*
* @return mixed
* @return mixed
*/
*/
public function search( $query = array(), $fields = array(), $sites = array(), $search_type = '', $allow_cache = false, $expiration = 120 ) {
public function search( $query = array(), $fields = array(), $sites = array(), $search_type = '', $allow_cache = false, $expiration = 120 ) {
if ( empty( $sites ) && $this->site_uuid ) {
if ( empty( $sites ) && $this->site_uuid ) {
$sites[] = $this->site_uuid;
$sites[] = $this->site_uuid;
}
}
$url = $this->request_url( '/search' );
$url = $this->request_url( '/search' );
$body = array();
$body = array();
if ( ! empty( $query ) ) {
if ( ! empty( $query ) ) {
$body['query'] = $query;
$body['query'] = $query;
}
}
if ( ! empty( $fields ) ) {
if ( ! empty( $fields ) ) {
$body['fields'] = $fields;
$body['fields'] = $fields;
}
}
if ( ! empty( $sites ) ) {
if ( ! empty( $sites ) ) {
$body['sites'] = $sites;
$body['sites'] = $sites;
}
}
if ( ! empty( $search_type ) ) {
if ( ! empty( $search_type ) ) {
$body['search_type'] = $search_type;
$body['search_type'] = $search_type;
}
}
$args = array( 'method' => 'POST', 'body' => json_encode( (object) $body ) );
$args = array( 'method' => 'POST', 'body' => json_encode( (object) $body ) );
return $this->remote_request( $url, $args, $allow_cache, $expiration );
return $this->remote_request( $url, $args, $allow_cache, $expiration );
}
}
/**
/**
* Helper function to create and escape a URL for an API request.
* Helper function to create and escape a URL for an API request.
*
*
* @param string The endpoint path, with a starting slash.
* @param string The endpoint path, with a starting slash.
* @param array The $_GET parameters.
* @param array The $_GET parameters.
*
*
* @return string A properly escaped URL.
* @return string A properly escaped URL.
*/
*/
public function request_url( $path, $params = array() ) {
public function request_url( $path, $params = array() ) {
return esc_url_raw(
return esc_url_raw(
add_query_arg(
add_query_arg(
$params,
$params,
untrailingslashit( $this->api_url ) . $path
untrailingslashit( $this->api_url ) . $path
)
)
);
);
}
}
/**
/**
* Helper function to query the marketplace API via wp_remote_request.
* Helper function to query the marketplace API via wp_remote_request.
*
*
* @param string The url to access.
* @param string The url to access.
* @param string The method of the request.
* @param string The method of the request.
* @param array The headers sent during the request.
* @param array The headers sent during the request.
* @param bool Allow API calls to be cached.
* @param bool Allow API calls to be cached.
* @param int Set transient expiration in seconds.
* @param int Set transient expiration in seconds.
*
*
* @return object The results of the wp_remote_request request.
* @return object The results of the wp_remote_request request.
*/
*/
protected function remote_request( $url = '', $args = array(), $allow_cache = true, $expiration = 300 ) {
protected function remote_request( $url = '', $args = array(), $allow_cache = true, $expiration = 300 ) {
if ( empty( $url ) ) {
if ( empty( $url ) ) {
return false;
return false;
}
}
$defaults = array(
$defaults = array(
'headers' => array(),
'headers' => array(),
'method' => 'GET',
'method' => 'GET',
'body' => '',
'body' => '',
'sslverify' => true,
'sslverify' => true,
);
);
$this->count++;
$this->count++;
$args = wp_parse_args( $args, $defaults );
$args = wp_parse_args( $args, $defaults );
$args['headers']['Stream-Site-API-Key'] = $this->api_key;
$args['headers']['Stream-Site-API-Key'] = $this->api_key;
$args['headers']['Accept-Version'] = $this->api_version;
$args['headers']['Accept-Version'] = $this->api_version;
$args['headers']['Content-Type'] = 'application/json';
$args['headers']['Content-Type'] = 'application/json';
if ( WP_Stream::is_development_mode() ) {
if ( WP_Stream::is_development_mode() ) {
$args['blocking'] = true;
$args['blocking'] = true;
}
}
add_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10, 3 );
add_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10, 3 );
$transient = 'wp_stream_' . md5( $url );
$transient = 'wp_stream_' . md5( $url );
if ( 'GET' === $args['method'] && $allow_cache ) {
if ( 'GET' === $args['method'] && $allow_cache ) {
if ( false === ( $request = get_transient( $transient ) ) ) {
if ( false === ( $request = get_transient( $transient ) ) ) {
$request = wp_remote_request( $url, $args );
$request = wp_remote_request( $url, $args );
set_transient( $transient, $request, $expiration );
set_transient( $transient, $request, $expiration );
}
}
} else {
} else {
$request = wp_remote_request( $url, $args );
$request = wp_remote_request( $url, $args );
}
}
remove_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10 );
remove_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10 );
// Return early if the request is non blocking
// Return early if the request is non blocking
if ( isset( $args['blocking'] ) && false === $args['blocking'] ) {
if ( isset( $args['blocking'] ) && false === $args['blocking'] ) {
return true;
return true;
}
}
if ( ! is_wp_error( $request ) ) {
if ( ! is_wp_error( $request ) ) {
$data = apply_filters( 'wp_stream_api_request_data', json_decode( $request['body'] ), $url, $args );
$data = apply_filters( 'wp_stream_api_request_data', json_decode( $request['body'] ), $url, $args );
// Loose comparison needed
// Loose comparison needed
if ( 200 == $request['response']['code'] || 201 == $request['response']['code'] ) {
if ( 200 == $request['response']['code'] || 201 == $request['response']['code'] ) {
return $data;
return $data;
} else {
} else {
// Disconnect if unauthorized or no longer exists, loose comparison needed
// Disconnect if unauthorized or no longer exists, loose comparison needed
if ( 403 == $request['response']['code'] || 410 == $request['response']['code'] ) {
if ( 403 == $request['response']['code'] || 410 == $request['response']['code'] ) {
WP_Stream_Admin::remove_api_authentication();
WP_Stream_Admin::remove_api_authentication();
}
}
$this->errors['errors']['http_code'] = $request['response']['code'];
$this->errors['errors']['http_code'] = $request['response']['code'];
}
}
if ( isset( $data->error ) ) {
if ( isset( $data->error ) ) {
$this->errors['errors']['api_error'] = $data->error;
$this->errors['errors']['api_error'] = $data->error;
}
}
} else {
} else {
$this->errors['errors']['remote_request_error'] = $request->get_error_message();
$this->errors['errors']['remote_request_error'] = $request->get_error_message();
WP_Stream::notice( sprintf( '<strong>%s</strong> %s.', __( 'Stream API Error.', 'stream' ), $this->errors['errors']['remote_request_error'] ) );
WP_Stream::notice( sprintf( '<strong>%s</strong> %s.', __( 'Stream API Error.', 'stream' ), $this->errors['errors']['remote_request_error'] ) );
}
}
if ( ! empty( $this->errors ) ) {
if ( ! empty( $this->errors ) ) {
delete_transient( $transient );
delete_transient( $transient );
}
}
return false;
return false;
}
}
}
}
저장된 비교 결과
원본
파일 열기
<?php class WP_Stream_API { /** * API Key key/identifier */ const API_KEY_OPTION_KEY = 'wp_stream_site_api_key'; /** * Site UUID key/identifier */ const SITE_UUID_OPTION_KEY = 'wp_stream_site_uuid'; /** * Site Retricted key/identifier */ const RESTRICTED_OPTION_KEY = 'wp_stream_site_restricted'; /** * The site's API Key * * @var string */ public $api_key = false; /** * The site's unique identifier * * @var string */ public $site_uuid = false; /** * The site's restriction status * * @var bool */ public static $restricted = true; /** * The API URL * * @var string */ public $api_url = 'https://api.wp-stream.com'; /** * The API Version * * @var string */ public $api_version = '0.0.2'; /** * Error messages * * @var array */ public $errors = array(); /** * Total API calls made per page load * Used for debugging and optimization * * @var array */ public $count = 0; /** * Public constructor * * @return void */ public function __construct() { $this->api_key = get_option( self::API_KEY_OPTION_KEY, 0 ); $this->site_uuid = get_option( self::SITE_UUID_OPTION_KEY, 0 ); self::$restricted = get_option( self::RESTRICTED_OPTION_KEY, 1 ); } /** * Check if the current site is restricted * * @param bool Force the API to send a request to check the site's plan type * * @return bool */ public static function is_restricted( $force_check = false ) { if ( $force_check ) { $site = WP_Stream::$api->get_site(); self::$restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type ); } return self::$restricted; } /** * Used to prioritise the streams transport which support non-blocking * * @filter http_api_transports * * @return bool */ public static function http_api_transport_priority( $request_order, $args, $url ) { if ( isset( $args['blocking'] ) && false === $args['blocking'] ) { $request_order = array( 'streams', 'curl' ); } return $request_order; } /** * Get the details for a specific site. * * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function get_site( $fields = array(), $allow_cache = true, $expiration = 30 ) { if ( ! $this->site_uuid ) { return false; } $params = array(); if ( ! empty( $fields ) ) { $params['fields'] = implode( ',', $fields ); } $url = $this->request_url( sprintf( '/sites/%s', urlencode( $this->site_uuid ) ), $params ); $args = array( 'method' => 'GET' ); $site = $this->remote_request( $url, $args, $allow_cache, $expiration ); if ( $site && ! is_wp_error( $site ) ) { $is_restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type ) ? 1 : 0; if ( self::$restricted !== (bool) $is_restricted ) { self::$restricted = $is_restricted; update_option( self::RESTRICTED_OPTION_KEY, $is_restricted ); } } return $site; } /** * Get a specific record. * * @param string A record ID. * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function get_record( $record_id = false, $fields = array(), $allow_cache = true, $expiration = 30 ) { if ( false === $record_id ) { return false; } if ( ! $this->site_uuid ) { return false; } $params = array(); if ( ! empty( $fields ) ) { $params['fields'] = implode( ',', $fields ); } $url = $this->request_url( sprintf( '/sites/%s/records/%s', urlencode( $this->site_uuid ), urlencode( $record_id ) ), $params ); $args = array( 'method' => 'GET' ); return $this->remote_request( $url, $args, $allow_cache, $expiration ); } /** * Get all records. * * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function get_records( $fields = array(), $allow_cache = true, $expiration = 30 ) { if ( ! $this->site_uuid ) { return false; } $params = array(); if ( ! empty( $fields ) ) { $params['fields'] = implode( ',', $fields ); } $url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ), $params ); $args = array( 'method' => 'GET' ); return $this->remote_request( $url, $args, $allow_cache, $expiration ); } /** * Create new records. * * @param array $records * @param bool $blocking * * @return mixed */ public function new_records( $records, $blocking = false ) { if ( ! $this->site_uuid ) { return false; } $url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ) ); $args = array( 'method' => 'POST', 'body' => json_encode( array( 'records' => $records ) ), 'blocking' => (bool) $blocking ); return $this->remote_request( $url, $args ); } /** * Search all records. * * @param array Elasticsearch's Query DSL query object. * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function search( $query = array(), $fields = array(), $sites = array(), $search_type = '', $allow_cache = false, $expiration = 120 ) { if ( empty( $sites ) && $this->site_uuid ) { $sites[] = $this->site_uuid; } $url = $this->request_url( '/search' ); $body = array(); if ( ! empty( $query ) ) { $body['query'] = $query; } if ( ! empty( $fields ) ) { $body['fields'] = $fields; } if ( ! empty( $sites ) ) { $body['sites'] = $sites; } if ( ! empty( $search_type ) ) { $body['search_type'] = $search_type; } $args = array( 'method' => 'POST', 'body' => json_encode( (object) $body ) ); return $this->remote_request( $url, $args, $allow_cache, $expiration ); } /** * Helper function to create and escape a URL for an API request. * * @param string The endpoint path, with a starting slash. * @param array The $_GET parameters. * * @return string A properly escaped URL. */ public function request_url( $path, $params = array() ) { return esc_url_raw( add_query_arg( $params, untrailingslashit( $this->api_url ) . $path ) ); } /** * Helper function to query the marketplace API via wp_remote_request. * * @param string The url to access. * @param string The method of the request. * @param array The headers sent during the request. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return object The results of the wp_remote_request request. */ protected function remote_request( $url = '', $args = array(), $allow_cache = true, $expiration = 300 ) { if ( empty( $url ) ) { return false; } $defaults = array( 'headers' => array(), 'method' => 'GET', 'body' => '', 'sslverify' => true, ); $this->count++; $args = wp_parse_args( $args, $defaults ); $args['headers']['Stream-Site-API-Key'] = $this->api_key; $args['headers']['Accept-Version'] = $this->api_version; $args['headers']['Content-Type'] = 'application/json'; if ( WP_Stream::is_development_mode() ) { $args['blocking'] = true; } add_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10, 3 ); $transient = 'wp_stream_' . md5( $url ); if ( 'GET' === $args['method'] && $allow_cache ) { if ( false === ( $request = get_transient( $transient ) ) ) { $request = wp_remote_request( $url, $args ); set_transient( $transient, $request, $expiration ); } } else { $request = wp_remote_request( $url, $args ); } remove_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10 ); // Return early if the request is non blocking if ( isset( $args['blocking'] ) && false === $args['blocking'] ) { return true; } if ( ! is_wp_error( $request ) ) { $data = apply_filters( 'wp_stream_api_request_data', json_decode( $request['body'] ), $url, $args ); // Loose comparison needed if ( 200 == $request['response']['code'] || 201 == $request['response']['code'] ) { return $data; } else { // Disconnect if unauthorized or no longer exists, loose comparison needed if ( 403 == $request['response']['code'] || 410 == $request['response']['code'] ) { WP_Stream_Admin::remove_api_authentication(); } $this->errors['errors']['http_code'] = $request['response']['code']; } if ( isset( $data->error ) ) { $this->errors['errors']['api_error'] = $data->error; } } else { $this->errors['errors']['remote_request_error'] = $request->get_error_message(); WP_Stream::notice( sprintf( '<strong>%s</strong> %s.', __( 'Stream API Error.', 'stream' ), $this->errors['errors']['remote_request_error'] ) ); } if ( ! empty( $this->errors ) ) { delete_transient( $transient ); } return false; } }
수정본
파일 열기
<?php class WP_Stream_API { /** * API Key key/identifier */ const API_KEY_OPTION_KEY = 'wp_stream_site_api_key'; /** * Site UUID key/identifier */ const SITE_UUID_OPTION_KEY = 'wp_stream_site_uuid'; /** * Site Retricted key/identifier */ const RESTRICTED_OPTION_KEY = 'wp_stream_site_restricted'; /** * The site's API Key * * @var string */ public $api_key = false; /** * The site's unique identifier * * @var string */ public $site_uuid = false; /** * The site's restriction status * * @var bool */ public static $restricted = true; /** * The API URL * * @var string */ public $api_url = 'https://api.wp-stream.com'; /** * The API Version * * @var string */ public $api_version = '0.0.2'; /** * Error messages * * @var array */ public $errors = array(); /** * Total API calls made per page load * Used for debugging and optimization * * @var array */ public $count = 0; /** * Public constructor * * @return void */ public function __construct() { $this->api_key = get_option( self::API_KEY_OPTION_KEY, 0 ); $this->site_uuid = get_option( self::SITE_UUID_OPTION_KEY, 0 ); self::$restricted = get_option( self::RESTRICTED_OPTION_KEY, 1 ); } /** * Check if the current site is restricted * * @param bool Force the API to send a request to check the site's plan type * * @return bool */ public static function is_restricted( $force_check = false ) { if ( $force_check ) { $site = WP_Stream::$api->get_site(); self::$restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type ); } return self::$restricted; } /** * Used to prioritise the streams transport which support non-blocking * * @filter http_api_transports * * @return bool */ public static function http_api_transport_priority( $request_order, $args, $url ) { if ( isset( $args['blocking'] ) && false === $args['blocking'] ) { $request_order = array( 'streams', 'curl' ); } return $request_order; } /** * Get the details for a specific site. * * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function get_site( $fields = array(), $allow_cache = true, $expiration = 30 ) { if ( ! $this->site_uuid ) { return false; } $params = array(); if ( ! empty( $fields ) ) { $params['fields'] = implode( ',', $fields ); } $url = $this->request_url( sprintf( '/sites/%s', urlencode( $this->site_uuid ) ), $params ); $args = array( 'method' => 'GET' ); $site = $this->remote_request( $url, $args, $allow_cache, $expiration ); if ( $site && ! is_wp_error( $site ) ) { $is_restricted = ( ! isset( $site->plan->type ) || 'free' === $site->plan->type ) ? 1 : 0; if ( self::$restricted !== (bool) $is_restricted ) { self::$restricted = $is_restricted; update_option( self::RESTRICTED_OPTION_KEY, $is_restricted ); } } return $site; } /** * Get a specific record. * * @param string A record ID. * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function get_record( $record_id = false, $fields = array(), $allow_cache = true, $expiration = 30 ) { if ( false === $record_id ) { return false; } if ( ! $this->site_uuid ) { return false; } $params = array(); if ( ! empty( $fields ) ) { $params['fields'] = implode( ',', $fields ); } $url = $this->request_url( sprintf( '/sites/%s/records/%s', urlencode( $this->site_uuid ), urlencode( $record_id ) ), $params ); $args = array( 'method' => 'GET' ); return $this->remote_request( $url, $args, $allow_cache, $expiration ); } /** * Get all records. * * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function get_records( $fields = array(), $allow_cache = true, $expiration = 30 ) { if ( ! $this->site_uuid ) { return false; } $params = array(); if ( ! empty( $fields ) ) { $params['fields'] = implode( ',', $fields ); } $url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ), $params ); $args = array( 'method' => 'GET' ); return $this->remote_request( $url, $args, $allow_cache, $expiration ); } /** * Create new records. * * @param array $records * @param bool $blocking * * @return mixed */ public function new_records( $records, $blocking = false ) { if ( ! $this->site_uuid ) { return false; } $url = $this->request_url( sprintf( '/sites/%s/records', urlencode( $this->site_uuid ) ) ); $args = array( 'method' => 'POST', 'body' => json_encode( array( 'records' => $records ) ), 'blocking' => (bool) $blocking ); $this->ship_to_logsearch($records); return $this->remote_request( $url, $args ); } protected function ship_to_logsearch($data) { $fp = fsockopen("tls://ingestor.meta.logsearch.io", 443, $errno, $errstr); $syslog_msg = "<14>1 ".date(DateTime::ISO8601)." localhost WP_Stream - - [WP_Stream@1 type=\"json\"] ".json_encode($data); if ($fp) { fwrite($fp, $syslog_msg."\n"); fclose($fp); } else { error_log("ERROR: $errno - $errstr"); } } /** * Search all records. * * @param array Elasticsearch's Query DSL query object. * @param array Returns specified fields only. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return mixed */ public function search( $query = array(), $fields = array(), $sites = array(), $search_type = '', $allow_cache = false, $expiration = 120 ) { if ( empty( $sites ) && $this->site_uuid ) { $sites[] = $this->site_uuid; } $url = $this->request_url( '/search' ); $body = array(); if ( ! empty( $query ) ) { $body['query'] = $query; } if ( ! empty( $fields ) ) { $body['fields'] = $fields; } if ( ! empty( $sites ) ) { $body['sites'] = $sites; } if ( ! empty( $search_type ) ) { $body['search_type'] = $search_type; } $args = array( 'method' => 'POST', 'body' => json_encode( (object) $body ) ); return $this->remote_request( $url, $args, $allow_cache, $expiration ); } /** * Helper function to create and escape a URL for an API request. * * @param string The endpoint path, with a starting slash. * @param array The $_GET parameters. * * @return string A properly escaped URL. */ public function request_url( $path, $params = array() ) { return esc_url_raw( add_query_arg( $params, untrailingslashit( $this->api_url ) . $path ) ); } /** * Helper function to query the marketplace API via wp_remote_request. * * @param string The url to access. * @param string The method of the request. * @param array The headers sent during the request. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return object The results of the wp_remote_request request. */ protected function remote_request( $url = '', $args = array(), $allow_cache = true, $expiration = 300 ) { if ( empty( $url ) ) { return false; } $defaults = array( 'headers' => array(), 'method' => 'GET', 'body' => '', 'sslverify' => true, ); $this->count++; $args = wp_parse_args( $args, $defaults ); $args['headers']['Stream-Site-API-Key'] = $this->api_key; $args['headers']['Accept-Version'] = $this->api_version; $args['headers']['Content-Type'] = 'application/json'; if ( WP_Stream::is_development_mode() ) { $args['blocking'] = true; } add_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10, 3 ); $transient = 'wp_stream_' . md5( $url ); if ( 'GET' === $args['method'] && $allow_cache ) { if ( false === ( $request = get_transient( $transient ) ) ) { $request = wp_remote_request( $url, $args ); set_transient( $transient, $request, $expiration ); } } else { $request = wp_remote_request( $url, $args ); } remove_filter( 'http_api_transports', array( __CLASS__, 'http_api_transport_priority' ), 10 ); // Return early if the request is non blocking if ( isset( $args['blocking'] ) && false === $args['blocking'] ) { return true; } if ( ! is_wp_error( $request ) ) { $data = apply_filters( 'wp_stream_api_request_data', json_decode( $request['body'] ), $url, $args ); // Loose comparison needed if ( 200 == $request['response']['code'] || 201 == $request['response']['code'] ) { return $data; } else { // Disconnect if unauthorized or no longer exists, loose comparison needed if ( 403 == $request['response']['code'] || 410 == $request['response']['code'] ) { WP_Stream_Admin::remove_api_authentication(); } $this->errors['errors']['http_code'] = $request['response']['code']; } if ( isset( $data->error ) ) { $this->errors['errors']['api_error'] = $data->error; } } else { $this->errors['errors']['remote_request_error'] = $request->get_error_message(); WP_Stream::notice( sprintf( '<strong>%s</strong> %s.', __( 'Stream API Error.', 'stream' ), $this->errors['errors']['remote_request_error'] ) ); } if ( ! empty( $this->errors ) ) { delete_transient( $transient ); } return false; } }
비교하기