Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
Wpct Plugin Abstracts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
codeccoop
WordPress
plugins
Wpct Plugin Abstracts
Compare revisions
79d1cee97887107a8bb3b6f1026ce4887f84f2c0 to d8ab19a1aab5d26e63041d3e24c8e52a3254e5e7
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
codeccoop/wp/plugins/wpct-plugin-abstracts
Select target project
No results found
d8ab19a1aab5d26e63041d3e24c8e52a3254e5e7
Select Git revision
Branches
main
release/forms-bridge
Swap
Target
codeccoop/wp/plugins/wpct-plugin-abstracts
Select target project
codeccoop/wp/plugins/wpct-plugin-abstracts
1 result
79d1cee97887107a8bb3b6f1026ce4887f84f2c0
Select Git revision
Branches
main
release/forms-bridge
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (1)
feat: conditional declaration and is_plugin_active hook
· d8ab19a1
Lucas García
authored
8 months ago
d8ab19a1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
class-field.php
+0
-7
0 additions, 7 deletions
class-field.php
class-plugin.php
+104
-74
104 additions, 74 deletions
class-plugin.php
class-settings.php
+174
-166
174 additions, 166 deletions
class-settings.php
class-singleton.php
+24
-20
24 additions, 20 deletions
class-singleton.php
with
302 additions
and
267 deletions
class-field.php
deleted
100755 → 0
View file @
79d1cee9
<?php
namespace
WPCT_ERP_FORMS\Abstract
;
abstract
class
Field
extends
Singleton
{
}
This diff is collapsed.
Click to expand it.
class-plugin.php
View file @
d8ab19a1
...
...
@@ -2,101 +2,131 @@
namespace
WPCT_ABSTRACT
;
use
WPCT_HTTP
\Menu
as
Menu
;
use
WPCT_HTTP
\Settings
as
Settings
;
use
Exception
;
abstract
class
Plugin
extends
Singleton
{
protected
static
$menu
;
protected
static
$textdomain
;
protected
static
$name
;
protected
static
$index
;
if
(
!
class_exists
(
'Plugin'
))
:
abstract
public
function
init
();
abstract
class
Plugin
extends
Singleton
{
protected
static
$menu
;
protected
static
$textdomain
;
protected
static
$name
;
protected
static
$index
;
abstract
public
static
function
activate
();
abstract
public
function
init
();
abstract
public
static
function
de
activate
();
abstract
public
static
function
activate
();
public
function
__construct
()
{
if
(
empty
(
$this
->
name
)
||
empty
(
$this
->
textdomain
))
{
throw
new
Exception
(
'Bad plugin initialization'
);
}
abstract
public
static
function
deactivate
();
$this
->
menu
=
self
::
$menu
::
get_instance
(
self
::
$name
);
public
function
__construct
()
{
if
(
empty
(
$this
->
name
)
||
empty
(
$this
->
textdomain
))
{
throw
new
Exception
(
'Bad plugin initialization'
);
}
add_action
(
'init'
,
[
$this
,
'init'
],
10
);
add_action
(
'init'
,
function
()
{
$this
->
load_textdomain
();
},
5
);
$this
->
menu
=
self
::
$menu
::
get_instance
(
self
::
$name
);
add_filter
(
'load_textdomain_mofile'
,
function
(
$mofile
,
$domain
)
{
return
$this
->
load_mofile
(
$mofile
,
$domain
);
},
10
,
2
);
}
add_action
(
'init'
,
[
$this
,
'init'
],
10
);
add_action
(
'init'
,
function
()
{
$this
->
load_textdomain
(
);
},
5
);
public
function
get_menu
()
{
return
$this
->
menu
;
}
add_filter
(
'load_textdomain_mofile'
,
function
(
$mofile
,
$domain
)
{
return
$this
->
load_mofile
(
$mofile
,
$domain
);
},
10
,
2
);
public
function
get
_name
(
)
{
return
$this
->
name
;
}
add_filter
(
'wpct_is_plugin_active'
,
function
(
$_
,
$plugin
_name
)
{
return
self
::
is_active
(
$plugin_name
);
},
1
)
;
}
public
function
get_index
()
{
$index
=
self
::
$index
;
if
(
empty
(
$index
))
{
$index
=
sanitize_title
(
self
::
$name
);
public
function
get_menu
()
{
return
$this
->
menu
;
}
return
plugin_basename
(
dirname
(
__FILE__
,
2
)
.
'/'
.
$index
);
}
public
function
get_name
()
{
return
$this
->
name
;
}
public
function
get_textdomain
()
{
return
$this
->
textdomain
;
}
public
function
get_index
()
{
$index
=
self
::
$index
;
if
(
empty
(
$index
))
{
$index
=
sanitize_title
(
self
::
$name
);
}
public
function
get_data
()
{
include_once
(
ABSPATH
.
'wp-admin/includes/plugin.php'
);
$plugins
=
get_plugins
();
$plugin_name
=
$this
->
get_index
();
foreach
(
$plugins
as
$plugin
=>
$data
)
{
if
(
$plugin
===
$plugin_name
)
{
return
$data
;
return
plugin_basename
(
dirname
(
__FILE__
,
2
)
.
'/'
.
$index
);
}
public
function
get_textdomain
()
{
return
$this
->
textdomain
;
}
public
function
get_data
()
{
include_once
(
ABSPATH
.
'wp-admin/includes/plugin.php'
);
$plugins
=
get_plugins
();
$plugin_name
=
$this
->
get_index
();
foreach
(
$plugins
as
$plugin
=>
$data
)
{
if
(
$plugin
===
$plugin_name
)
{
return
$data
;
}
}
}
}
private
function
load_textdomain
()
{
$data
=
$this
->
get_data
();
$domain_path
=
isset
(
$data
[
'DomainPath'
])
&&
!
empty
(
$data
[
'DomainPath'
])
?
$data
[
'DomainPath'
]
:
'/languages'
;
load_plugin_textdomain
(
$this
->
textdomain
,
false
,
dirname
(
$this
->
index
)
.
$domain_path
,
);
}
private
function
load_textdomain
()
{
$data
=
$this
->
get_data
();
$domain_path
=
isset
(
$data
[
'DomainPath'
])
&&
!
empty
(
$data
[
'DomainPath'
])
?
$data
[
'DomainPath'
]
:
'/languages'
;
private
function
load_mofile
(
$mofile
,
$domain
)
{
$data
=
$this
->
get_data
();
$domain_path
=
isset
(
$data
[
'DomainPath'
])
&&
!
empty
(
$data
[
'DomainPath'
])
?
$data
[
'DomainPath'
]
:
'/languages'
;
load_plugin_textdomain
(
$this
->
textdomain
,
false
,
dirname
(
$this
->
index
)
.
$domain_path
,
);
}
if
(
$domain
===
$this
->
textdomain
&&
strpos
(
$mofile
,
WP_LANG_DIR
.
'/plugins/'
)
===
false
)
{
$locale
=
apply_filters
(
'plugin_locale'
,
determine_locale
(),
$domain
);
$mofile
=
dirname
(
$this
->
index
)
.
$domain_path
.
'/'
.
$domain
.
'-'
.
$locale
.
'.mo'
;
private
function
load_mofile
(
$mofile
,
$domain
)
{
$data
=
$this
->
get_data
();
$domain_path
=
isset
(
$data
[
'DomainPath'
])
&&
!
empty
(
$data
[
'DomainPath'
])
?
$data
[
'DomainPath'
]
:
'/languages'
;
if
(
$domain
===
$this
->
textdomain
&&
strpos
(
$mofile
,
WP_LANG_DIR
.
'/plugins/'
)
===
false
)
{
$locale
=
apply_filters
(
'plugin_locale'
,
determine_locale
(),
$domain
);
$mofile
=
dirname
(
$this
->
index
)
.
$domain_path
.
'/'
.
$domain
.
'-'
.
$locale
.
'.mo'
;
}
return
$mofile
;
}
return
$mofile
;
private
function
is_active
(
$plugin_name
)
{
include_once
(
ABSPATH
.
'wp-admin/includes/plugin.php'
);
$plugins
=
get_plugins
();
if
(
is_multisite
())
{
$actives
=
apply_filters
(
'active_plugins'
,
array_map
(
function
(
$plugin_path
)
{
return
plugin_basename
(
$plugin_path
);
},
wp_get_active_network_plugins
()));
}
else
{
$actives
=
apply_filters
(
'active_plugins'
,
get_option
(
'active_plugins'
));
}
$actives
=
array_reduce
(
array_keys
(
$plugins
),
function
(
$carry
,
$plugin_path
)
use
(
$plugins
,
$actives
)
{
if
(
in_array
(
$plugin_path
,
$actives
))
{
$carry
[
$plugin_path
]
=
$plugins
[
$plugin_path
];
}
return
$carry
;
},
[]);
$plugin_name
=
plugin_basename
(
$plugin_name
);
return
in_array
(
$plugin_name
,
array_keys
(
$actives
));
}
}
}
endif
;
This diff is collapsed.
Click to expand it.
class-settings.php
View file @
d8ab19a1
...
...
@@ -2,211 +2,219 @@
namespace
WPCT_ABSTRACT
;
class
Undefined
{
};
if
(
!
class_exists
(
'Settings'
))
:
abstract
class
Settings
extends
Singleton
{
protected
$group_name
;
protected
$_default
=
[];
abstract
public
function
register
();
public
function
__construct
(
$textdomain
)
class
Undefined
{
$this
->
group_name
=
$textdomain
;
}
};
public
function
get_name
()
abstract
class
Settings
extends
Singleton
{
return
$this
->
group_name
;
}
protected
$
group_name
;
protected
$_default
=
[];
public
function
register_setting
(
$name
)
{
$default
=
$this
->
get_default
(
$name
);
$default
=
$this
->
get_default
(
$name
,
$default
);
register_setting
(
$this
->
group_name
,
$name
,
[
'type'
=>
'array'
,
'show_in_rest'
=>
false
,
'default'
=>
$default
,
],
);
add_settings_section
(
$name
.
'_section'
,
__
(
$name
.
'--title'
,
'wpct'
),
function
()
use
(
$name
)
{
$title
=
__
(
$name
.
'--description'
,
'wpct'
);
echo
"<p>
{
$title
}
</p>"
;
},
$this
->
group_name
,
);
$this
->
_default
[
$name
]
=
$default
;
foreach
(
array_keys
(
$default
)
as
$field
)
{
$this
->
register_field
(
$field
,
$name
);
}
}
abstract
public
function
register
();
public
function
register_field
(
$field_name
,
$setting_name
)
{
$field_id
=
$setting_name
.
'__'
.
$field_name
;
add_settings_field
(
$field_name
,
__
(
$field_id
.
'--label'
,
'wpct'
),
function
()
use
(
$setting_name
,
$field_name
)
{
echo
$this
->
field_render
(
$setting_name
,
$field_name
);
},
$this
->
group_name
,
$setting_name
.
'_section'
,
[
'class'
=>
$field_id
,
]
);
}
public
function
__construct
(
$textdomain
)
{
$this
->
group_name
=
$textdomain
;
}
public
function
field_render
()
{
$args
=
func_get_args
();
$setting
=
$args
[
0
];
$field
=
$args
[
1
];
if
(
count
(
$args
)
>=
3
)
{
$value
=
$args
[
2
];
}
else
{
$value
=
new
Undefined
();
public
function
get_name
()
{
return
$this
->
group_name
;
}
return
$this
->
_field_render
(
$setting
,
$field
,
$value
);
}
public
function
register_setting
(
$name
)
{
$default
=
$this
->
get_default
(
$name
);
$default
=
$this
->
get_default
(
$name
,
$default
);
register_setting
(
$this
->
group_name
,
$name
,
[
'type'
=>
'array'
,
'show_in_rest'
=>
false
,
'default'
=>
$default
,
],
);
add_settings_section
(
$name
.
'_section'
,
__
(
$name
.
'--title'
,
'wpct'
),
function
()
use
(
$name
)
{
$title
=
__
(
$name
.
'--description'
,
'wpct'
);
echo
"<p>
{
$title
}
</p>"
;
},
$this
->
group_name
,
);
$this
->
_default
[
$name
]
=
$default
;
foreach
(
array_keys
(
$default
)
as
$field
)
{
$this
->
register_field
(
$field
,
$name
);
}
}
private
function
_field_render
(
$setting
,
$field
,
$value
)
{
$is_root
=
false
;
if
(
$value
instanceof
Undefined
)
{
$value
=
$this
->
option_getter
(
$setting
,
$field
);
$is_root
=
true
;
public
function
register_field
(
$field_name
,
$setting_name
)
{
$field_id
=
$setting_name
.
'__'
.
$field_name
;
add_settings_field
(
$field_name
,
__
(
$field_id
.
'--label'
,
'wpct'
),
function
()
use
(
$setting_name
,
$field_name
)
{
echo
$this
->
field_render
(
$setting_name
,
$field_name
);
},
$this
->
group_name
,
$setting_name
.
'_section'
,
[
'class'
=>
$field_id
,
]
);
}
if
(
!
is_array
(
$value
))
{
return
$this
->
input_render
(
$setting
,
$field
,
$value
);
}
else
{
$fieldset
=
$this
->
fieldset_render
(
$setting
,
$field
,
$value
);
if
(
$is_root
)
{
$fieldset
=
$this
->
control_style
(
$setting
,
$field
)
.
$fieldset
.
$this
->
control_render
(
$setting
,
$field
);
public
function
field_render
()
{
$args
=
func_get_args
();
$setting
=
$args
[
0
];
$field
=
$args
[
1
];
if
(
count
(
$args
)
>=
3
)
{
$value
=
$args
[
2
];
}
else
{
$value
=
new
Undefined
();
}
return
$
fieldset
;
return
$
this
->
_field_render
(
$setting
,
$field
,
$value
)
;
}
}
public
function
input_render
(
$setting
,
$field
,
$value
)
{
$default_value
=
$this
->
get_default
(
$setting
);
$keys
=
explode
(
']['
,
$field
);
$is_list
=
is_list
(
$default_value
);
for
(
$i
=
0
;
$i
<
count
(
$keys
);
$i
++
)
{
$key
=
$keys
[
$i
];
if
(
$is_list
)
{
$key
=
(
int
)
$key
;
private
function
_field_render
(
$setting
,
$field
,
$value
)
{
$is_root
=
false
;
if
(
$value
instanceof
Undefined
)
{
$value
=
$this
->
option_getter
(
$setting
,
$field
);
$is_root
=
true
;
}
$default_value
=
isset
(
$default_value
[
$key
])
?
$default_value
[
$key
]
:
$default_value
[
0
];
$is_list
=
is_list
(
$default_value
);
}
$is_bool
=
is_bool
(
$default_value
);
if
(
$is_bool
)
{
$is_bool
=
true
;
$value
=
'on'
===
$value
;
}
if
(
$is_bool
)
{
return
"<input type='checkbox' name='
{
$setting
}
[
$field
]' "
.
(
$value
?
'checked'
:
''
)
.
" />"
;
}
else
{
return
"<input type='text' name='
{
$setting
}
[
{
$field
}
]' value='
{
$value
}
' />"
;
if
(
!
is_array
(
$value
))
{
return
$this
->
input_render
(
$setting
,
$field
,
$value
);
}
else
{
$fieldset
=
$this
->
fieldset_render
(
$setting
,
$field
,
$value
);
if
(
$is_root
)
{
$fieldset
=
$this
->
control_style
(
$setting
,
$field
)
.
$fieldset
.
$this
->
control_render
(
$setting
,
$field
);
}
return
$fieldset
;
}
}
}
public
function
fieldset_render
(
$setting
,
$field
,
$data
)
{
$table_id
=
$setting
.
'__'
.
str_replace
(
']['
,
'_'
,
$field
);
$fieldset
=
"<table id='
{
$table_id
}
'>"
;
$is_list
=
is_list
(
$data
);
foreach
(
array_keys
(
$data
)
as
$key
)
{
$fieldset
.
=
'<tr>'
;
if
(
!
$is_list
)
{
$fieldset
.
=
"<th>
{
$key
}
</th>"
;
public
function
input_render
(
$setting
,
$field
,
$value
)
{
$default_value
=
$this
->
get_default
(
$setting
);
$keys
=
explode
(
']['
,
$field
);
$is_list
=
is_list
(
$default_value
);
for
(
$i
=
0
;
$i
<
count
(
$keys
);
$i
++
)
{
$key
=
$keys
[
$i
];
if
(
$is_list
)
{
$key
=
(
int
)
$key
;
}
$default_value
=
isset
(
$default_value
[
$key
])
?
$default_value
[
$key
]
:
$default_value
[
0
];
$is_list
=
is_list
(
$default_value
);
}
$is_bool
=
is_bool
(
$default_value
);
if
(
$is_bool
)
{
$is_bool
=
true
;
$value
=
'on'
===
$value
;
}
if
(
$is_bool
)
{
return
"<input type='checkbox' name='
{
$setting
}
[
$field
]' "
.
(
$value
?
'checked'
:
''
)
.
" />"
;
}
else
{
return
"<input type='text' name='
{
$setting
}
[
{
$field
}
]' value='
{
$value
}
' />"
;
}
$_field
=
$field
.
']['
.
$key
;
$fieldset
.
=
"<td>
{
$this
->
field_render
(
$setting
,
$_field
,
$data
[
$key
])
}
</td>"
;
$fieldset
.
=
'</tr>'
;
}
$fieldset
.
=
'</table>'
;
return
$fieldset
;
}
public
function
fieldset_render
(
$setting
,
$field
,
$data
)
{
$table_id
=
$setting
.
'__'
.
str_replace
(
']['
,
'_'
,
$field
);
$fieldset
=
"<table id='
{
$table_id
}
'>"
;
$is_list
=
is_list
(
$data
);
foreach
(
array_keys
(
$data
)
as
$key
)
{
$fieldset
.
=
'<tr>'
;
if
(
!
$is_list
)
{
$fieldset
.
=
"<th>
{
$key
}
</th>"
;
}
$_field
=
$field
.
']['
.
$key
;
$fieldset
.
=
"<td>
{
$this
->
field_render
(
$setting
,
$_field
,
$data
[
$key
])
}
</td>"
;
$fieldset
.
=
'</tr>'
;
}
$fieldset
.
=
'</table>'
;
public
function
control_render
(
$setting
,
$field
)
{
$default
=
$this
->
get_default
(
$setting
);
ob_start
();
?>
return
$fieldset
;
}
public
function
control_render
(
$setting
,
$field
)
{
$default
=
$this
->
get_default
(
$setting
);
ob_start
();
?>
<div
class=
"
<?=
$setting
;
?>
__
<?=
$field
?>
--controls"
>
<button
class=
"button button-primary"
data-action=
"add"
>
Add
</button>
<button
class=
"button button-secondary"
data-action=
"remove"
>
Remove
</button>
</div>
<?php
include
'fieldset-control-js.php'
?>
<?php
return
ob_get_clean
();
}
return
ob_get_clean
();
}
public
function
control_style
(
$setting
,
$field
)
{
return
"<style>#
{
$setting
}
__
{
$field
}
td td,#
{
$setting
}
__
{
$field
}
td th
{
padding:0}#{$setting}__{$field
}
table table
{
margin-bottom:1rem
}
</style>"
;
}
public
function
control_style
(
$setting
,
$field
)
{
return
"<style>#
{
$setting
}
__
{
$field
}
td td,#
{
$setting
}
__
{
$field
}
td th
{
padding:0}#{$setting}__{$field
}
table table
{
margin-bottom:1rem
}
</style>"
;
}
public
function
option_getter
(
$setting
,
$option
)
{
$default
=
$this
->
get_default
(
$setting
,
$option
);
$setting
=
get_option
(
$setting
)
?:
[];
if
(
!
key_exists
(
$option
,
$setting
))
{
return
null
;
public
function
option_getter
(
$setting
,
$option
)
{
$default
=
$this
->
get_default
(
$setting
,
$option
);
$setting
=
get_option
(
$setting
)
?:
[];
if
(
!
key_exists
(
$option
,
$setting
))
{
return
null
;
}
if
(
empty
(
$setting
[
$option
]))
{
return
$default
;
}
elseif
(
is_list
(
$setting
[
$option
]))
{
// $setting[$option] = array_map($setting[$option]);
}
return
$setting
[
$option
];
}
if
(
empty
(
$setting
[
$option
]))
{
public
function
get_default
(
$setting_name
,
$field
=
null
)
{
$default
=
usset
(
$this
->
_default
[
$setting_name
])
?:
[];
$default
=
apply_filters
(
$setting_name
.
'_default'
,
$default
);
if
(
$field
&&
isset
(
$default
[
$field
]))
{
return
$default
[
$field
];
}
return
$default
;
}
elseif
(
is_list
(
$setting
[
$option
]))
{
// $setting[$option] = array_map($setting[$option]);
}
return
$setting
[
$option
];
}
public
function
get_default
(
$setting_name
,
$field
=
null
)
{
$default
=
usset
(
$this
->
_default
[
$setting_name
])
?:
[];
$default
=
apply_filters
(
$setting_name
.
'_default'
,
$default
);
endif
;
if
(
$field
&&
isset
(
$default
[
$field
]))
{
return
$default
[
$field
];
}
if
(
!
function_exists
(
'is_list'
))
:
return
$default
;
function
is_list
(
$arr
)
{
if
(
!
is_array
(
$arr
))
{
return
false
;
}
if
(
sizeof
(
$arr
)
===
0
)
{
return
true
;
}
return
array_keys
(
$arr
)
===
range
(
0
,
count
(
$arr
)
-
1
);
}
}
function
is_list
(
$arr
)
{
if
(
!
is_array
(
$arr
))
{
return
false
;
}
if
(
sizeof
(
$arr
)
===
0
)
{
return
true
;
}
return
array_keys
(
$arr
)
===
range
(
0
,
count
(
$arr
)
-
1
);
}
endif
;
This diff is collapsed.
Click to expand it.
class-singleton.php
View file @
d8ab19a1
...
...
@@ -4,31 +4,35 @@ namespace WPCT_ABSTRACT;
use
Exception
;
abstract
class
Singleton
{
private
static
$_instances
=
[];
if
(
!
class_exists
(
'Singleton'
))
:
protected
function
__construct
()
abstract
class
Singleton
{
}
private
static
$_instances
=
[];
protected
function
__c
l
on
e
()
{
}
protected
function
__con
struct
()
{
}
public
function
__wakeup
()
{
throw
new
Exception
(
'Cannot unserialize a singleton.'
);
}
protected
function
__clone
()
{
}
public
static
function
get_instance
()
{
$args
=
func_get_args
();
$cls
=
static
::
class
;
if
(
!
isset
(
self
::
$_instances
[
$cls
]))
{
self
::
$_instances
[
$cls
]
=
new
static
(
...
$args
);
public
function
__wakeup
()
{
throw
new
Exception
(
'Cannot unserialize a singleton.'
);
}
return
self
::
$_instances
[
$cls
];
public
static
function
get_instance
()
{
$args
=
func_get_args
();
$cls
=
static
::
class
;
if
(
!
isset
(
self
::
$_instances
[
$cls
]))
{
self
::
$_instances
[
$cls
]
=
new
static
(
...
$args
);
}
return
self
::
$_instances
[
$cls
];
}
}
}
endif
;
This diff is collapsed.
Click to expand it.