From d677d795d5e9a893a49e72d15e0987a3519951a4 Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 5 Mar 2018 20:25:01 +0100 Subject: [PATCH 1/8] improve readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef4be45..e9faba3 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ This plugin is storing a value for each language into database. That means at # Requirements - [WordPress](https://wordpress.org/) 4.7+ -- Tested up to 4.9.1 +- Tested up to 4.9.4 - PHP 5.6 -- [Advanced Custom Fields](https://www.advancedcustomfields.com/pro) +- [Advanced Custom Fields](https://www.advancedcustomfields.com/pro) 5.6.0+ - [Polylang](https://polylang.pro/) # Installation From 1508e26719ab42868fb17286bbd3220dff1ce2c5 Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 5 Mar 2018 20:25:32 +0100 Subject: [PATCH 2/8] add plugin class --- classes/plugin.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 classes/plugin.php diff --git a/classes/plugin.php b/classes/plugin.php new file mode 100644 index 0000000..c8e60f4 --- /dev/null +++ b/classes/plugin.php @@ -0,0 +1,41 @@ + acf()->version ) { + self::display_error( __( 'Advanced Custom Fields should be on version 5.6.0 or above.', 'bea-acf-options-for-polylang' ) ); + return false; + }; + + return true; + } + + public static function deactivate() {} + + public static function display_error( $message ) { + printf( '

%s

