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. 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 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' ); diff --git a/bea-acf-options-for-polylang.php b/bea-acf-options-for-polylang.php new file mode 100644 index 0000000..e2f441e --- /dev/null +++ b/bea-acf-options-for-polylang.php @@ -0,0 +1,53 @@ + '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 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 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