Skip to content

Commit 023f6fe

Browse files
committed
Merge branch 'true-plugin'
2 parents 9a77bee + 557b0b2 commit 023f6fe

File tree

7 files changed

+393
-33
lines changed

7 files changed

+393
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.0 - Mar 2018
4+
* True (complet) plugin.
5+
* Add check for ACF 5.6.
6+
37
## 1.0.2 - 23 Dec 2017
48
* Refactor and reformat.
59
* Handle all options page and custom post_id.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ This plugin is storing a value for each language into database. <b>That means at
1515
# Requirements
1616

1717
- [WordPress](https://wordpress.org/) 4.7+
18-
- Tested up to 4.9.1
18+
- Tested up to 4.9.4
1919
- PHP 5.6
20-
- [Advanced Custom Fields](https://www.advancedcustomfields.com/pro)
20+
- [Advanced Custom Fields](https://www.advancedcustomfields.com/pro) 5.6.0+
2121
- [Polylang](https://polylang.pro/)
2222

2323
# Installation

autoload.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php namespace BEA\ACF_Options_For_Polylang;
2+
3+
/**
4+
* An example of a general-purpose implementation that includes the optional
5+
* functionality of allowing multiple base directories for a single namespace
6+
* prefix.
7+
*
8+
* Given a foo-bar package of classes in the file system at the following
9+
* paths ...
10+
*
11+
* /path/to/packages/foo-bar/
12+
* src/
13+
* Baz.php # Foo\Bar\Baz
14+
* Qux/
15+
* Quux.php # Foo\Bar\Qux\Quux
16+
* tests/
17+
* BazTest.php # Foo\Bar\BazTest
18+
* Qux/
19+
* QuuxTest.php # Foo\Bar\Qux\QuuxTest
20+
*
21+
* ... add the path to the class files for the \Foo\Bar\ namespace prefix
22+
* as follows:
23+
*
24+
* <?php
25+
* // instantiate the loader
26+
* $loader = new \Example\Psr4AutoloaderClass;
27+
*
28+
* // register the autoloader
29+
* $loader->register();
30+
*
31+
* // register the base directories for the namespace prefix
32+
* $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
33+
* $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests');
34+
*
35+
* The following line would cause the autoloader to attempt to load the
36+
* \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:
37+
*
38+
* <?php
39+
* new \Foo\Bar\Qux\Quux;
40+
*
41+
* The following line would cause the autoloader to attempt to load the
42+
* \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:
43+
*
44+
* <?php
45+
* new \Foo\Bar\Qux\QuuxTest;
46+
*/
47+
class Autoloader {
48+
/**
49+
* An associative array where the key is a namespace prefix and the value
50+
* is an array of base directories for classes in that namespace.
51+
*
52+
* @var array
53+
*/
54+
protected $prefixes = array();
55+
56+
/**
57+
* Register loader with SPL autoloader stack.
58+
*
59+
* @return void
60+
*/
61+
public function register() {
62+
spl_autoload_register( array( $this, 'loadClass' ) );
63+
}
64+
65+
/**
66+
* Adds a base directory for a namespace prefix.
67+
*
68+
* @param string $prefix The namespace prefix.
69+
* @param string $base_dir A base directory for class files in the
70+
* namespace.
71+
* @param bool $prepend If true, prepend the base directory to the stack
72+
* instead of appending it; this causes it to be searched first rather
73+
* than last.
74+
*
75+
* @return void
76+
*/
77+
public function addNamespace( $prefix, $base_dir, $prepend = false ) {
78+
// normalize namespace prefix
79+
$prefix = trim( $prefix, '\\' ) . '\\';
80+
81+
// normalize the base directory with a trailing separator
82+
$base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . '/';
83+
84+
// initialize the namespace prefix array
85+
if ( isset( $this->prefixes[ $prefix ] ) === false ) {
86+
$this->prefixes[ $prefix ] = array();
87+
}
88+
89+
// retain the base directory for the namespace prefix
90+
if ( $prepend ) {
91+
array_unshift( $this->prefixes[ $prefix ], $base_dir );
92+
} else {
93+
array_push( $this->prefixes[ $prefix ], $base_dir );
94+
}
95+
}
96+
97+
/**
98+
* Loads the class file for a given class name.
99+
*
100+
* @param string $class The fully-qualified class name.
101+
*
102+
* @return mixed The mapped file name on success, or boolean false on
103+
* failure.
104+
*/
105+
public function loadClass( $class ) {
106+
// the current namespace prefix
107+
$prefix = $class;
108+
109+
// work backwards through the namespace names of the fully-qualified
110+
// class name to find a mapped file name
111+
while ( false !== $pos = strrpos( $prefix, '\\' ) ) {
112+
113+
// retain the trailing namespace separator in the prefix
114+
$prefix = substr( $class, 0, $pos + 1 );
115+
116+
// the rest is the relative class name
117+
$relative_class = substr( $class, $pos + 1 );
118+
119+
// try to load a mapped file for the prefix and relative class
120+
$mapped_file = $this->loadMappedFile( $prefix, $relative_class );
121+
if ( $mapped_file ) {
122+
return $mapped_file;
123+
}
124+
125+
// remove the trailing namespace separator for the next iteration
126+
// of strrpos()
127+
$prefix = rtrim( $prefix, '\\' );
128+
}
129+
130+
// never found a mapped file
131+
return false;
132+
}
133+
134+
/**
135+
* Load the mapped file for a namespace prefix and relative class.
136+
*
137+
* @param string $prefix The namespace prefix.
138+
* @param string $relative_class The relative class name.
139+
*
140+
* @return mixed Boolean false if no mapped file can be loaded, or the
141+
* name of the mapped file that was loaded.
142+
*/
143+
protected function loadMappedFile( $prefix, $relative_class ) {
144+
// are there any base directories for this namespace prefix?
145+
if ( isset( $this->prefixes[ $prefix ] ) === false ) {
146+
return false;
147+
}
148+
149+
// look through base directories for this namespace prefix
150+
foreach ( $this->prefixes[ $prefix ] as $base_dir ) {
151+
152+
// replace the namespace prefix with the base directory,
153+
// replace namespace separators with directory separators
154+
// in the relative class name, append with .php
155+
$file = $base_dir
156+
. strtolower( str_replace( array( '\\', '_' ), array( '/', '-' ), $relative_class ) )
157+
. '.php';
158+
159+
// if the mapped file exists, require it
160+
if ( $this->requireFile( $file ) ) {
161+
// yes, we're done
162+
return $file;
163+
}
164+
}
165+
166+
// never found it
167+
return false;
168+
}
169+
170+
/**
171+
* If a file exists, require it from the file system.
172+
*
173+
* @param string $file The file to require.
174+
*
175+
* @return bool True if the file exists, false if not.
176+
*/
177+
protected function requireFile( $file ) {
178+
if ( file_exists( $file ) ) {
179+
require $file;
180+
181+
return true;
182+
}
183+
184+
return false;
185+
}
186+
}
187+
188+
// instantiate the loader
189+
$loader = new \BEA\ACF_Options_For_Polylang\Autoloader;
190+
191+
// register the autoloader
192+
$loader->register();
193+
194+
// register the base directories for the namespace prefix
195+
$loader->addNamespace( 'BEA\ACF_Options_For_Polylang', BEA_ACF_OPTIONS_FOR_POLYLANG_DIR . 'classes' );

bea-acf-options-for-polylang.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
Plugin Name: BEA - ACF Options for Polylang
5+
Version: 1.1.0
6+
Plugin URI: https://github.com/BeAPI/acf-options-for-polylang
7+
Description: Add ACF options page support for Polylang.
8+
Author: Be API Technical team
9+
Author URI: https://beapi.fr
10+
Contributors: Maxime Culea
11+
----
12+
13+
Copyright 2018 Be API Technical team ([email protected])
14+
15+
This program is free software; you can redistribute it and/or modify
16+
it under the terms of the GNU General Public License as published by
17+
the Free Software Foundation; either version 2 of the License, or
18+
(at your option) any later version.
19+
20+
This program is distributed in the hope that it will be useful,
21+
but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
GNU General Public License for more details.
24+
25+
You should have received a copy of the GNU General Public License
26+
along with this program; if not, write to the Free Software
27+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28+
*/
29+
30+
31+
// don't load directly
32+
if ( ! defined( 'ABSPATH' ) ) {
33+
die( '-1' );
34+
}
35+
36+
// Plugin constants
37+
define( 'BEA_ACF_OPTIONS_FOR_POLYLANG_VERSION', '1.1.0' );
38+
define( 'BEA_ACF_OPTIONS_FOR_POLYLANG_MIN_PHP_VERSION', '5.6' );
39+
40+
// Plugin URL and PATH
41+
define( 'BEA_ACF_OPTIONS_FOR_POLYLANG_URL', plugin_dir_url( __FILE__ ) );
42+
define( 'BEA_ACF_OPTIONS_FOR_POLYLANG_DIR', plugin_dir_path( __FILE__ ) );
43+
define( 'BEA_ACF_OPTIONS_MAIN_FILE_DIR', __FILE__ );
44+
define( 'BEA_ACF_OPTIONS_FOR_POLYLANG_PLUGIN_DIRNAME', basename( rtrim( dirname( __FILE__ ), '/' ) ) );
45+
46+
/** Autoload all the things \o/ */
47+
require_once BEA_ACF_OPTIONS_FOR_POLYLANG_DIR . 'autoload.php';
48+
49+
\BEA\ACF_Options_For_Polylang\Requirements::get_instance();
50+
51+
add_action( 'bea_acf_options_for_polylang_load', function () {
52+
\BEA\ACF_Options_For_Polylang\Main::get_instance();
53+
} );
Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
<?php
2-
3-
/*
4-
Plugin Name: BEA - ACF Options for Polylang
5-
Version: 1.0.2
6-
Plugin URI: https://github.com/BeAPI/acf-options-for-polylang
7-
Description: Add ACF options page support for Polylang.
8-
Author: Be API Technical team
9-
Author URI: https://beapi.fr
10-
Contributors: Maxime Culea
11-
----
12-
Copyright 2017 Be API Technical team ([email protected])
13-
*/
14-
15-
class BEA_ACF_For_Polylang {
16-
17-
function __construct() {
1+
<?php namespace BEA\ACF_Options_For_Polylang;
2+
3+
class Main {
4+
/**
5+
* Use the trait
6+
*/
7+
use Singleton;
8+
9+
protected function init() {
1810
// Set the setting's lang
1911
add_filter( 'acf/validate_post_id', [ $this, 'set_options_id_lang' ], 10, 2 );
2012

@@ -23,6 +15,8 @@ function __construct() {
2315

2416
// Get default Polylang's option page value
2517
add_filter( 'acf/load_value', [ $this, 'get_default_value' ], 10, 3 );
18+
19+
add_action( 'init', [ $this, 'init_translations' ] );
2620
}
2721

2822
/**
@@ -124,13 +118,17 @@ public function get_default_value( $value, $post_id, $field ) {
124118
* @return array
125119
*/
126120
function get_option_page_ids() {
127-
$rule = [
121+
if ( ! function_exists( 'acf_get_valid_location_rule' ) ) {
122+
return [];
123+
}
124+
$rule = [
128125
'param' => 'options_page',
129126
'operator' => '==',
130127
'value' => 'acf-options',
131128
'id' => 'rule_0',
132129
'group' => 'group_0',
133130
];
131+
134132
$rule = acf_get_valid_location_rule( $rule );
135133
$options_pages = acf_get_location_rule_values( $rule );
136134

@@ -186,17 +184,12 @@ function set_options_id_lang( $future_post_id, $original_post_id ) {
186184

187185
return $future_post_id;
188186
}
189-
}
190-
191-
/**
192-
* Load at plugins loaded to ensure ACF and Polylang are used
193-
*
194-
* @since 1.0.2
195-
* @author Maxime CULEA
196-
*/
197-
add_action( 'plugins_loaded', function () {
198-
if ( ! function_exists( 'get_field' ) || ! function_exists( 'pll_current_language' ) ) {
199-
return;
187+
188+
/**
189+
* Load the plugin translation
190+
*/
191+
public function init_translations() {
192+
// Load translations
193+
load_plugin_textdomain( 'bea-acf-options-for-polylang', false, BEA_CPT_AGENT_PLUGIN_DIRNAME . '/languages' );
200194
}
201-
new BEA_ACF_For_Polylang();
202-
} );
195+
}

0 commit comments

Comments
 (0)