', $message ); + } +} \ No newline at end of file From 5353d161e3376f78e2d1b83ce05d7358f4c676f8 Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 5 Mar 2018 20:25:38 +0100 Subject: [PATCH 3/8] add singleton --- classes/singleton.php | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 classes/singleton.php diff --git a/classes/singleton.php b/classes/singleton.php new file mode 100644 index 0000000..b287aba --- /dev/null +++ b/classes/singleton.php @@ -0,0 +1,48 @@ +init(); + } + + /** + * Add init function by default + * Implement this method in your child class + * If you want to have actions send at construct + */ + protected function init() {} + + /** + * prevent the instance from being cloned + * + * @return void + */ + final private function __clone() {} + + /** + * prevent from being unserialized + * + * @return void + */ + final private function __wakeup() {} +} \ No newline at end of file From 88e1b8f99132086a773af0e9c03fcadd5df8fd14 Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 5 Mar 2018 20:25:45 +0100 Subject: [PATCH 4/8] add autoload class --- autoload.php | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 autoload.php diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..67e1249 --- /dev/null +++ b/autoload.php @@ -0,0 +1,195 @@ +register(); + * + * // register the base directories for the namespace prefix + * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src'); + * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests'); + * + * The following line would cause the autoloader to attempt to load the + * \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php: + * + * prefixes[ $prefix ] ) === false ) { + $this->prefixes[ $prefix ] = array(); + } + + // retain the base directory for the namespace prefix + if ( $prepend ) { + array_unshift( $this->prefixes[ $prefix ], $base_dir ); + } else { + array_push( $this->prefixes[ $prefix ], $base_dir ); + } + } + + /** + * Loads the class file for a given class name. + * + * @param string $class The fully-qualified class name. + * + * @return mixed The mapped file name on success, or boolean false on + * failure. + */ + public function loadClass( $class ) { + // the current namespace prefix + $prefix = $class; + + // work backwards through the namespace names of the fully-qualified + // class name to find a mapped file name + while ( false !== $pos = strrpos( $prefix, '\\' ) ) { + + // retain the trailing namespace separator in the prefix + $prefix = substr( $class, 0, $pos + 1 ); + + // the rest is the relative class name + $relative_class = substr( $class, $pos + 1 ); + + // try to load a mapped file for the prefix and relative class + $mapped_file = $this->loadMappedFile( $prefix, $relative_class ); + if ( $mapped_file ) { + return $mapped_file; + } + + // remove the trailing namespace separator for the next iteration + // of strrpos() + $prefix = rtrim( $prefix, '\\' ); + } + + // never found a mapped file + return false; + } + + /** + * Load the mapped file for a namespace prefix and relative class. + * + * @param string $prefix The namespace prefix. + * @param string $relative_class The relative class name. + * + * @return mixed Boolean false if no mapped file can be loaded, or the + * name of the mapped file that was loaded. + */ + protected function loadMappedFile( $prefix, $relative_class ) { + // are there any base directories for this namespace prefix? + if ( isset( $this->prefixes[ $prefix ] ) === false ) { + return false; + } + + // look through base directories for this namespace prefix + foreach ( $this->prefixes[ $prefix ] as $base_dir ) { + + // replace the namespace prefix with the base directory, + // replace namespace separators with directory separators + // in the relative class name, append with .php + $file = $base_dir + . strtolower( str_replace( array( '\\', '_' ), array( '/', '-' ), $relative_class ) ) + . '.php'; + + // if the mapped file exists, require it + if ( $this->requireFile( $file ) ) { + // yes, we're done + return $file; + } + } + + // never found it + return false; + } + + /** + * If a file exists, require it from the file system. + * + * @param string $file The file to require. + * + * @return bool True if the file exists, false if not. + */ + protected function requireFile( $file ) { + if ( file_exists( $file ) ) { + require $file; + + return true; + } + + return false; + } +} + +// instantiate the loader +$loader = new \BEA\ACF_Options_For_Polylang\Autoloader; + +// register the autoloader +$loader->register(); + +// register the base directories for the namespace prefix +$loader->addNamespace( 'BEA\ACF_Options_For_Polylang', BEA_ACF_OPTIONS_FOR_POLYLANG_DIR . 'classes' ); From e22a6939cd5d15f23a7e3347d9fafb748a8be6f1 Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 5 Mar 2018 20:25:57 +0100 Subject: [PATCH 5/8] real file for plugin load --- bea-acf-options-for-polylang.php | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 bea-acf-options-for-polylang.php diff --git a/bea-acf-options-for-polylang.php b/bea-acf-options-for-polylang.php new file mode 100644 index 0000000..ea9529f --- /dev/null +++ b/bea-acf-options-for-polylang.php @@ -0,0 +1,57 @@ + Date: Mon, 5 Mar 2018 20:26:51 +0100 Subject: [PATCH 6/8] all the plugin mess --- .../main.php | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) rename acf-options-for-polylang.php => classes/main.php (85%) diff --git a/acf-options-for-polylang.php b/classes/main.php similarity index 85% rename from acf-options-for-polylang.php rename to classes/main.php index fed6b3b..7511534 100644 --- a/acf-options-for-polylang.php +++ b/classes/main.php @@ -1,20 +1,12 @@ - 'options_page', 'operator' => '==', 'value' => 'acf-options', 'id' => 'rule_0', 'group' => 'group_0', ]; + $rule = acf_get_valid_location_rule( $rule ); $options_pages = acf_get_location_rule_values( $rule ); @@ -186,17 +184,12 @@ function set_options_id_lang( $future_post_id, $original_post_id ) { return $future_post_id; } -} - -/** - * Load at plugins loaded to ensure ACF and Polylang are used - * - * @since 1.0.2 - * @author Maxime CULEA - */ -add_action( 'plugins_loaded', function () { - if ( ! function_exists( 'get_field' ) || ! function_exists( 'pll_current_language' ) ) { - return; + + /** + * Load the plugin translation + */ + public function init_translations() { + // Load translations + load_plugin_textdomain( 'bea-acf-options-for-polylang', false, BEA_CPT_AGENT_PLUGIN_DIRNAME . '/languages' ); } - new BEA_ACF_For_Polylang(); -} ); \ No newline at end of file +} \ No newline at end of file From d248ca288159554f5f7d99ea5d75576eaf6bbd9e Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 12 Mar 2018 19:02:03 +0100 Subject: [PATCH 7/8] Update changelog 1.1.0 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5497079..336ece2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.1.0 - Mar 2018 +* True (complet) plugin. +* Add check for ACF 5.6. + ## 1.0.2 - 23 Dec 2017 * Refactor and reformat. * Handle all options page and custom post_id. From 557b0b2322f1e981f82dfec80d2e089b76857a30 Mon Sep 17 00:00:00 2001 From: Maxime Culea Date: Mon, 12 Mar 2018 19:02:25 +0100 Subject: [PATCH 8/8] Check requirements and display error messages when needed --- bea-acf-options-for-polylang.php | 42 +++++++++----------- classes/plugin.php | 41 ------------------- classes/requirements.php | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 64 deletions(-) delete mode 100644 classes/plugin.php create mode 100644 classes/requirements.php diff --git a/bea-acf-options-for-polylang.php b/bea-acf-options-for-polylang.php index ea9529f..e2f441e 100644 --- a/bea-acf-options-for-polylang.php +++ b/bea-acf-options-for-polylang.php @@ -9,7 +9,22 @@ Author URI: https://beapi.fr Contributors: Maxime Culea ---- - Copyright 2017 Be API Technical team (human@beapi.fr) + + Copyright 2018 Be API Technical team (human@beapi.fr) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -28,30 +43,11 @@ define( 'BEA_ACF_OPTIONS_MAIN_FILE_DIR', __FILE__ ); define( 'BEA_ACF_OPTIONS_FOR_POLYLANG_PLUGIN_DIRNAME', basename( rtrim( dirname( __FILE__ ), '/' ) ) ); -// Check PHP min version -if ( version_compare( PHP_VERSION, BEA_CPT_AGENT_MIN_PHP_VERSION, '<' ) ) { - require_once( BEA_CPT_AGENT_DIR . 'compat.php' ); - - // possibly display a notice, trigger error - add_action( 'admin_init', array( 'BEA\CPT_Agent\Compatibility', 'admin_init' ) ); - - // stop execution of this file - return; -} - -/** - * Autoload all the things \o/ - */ +/** Autoload all the things \o/ */ require_once BEA_ACF_OPTIONS_FOR_POLYLANG_DIR . 'autoload.php'; -BEA\ACF_Options_For_Polylang\Plugin::get_instance(); +\BEA\ACF_Options_For_Polylang\Requirements::get_instance(); -/** - * Load at plugins loaded to ensure ACF and Polylang are used - * - * @since 1.0.2 - * @author Maxime CULEA - */ -add_action( 'plugins_loaded', function () { +add_action( 'bea_acf_options_for_polylang_load', function () { \BEA\ACF_Options_For_Polylang\Main::get_instance(); } ); \ No newline at end of file diff --git a/classes/plugin.php b/classes/plugin.php deleted file mode 100644 index c8e60f4..0000000 --- a/classes/plugin.php +++ /dev/null @@ -1,41 +0,0 @@ - acf()->version ) { - self::display_error( __( 'Advanced Custom Fields should be on version 5.6.0 or above.', 'bea-acf-options-for-polylang' ) ); - return false; - }; - - return true; - } - - public static function deactivate() {} - - public static function display_error( $message ) { - printf( '

