Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
876790b
Added an interface as a guide for the new package methods.
zinigor May 9, 2019
4a6db30
Add package structure
oskosk May 14, 2019
f13489f
Initialized the composer library, added gitignore.
zinigor May 14, 2019
d2e74b1
Removed phpunit from connection dependencies.
zinigor May 14, 2019
9d116c8
Added first real code to the connection manager class, as well as tes…
zinigor May 16, 2019
3a9d745
Renamed the test folder to tests.
zinigor May 17, 2019
acf4c69
Made the option name be a class constant.
zinigor May 17, 2019
67c976e
Added 10up's WP_Mock as a dependency, refactored the injection to use…
zinigor May 17, 2019
b986e3a
Changed the internal method accessibility to protected.
zinigor May 21, 2019
54b594e
Initialized an empty package repo for the options manager.
zinigor May 21, 2019
a04464e
Added the options package as a dependency to the master repo.
zinigor May 21, 2019
48cfad0
Added a package structure placeholder for the options manager.
zinigor May 21, 2019
cb68722
Added the first test for the delete option method.
zinigor May 21, 2019
5a2f771
Added the update method and a test for it.
zinigor May 22, 2019
174d545
Added a test for the grouped option deletion.
zinigor May 22, 2019
e06b5da
Added the group updater method and a test for it.
zinigor May 22, 2019
471113d
Added the grouped option getter and a test for it.
zinigor May 22, 2019
e48ecab
Added a test for the noncompact option getter.
zinigor May 22, 2019
45fd847
Added tests for network option manipulation.
zinigor May 24, 2019
b1dbef3
Removed autoloader requirement in favour of the existing one.
zinigor May 27, 2019
358c2fb
Added the connection manager secret generator to actual Jetpack class.
zinigor May 29, 2019
ab347da
Removed individual gitignore files in favor of a parent-level file, p…
zinigor May 29, 2019
18c3261
Moved packages source code into src folders.
zinigor May 29, 2019
3925714
Removed the V7 part of the namespace.
zinigor May 30, 2019
12ae781
Remove v7 from namespace in the Manager Interface
roccotripaldi May 30, 2019
17aa99a
Remove V7 from namespaces
gravityrail May 30, 2019
b1066c9
Add Automattic to the namespace
gravityrail May 30, 2019
1288f29
Rename jetpack-[name] packages to [name]
gravityrail May 30, 2019
09a0774
Update composer lock
gravityrail May 30, 2019
afa5096
Adding a package for Jetpack Constants
roccotripaldi May 30, 2019
57ea581
Attempting to fix WPCOM tests.
zinigor May 31, 2019
5e2c656
Excluding packages folder from linting for PHP 5.6 and earlier.
zinigor May 31, 2019
1b9bb2a
adjust syntax in connection tests
roccotripaldi Jun 3, 2019
98ba081
removing new unit tests. i'm not sure why they are failing. we can re…
roccotripaldi Jun 3, 2019
56aedcc
reverting last commit, and fixing tests in `packages/options`
roccotripaldi Jun 3, 2019
5be105f
ah, one more
roccotripaldi Jun 3, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions class.jetpack-options.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
<?php

use \Automattic\Jetpack\Options\Manager as Options_Manager;

if ( class_exists( '\Automattic\Jetpack\Options\Manager' ) ) :

/**
* The option manager class that will eventually replace Jetpack_Options
*/
class Jetpack_Options_Manager extends Options_Manager {

/**
* Returns an array of option names for a given type.
*
* @param string $type The type of option to return. Defaults to 'compact'.
*
* @return array
*/
public function get_option_names( $type = 'compact' ) {
switch ( $type ) {
case 'non-compact':
case 'non_compact':
return array( 'secrets' );

case 'private':
return array();

case 'network':
return array();
}

return array();
}
}

endif;

