Skip to content

Commit

Permalink
Add load string from themes and plugins functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
System Administrator committed Oct 22, 2014
1 parent fac7182 commit 5ee85f7
Show file tree
Hide file tree
Showing 14 changed files with 2,098 additions and 22 deletions.
154 changes: 154 additions & 0 deletions admin/admin-filters-strings.php
@@ -0,0 +1,154 @@
<?php
/*
* manages filters and actions related to strings on admin side
*
* @since 1.6 - forked
*/
class PLL_Admin_Filters_Strings {
public $links, $model, $curlang, $current_strings, $new_strings, $settings;

/*
* constructor: setups filters and actions
*
* @since 1.6 - forked
*
* @param object $polylang
*/
public function __construct(&$polylang) {
if (!post_type_exists('polylang_strings'))
register_post_type('polylang_strings', array('rewrite' => false, 'query_var' => false, '_pll' => true));

$this->links = &$polylang->links;
$this->model = &$polylang->model;
$this->curlang = &$polylang->curlang;

// ajax response for edit term form
add_action('wp_ajax_get-strings', array(&$this,'load_strings'));

// add filter to get all the strings
add_filter('pll_get_strings', array(&$this,'add_strings_in_admin_panel'));
}


/*
* ajax response for edit term form
*
* @since 1.6 - forked
*/
public function load_strings($path) {

require_once('get_strings.php');

// check activated theme
$string_manager = new PLL_Get_Strings( $path );

foreach ($string_manager->files as $file) {
$string_manager->analyze_files($file);
}

$this->save_strings($string_manager->strings);

// show ajax response after processing if doing ajax (not useful for the moment)
if ( defined('DOING_AJAX') && DOING_AJAX )
$this->show_ajax_response();
}


/*
* save in custom post type
*
* @since 1.6 - forked
*/
private function save_strings($strings) {
$has_been_added = false;

foreach ($strings as $string) {
$post_string = array(
'post_title' => sanitize_title( $string['string'] ),
'post_content' => $string['domain'],
'post_excerpt' => $string['string'],
'post_status' => 'publish',
'post_type' => 'polylang_strings'
);

$string_exists = get_page_by_title( sanitize_title( $string['string'] ), 'OBJECT', 'polylang_strings' );

if ( $string_exists == null ) {
$insert = wp_insert_post( $post_string );

if ( $insert ) {
// add string in new strings
$this->new_strings[] = $string;
}
}
}
}


/*
* display ajax response
*
* @since 1.6 - forked
*/
public function show_ajax_response() {
echo json_encode($this->new_strings);
}


/*
* get all strings from custom post type
*
* @since 1.6 - forked
*/
private function get_strings() {
$args = array(
'post_type' => 'polylang_strings',
'post_status' => 'publish',
'posts_per_page' => -1
);

$strings = get_posts($args);

foreach ($strings as $string) {
$this->current_strings[] = array(
'name' => $string->post_title,
'string' => $string->post_excerpt,
'domain' => $string->post_content
);
}
}


/*
* register all strings to display in admin panel
*
* @since 1.6 - forked
*/
public function add_strings_in_admin_panel($strings) {
$this->get_strings();

if ( !empty( $this->current_strings ) ) {
foreach ($this->current_strings as $string) {
if (strlen($string['string']) > 60)
$strings[] = array(
'name' => $string['domain'] . chr(4) . $string['name'],
'string' => $string['string'],
'context' => $string['domain'],
'can_be_removed' => true,
'multiline' => true
);
else
$strings[] = array(
'name' => $string['domain'] . chr(4) . $string['name'],
'string' => $string['string'],
'context' => $string['domain'],
'can_be_removed' => true,
'multiline' => false
);
}
}

return $strings;
}

}
21 changes: 20 additions & 1 deletion admin/admin.php
Expand Up @@ -79,6 +79,8 @@ public function init() {
// setup filters for admin pages
if (!PLL_SETTINGS)
add_action('wp_loaded', array(&$this, 'add_filters'));
else
add_action('wp_loaded', array(&$this, 'add_admin_filters'));
}

/*
Expand Down Expand Up @@ -111,6 +113,7 @@ public function admin_enqueue_scripts() {
'post' => array( array('post', 'media', 'async-upload', 'edit'), array('jquery', 'wp-ajax-response', 'inline-edit-post', 'post', 'jquery-ui-autocomplete'), 0 , 0),
'term' => array( array('edit-tags'), array('jquery', 'wp-ajax-response', 'jquery-ui-autocomplete'), 0, 1),
'user' => array( array('profile', 'user-edit'), array('jquery'), 0 , 0),
'get-strings' => array( array('settings_page_mlang'), array('jquery', 'wp-ajax-response'), 0 , 0)
);

foreach ($scripts as $script => $v)
Expand Down Expand Up @@ -234,7 +237,7 @@ public function get_locale($locale) {
*/
public function add_filters() {
// all these are separated just for convenience and maintainability
$classes = array('Filters', 'Filters_Columns', 'Filters_Post', 'Filters_Term', 'Nav_Menu', 'Sync');
$classes = array('Filters', 'Filters_Columns', 'Filters_Post', 'Filters_Term', 'Nav_Menu', 'Sync', 'Filters_Strings');

// don't load media filters if option is disabled or if user has no right
if ($this->options['media_support'] && ($obj = get_post_type_object('attachment')) && (current_user_can($obj->cap->edit_posts) || current_user_can($obj->cap->create_posts)))
Expand All @@ -247,6 +250,22 @@ public function add_filters() {
}
}

/*
* setup filters for admin settings area
*
* @since 1.6 - forked
*/
public function add_admin_filters() {
// all these are separated just for convenience and maintainability
$classes = array('Filters_Strings');

foreach ($classes as $class) {
$obj = strtolower($class);
$class = apply_filters('pll_' . $obj, 'PLL_Admin_' . $class);
$this->$obj = new $class($this);
}
}

/*
* adds the languages list in admin bar for the admin languages filter
*
Expand Down
52 changes: 52 additions & 0 deletions admin/get_strings.php
@@ -0,0 +1,52 @@
<?php

class PLL_Get_Strings {
private $path;
public $files = array();

/*
@$strings
used to return all strings as an array of arrays
line: line of code where it's found
string: string
domain: domain
file: file
*/
public $strings = array();


public function __construct($path_to_analyze) {
// require potx (from drupal)
require_once('potx.inc');

$this->path = $path_to_analyze;

$this->files = pll_get_files_in_path('*.php', $path_to_analyze);
}

public function analyze_files($file) {
$self = $this;
$callback_string = function($string, $domain, $file, $line) use ($self) { return $self->add_string($string, $domain, $file, $line); } ;

_potx_process_file($file, 0, $callback_string,'_potx_save_version', POTX_API_7);
}

public function add_string($string, $domain, $file, $line) {
$this->strings[] = array(
'string' => $string,
'domain' => $domain,
'file' => $file,
'line' => $line
);
}
}

// utils
function pll_get_files_in_path($pattern, $path, $flags = 0) {
$paths = glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
$files = glob($path.$pattern, $flags);
foreach ($paths as $path) {
$files = array_merge($files,pll_get_files_in_path($pattern, $path, $flags));
}
return $files;
}

0 comments on commit 5ee85f7

Please sign in to comment.