first commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\PackageTranslation;
|
||||
|
||||
class Assign {
|
||||
/**
|
||||
* Assign all strings from specified domain to existing package.
|
||||
*
|
||||
* @param string $domainName
|
||||
* @param int $packageId
|
||||
*
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public static function stringsFromDomainToExistingPackage( $domainName, $packageId ) {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->update(
|
||||
$wpdb->prefix . 'icl_strings',
|
||||
[ 'string_package_id' => $packageId ],
|
||||
[ 'context' => $domainName ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign all strings from specified domain to new package which is created on fly.
|
||||
*
|
||||
* @param string $domainName
|
||||
* @param array $packageData {
|
||||
*
|
||||
* @type string $kind_slug e.g. toolset_forms
|
||||
* @type string $kind e.g. "Toolset forms"
|
||||
* @type string $name e.g. "1"
|
||||
* @type string $title e.g. "Form 1"
|
||||
* @type string $edit_link URL to edit page of resource
|
||||
* @type string $view_link (Optional) Url to frontend view page of resource
|
||||
* @type int $page_id (optional)
|
||||
* }
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public static function stringsFromDomainToNewPackage( $domainName, array $packageData ) {
|
||||
$packageId = \WPML_Package_Helper::create_new_package( new \WPML_Package( $packageData ) );
|
||||
if ( $packageId ) {
|
||||
self::stringsFromDomainToExistingPackage( $domainName, $packageId );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\PackageTranslation;
|
||||
|
||||
class Hooks implements \IWPML_Action, \IWPML_Backend_Action, \IWPML_Frontend_Action {
|
||||
|
||||
public function add_hooks() {
|
||||
/**
|
||||
* @see Assign::stringsFromDomainToExistingPackage()
|
||||
*/
|
||||
add_action(
|
||||
'wpml_st_assign_strings_from_domain_to_existing_package',
|
||||
Assign::class . '::stringsFromDomainToExistingPackage',
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
/**
|
||||
* @see Assign::stringsFromDomainToNewPackage()
|
||||
*/
|
||||
add_action(
|
||||
'wpml_st_assign_strings_from_domain_to_new_package',
|
||||
Assign::class . '::stringsFromDomainToNewPackage',
|
||||
10,
|
||||
2
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Package_Cleanup {
|
||||
|
||||
private $existing_strings_in_package = array();
|
||||
|
||||
public function record_existing_strings( WPML_Package $package ) {
|
||||
$strings = $package->get_package_strings();
|
||||
$this->existing_strings_in_package[ $package->ID ] = array();
|
||||
if ( $strings ) {
|
||||
foreach ( $strings as $string ) {
|
||||
$this->existing_strings_in_package[ $package->ID ][ $string->id ] = $string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function record_register_string( WPML_Package $package, $string_id ) {
|
||||
unset( $this->existing_strings_in_package[ $package->ID ][ $string_id ] );
|
||||
}
|
||||
|
||||
public function delete_unused_strings( WPML_Package $package ) {
|
||||
if ( array_key_exists( $package->ID, $this->existing_strings_in_package ) ) {
|
||||
foreach ( $this->existing_strings_in_package[ $package->ID ] as $string_data ) {
|
||||
icl_unregister_string( $package->get_string_context_from_package(), $string_data->name );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: bruce
|
||||
* Date: 16/06/17
|
||||
* Time: 10:57 AM
|
||||
*/
|
||||
class WPML_ST_Package_Storage {
|
||||
|
||||
private $package_id;
|
||||
private $wpdb;
|
||||
|
||||
/**
|
||||
* WPML_ST_Package_Storage constructor.
|
||||
*
|
||||
* @param int $package_id
|
||||
* @param \wpdb $wpdb
|
||||
*/
|
||||
public function __construct( $package_id, wpdb $wpdb ) {
|
||||
$this->package_id = $package_id;
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string_title
|
||||
* @param string $string_type
|
||||
* @param string $string_value
|
||||
* @param int $string_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update( $string_title, $string_type, $string_value, $string_id ) {
|
||||
|
||||
$update_where = array( 'id' => $string_id );
|
||||
|
||||
$update_data = array(
|
||||
'type' => $string_type,
|
||||
'title' => $this->truncate_long_string( $string_title ),
|
||||
);
|
||||
$type_or_title_updated = $this->wpdb->update( $this->wpdb->prefix . 'icl_strings', $update_data, $update_where );
|
||||
|
||||
$update_data = array(
|
||||
'string_package_id' => $this->package_id,
|
||||
'value' => $string_value,
|
||||
);
|
||||
$package_id_or_value_updated = $this->wpdb->update( $this->wpdb->prefix . 'icl_strings', $update_data, $update_where );
|
||||
|
||||
if ( $package_id_or_value_updated ) {
|
||||
$this->set_string_status_to_needs_update_if_translated( $string_id );
|
||||
|
||||
$this->set_translations_to_needs_update();
|
||||
|
||||
}
|
||||
|
||||
return $type_or_title_updated || $package_id_or_value_updated;
|
||||
}
|
||||
|
||||
private function set_string_status_to_needs_update_if_translated( $string_id ) {
|
||||
$this->wpdb->query(
|
||||
$this->wpdb->prepare(
|
||||
"UPDATE {$this->wpdb->prefix}icl_strings
|
||||
SET status=%d
|
||||
WHERE id=%d AND status<>%d",
|
||||
ICL_TM_NEEDS_UPDATE,
|
||||
$string_id,
|
||||
ICL_TM_NOT_TRANSLATED
|
||||
)
|
||||
);
|
||||
$this->wpdb->query(
|
||||
$this->wpdb->prepare(
|
||||
"UPDATE {$this->wpdb->prefix}icl_string_translations
|
||||
SET status=%d
|
||||
WHERE string_id=%d AND status<>%d",
|
||||
ICL_TM_NEEDS_UPDATE,
|
||||
$string_id,
|
||||
ICL_TM_NOT_TRANSLATED
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function set_translations_to_needs_update() {
|
||||
|
||||
$translation_ids = $this->wpdb->get_col(
|
||||
$this->wpdb->prepare(
|
||||
"SELECT translation_id
|
||||
FROM {$this->wpdb->prefix}icl_translations
|
||||
WHERE trid = ( SELECT trid
|
||||
FROM {$this->wpdb->prefix}icl_translations
|
||||
WHERE element_id = %d
|
||||
AND element_type LIKE 'package%%'
|
||||
LIMIT 1 )",
|
||||
$this->package_id
|
||||
)
|
||||
);
|
||||
if ( ! empty( $translation_ids ) ) {
|
||||
$this->wpdb->query(
|
||||
"UPDATE {$this->wpdb->prefix}icl_translation_status
|
||||
SET needs_update = 1
|
||||
WHERE translation_id IN (" . wpml_prepare_in( $translation_ids, '%d' ) . ' ) '
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function truncate_long_string( $string ) {
|
||||
return strlen( $string ) > WPML_STRING_TABLE_NAME_CONTEXT_LENGTH
|
||||
? mb_substr( $string, 0, WPML_STRING_TABLE_NAME_CONTEXT_LENGTH )
|
||||
: $string;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user