class Jetpack_Options {

/**
Expand Down
60 changes: 27 additions & 33 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
*/

use \Automattic\Jetpack\Connection\Manager as Connection_Manager;

require_once( JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php' );

class Jetpack {
Expand Down Expand Up @@ -332,6 +334,11 @@ class Jetpack {
*/
public $json_api_authorization_request = array();

/**
* @var \Automattic\Jetpack\Connection\Manager
*/
protected $connection_manager;

/**
* @var string Transient key used to prevent multiple simultaneous plugin upgrades
*/
Expand Down Expand Up @@ -532,6 +539,18 @@ private function __construct() {
Jetpack_Network::init();
}

add_filter( 'jetpack_connection_option_manager', function() {
return new Jetpack_Options_Manager();
} );

add_filter( 'jetpack_connection_secret_generator', function( $callable ) {
return function() {
return wp_generate_password( 32, false );
};
} );

$this->connection_manager = new Connection_Manager( );

/**
* Prepare Gutenberg Editor functionality
*/
Expand Down Expand Up @@ -4536,6 +4555,7 @@ function build_connect_url( $raw = false, $redirect = false, $from = false, $reg
);

if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {

// Generating a register URL instead to refresh the existing token
return $this->build_connect_url( $raw, $redirect, $from, true );
}
Expand Down Expand Up @@ -4945,55 +4965,29 @@ public static function xmlrpc_api_url() {
* @return array
*/
public static function generate_secrets( $action, $user_id = false, $exp = 600 ) {
if ( ! $user_id ) {
if ( false === $user_id ) {
$user_id = get_current_user_id();
}

$secret_name = 'jetpack_' . $action . '_' . $user_id;
$secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );

if (
isset( $secrets[ $secret_name ] ) &&
$secrets[ $secret_name ]['exp'] > time()
) {
return $secrets[ $secret_name ];
}

$secret_value = array(
'secret_1' => wp_generate_password( 32, false ),
'secret_2' => wp_generate_password( 32, false ),
'exp' => time() + $exp,
);

$secrets[ $secret_name ] = $secret_value;

Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets );
return $secrets[ $secret_name ];
return self::init()->connection_manager->generate_secrets( $action, $user_id, $exp );
}

public static function get_secrets( $action, $user_id ) {
$secret_name = 'jetpack_' . $action . '_' . $user_id;
$secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
$secrets = self::init()->connection_manager->get_secrets( $action, $user_id );

if ( ! isset( $secrets[ $secret_name ] ) ) {
if ( Connection_Manager::SECRETS_MISSING === $secrets ) {
return new WP_Error( 'verify_secrets_missing', 'Verification secrets not found' );
}

if ( $secrets[ $secret_name ]['exp'] < time() ) {
self::delete_secrets( $action, $user_id );
if ( Connection_Manager::SECRETS_EXPIRED === $secrets ) {
return new WP_Error( 'verify_secrets_expired', 'Verification took too long' );
}

return $secrets[ $secret_name ];
return $secrets;
}

public static function delete_secrets( $action, $user_id ) {
$secret_name = 'jetpack_' . $action . '_' . $user_id;
$secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
if ( isset( $secrets[ $secret_name ] ) ) {
unset( $secrets[ $secret_name ] );
Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets );
}
return self::init()->connection_manager->delete_secrets( $action, $user_id );
}

/**
Expand Down
22 changes: 21 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
"issues": "https://github.com/Automattic/jetpack/issues"
},
"require": {
"composer/installers": "1.6.0",
"ext-openssl": "*",
"automattic/jetpack-logo": "@dev"
"automattic/jetpack-connection": "^1.0",
"automattic/jetpack-options": "^1.0",
"automattic/jetpack-logo": "@dev",
"automattic/jetpack-constants": "@dev"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "0.5.0",
Expand All @@ -18,6 +22,22 @@
"phpcompatibility/phpcompatibility-wp": "2.0.0",
"automattic/jetpack-autoloader": "@dev"
},
"repositories": [
{
"type": "path",
"url": "./packages/jetpack-connection",
"options": {
"symlink": true
}
},
{
"type": "path",
"url": "./packages/jetpack-options",
"options": {
"symlink": true
}
}
],
"scripts": {
"php:compatibility": "composer install && vendor/bin/phpcs -p -s --runtime-set testVersion '5.3-' --standard=PHPCompatibilityWP --ignore=docker,tools,tests,node_modules,vendor --extensions=php",
"php:lint": "composer install && vendor/bin/phpcs -p -s",
Expand Down
Loading