%s

', $message ); - } -} \ No newline at end of file diff --git a/classes/requirements.php b/classes/requirements.php new file mode 100644 index 0000000..9f88f8f --- /dev/null +++ b/classes/requirements.php @@ -0,0 +1,67 @@ +display_error( sprintf( __( 'Plugin Boilerplate require PHP version %s or greater to be activated. Your server is currently running PHP version %s.', 'bea-acf-options-for-polylang' ), BEA_ACF_OPTIONS_FOR_POLYLANG_MIN_PHP_VERSION, PHP_VERSION ) ); + return false; + } + + if ( ! function_exists( 'acf' ) || ! function_exists( 'pll_current_language' ) ) { + $this->display_error( __( 'Advanced Custom Fields and Polylang are required plugins.', 'bea-acf-options-for-polylang') ); + return false; + } + + if ( '5.6.0' > acf()->version ) { + $this->display_error( __( 'Advanced Custom Fields should be on version 5.6.0 or above.', 'bea-acf-options-for-polylang' ) ); + return false; + }; + + do_action( 'bea_acf_options_for_polylang_load' ); + return true; + } + + // Display message and handle errors + public function display_error( $message ) { + $this->satsify_requiremeents = false; + + trigger_error( $message ); + + add_action( 'admin_notices', function () use ($message) { + printf('

%s

', $message ); + } ); + + // Deactive self + deactivate_plugins( BEA_ACF_OPTIONS_MAIN_FILE_DIR ); + unset( $_GET['activate'] ); + } +} \ No newline at end of file