first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
<?php
class WPML_TM_Word_Count_Admin_Hooks implements IWPML_Action {
/** @var WPML_WP_API $wp_api */
private $wp_api;
public function __construct( WPML_WP_API $wp_api ) {
$this->wp_api = $wp_api;
}
public function add_hooks() {
if ( $this->wp_api->is_dashboard_tab() ) {
add_action( 'wpml_tm_dashboard_word_count_estimation', array( $this, 'display_dialog_open_link' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
add_filter( 'wpml_words_count_url', array( $this, 'words_count_url_filter' ) );
}
public function enqueue_scripts() {
wp_enqueue_script(
'word-count-report',
WPML_TM_URL . '/dist/js/word-count/app.js',
array( 'jquery-ui-dialog' ),
WPML_TM_VERSION
);
}
/**
* @param string $default_url
*
* @return string
*/
public function words_count_url_filter( $default_url ) {
return $this->wp_api->get_tm_url( 'dashboard', '#words-count' );
}
public function display_dialog_open_link() {
echo '<a href="#" class="js-word-count-dialog-open"
data-nonce="' . wp_create_nonce( WPML_TM_Word_Count_Hooks_Factory::NONCE_ACTION ) . '"
data-dialog-title="' . esc_attr__( 'Word count estimation', 'wpml-translation-management' ) . '"
data-cancel="' . esc_attr__( 'Cancel', 'wpml-translation-management' ) . '"
data-loading-text="' . esc_attr__( 'Initializing...', 'wpml-translation-management' ) . '">'
. esc_html__( 'Word count for the entire site', 'wpml-translation-management' ) . '</a>';
}
}

View File

@@ -0,0 +1,64 @@
<?php
class WPML_TM_Word_Count_Ajax_Hooks implements IWPML_Action {
/** @var WPML_TM_Word_Count_Report $report */
private $report;
/** @var WPML_TM_Word_Count_Background_Process_Factory $process_factory*/
private $process_factory;
/** @var bool $requested_types_status */
private $requested_types_status;
public function __construct(
WPML_TM_Word_Count_Report $report,
WPML_TM_Word_Count_Background_Process_Factory $process_factory,
$requested_types_status
) {
$this->report = $report;
$this->process_factory = $process_factory;
$this->requested_types_status = $requested_types_status;
}
public function add_hooks() {
add_action( 'wp_ajax_wpml_word_count_get_report', array( $this, 'get_report' ) );
add_action( 'wp_ajax_wpml_word_count_start_count', array( $this, 'start_count' ) );
add_action( 'wp_ajax_wpml_word_count_cancel_count', array( $this, 'cancel_count' ) );
}
public function __call( $method_name, $arguments ) {
check_admin_referer( WPML_TM_Word_Count_Hooks_Factory::NONCE_ACTION, 'nonce' );
$this->{$method_name}();
wp_send_json_success();
}
private function get_report() {
$data = array(
'status' => $this->requested_types_status,
'report' => $this->report->render(),
);
wp_send_json_success( $data );
}
private function start_count() {
$requested_types = array();
foreach ( array( 'post_types', 'package_kinds' ) as $group ) {
$requested_types[ $group ] = isset( $_POST['requested_types'][ $group ] )
? filter_var_array( $_POST['requested_types'][ $group ] ) : array();
}
$requested_types_process = $this->process_factory->create_requested_types();
$this->report->set_requested_types( $requested_types );
$requested_types_process->init( $requested_types );
}
private function cancel_count() {
update_option(
WPML_TM_Word_Count_Hooks_Factory::OPTION_KEY_REQUESTED_TYPES_STATUS,
WPML_TM_Word_Count_Hooks_Factory::PROCESS_COMPLETED
);
}
}

View File

@@ -0,0 +1,122 @@
<?php
class WPML_TM_Word_Count_Hooks_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
const PROCESS_PENDING = 'pending';
const PROCESS_IN_PROGRESS = 'in-progress';
const PROCESS_COMPLETED = 'completed';
const OPTION_KEY_REQUESTED_TYPES_STATUS = 'wpml_word_count_requested_types_status';
const NONCE_ACTION = 'wpml_tm_word_count_ajax';
private $hooks = array();
private $requested_types_status;
private $translation_element_factory;
private $words_count_background_process_factory;
private $words_count_single_process_factory;
public function create() {
$this->requested_types_status = get_option( self::OPTION_KEY_REQUESTED_TYPES_STATUS, false );
$this->add_refresh_hooks();
$this->add_process_hooks();
$this->add_admin_hooks();
$this->add_ajax_hooks();
return $this->hooks;
}
private function add_refresh_hooks() {
if ( $this->is_heartbeat_autosave() ) {
return;
}
$this->hooks['refresh'] = new WPML_TM_Word_Count_Refresh_Hooks(
$this->get_words_count_single_process_factory(),
$this->get_translation_element_factory(),
class_exists( 'WPML_ST_Package_Factory' ) ? new WPML_ST_Package_Factory() : null
);
}
private function add_process_hooks() {
if ( $this->requested_types_status === self::PROCESS_IN_PROGRESS ) {
$this->hooks['process'] = new WPML_TM_Word_Count_Process_Hooks(
$this->get_words_count_background_process_factory()
);
}
}
private function add_admin_hooks() {
if ( is_admin() ) {
$this->hooks['admin'] = new WPML_TM_Word_Count_Admin_Hooks( new WPML_WP_API() );
}
}
private function add_ajax_hooks() {
if ( isset( $_POST['module'] ) && 'wpml_word_count' === $_POST['module'] && wpml_is_ajax() ) {
$report_view = new WPML_TM_Word_Count_Report_View(
new WPML_Twig_Template_Loader( array( WPML_TM_PATH . WPML_TM_Word_Count_Report_View::TEMPLATE_PATH ) ),
new WPML_WP_Cron_Check( new WPML_PHP_Functions() )
);
$records_factory = new WPML_TM_Word_Count_Records_Factory();
$report = new WPML_TM_Word_Count_Report(
$report_view,
$records_factory->create(),
$this->get_sitepress(),
$this->requested_types_status,
class_exists( 'WPML_Package_Helper' ) ? new WPML_Package_Helper() : null
);
$this->hooks['ajax'] = new WPML_TM_Word_Count_Ajax_Hooks(
$report,
$this->get_words_count_background_process_factory(),
$this->requested_types_status
);
}
}
/**
* @return WPML_TM_Word_Count_Single_Process_Factory
*/
private function get_words_count_single_process_factory() {
if ( ! $this->words_count_single_process_factory ) {
$this->words_count_single_process_factory = new WPML_TM_Word_Count_Single_Process_Factory();
}
return $this->words_count_single_process_factory;
}
private function get_translation_element_factory() {
if ( ! $this->translation_element_factory ) {
$this->translation_element_factory = new WPML_Translation_Element_Factory( $this->get_sitepress() );
}
return $this->translation_element_factory;
}
/**
* @return WPML_TM_Word_Count_Background_Process_Factory
*/
private function get_words_count_background_process_factory() {
if ( ! $this->words_count_background_process_factory ) {
$this->words_count_background_process_factory = new WPML_TM_Word_Count_Background_Process_Factory();
}
return $this->words_count_background_process_factory;
}
/**
* @return SitePress
*/
private function get_sitepress() {
global $sitepress;
return $sitepress;
}
private function is_heartbeat_autosave() {
return isset( $_POST['data']['wp_autosave'] );
}
}

View File

@@ -0,0 +1,21 @@
<?php
class WPML_TM_Word_Count_Process_Hooks implements IWPML_Action {
/** @var WPML_TM_Word_Count_Background_Process_Factory $process_factory */
private $process_factory;
/**
* @param WPML_TM_Word_Count_Background_Process_Factory $process_factory
*/
public function __construct( WPML_TM_Word_Count_Background_Process_Factory $process_factory ) {
$this->process_factory = $process_factory;
}
/**
* We need to include the hooks located in WP_Async_Request::__construct.
*/
public function add_hooks() {
$this->process_factory->create_requested_types();
}
}

View File

@@ -0,0 +1,92 @@
<?php
class WPML_TM_Word_Count_Refresh_Hooks implements IWPML_Action {
/** @var WPML_TM_Word_Count_Single_Process_Factory $single_process_factory */
private $single_process_factory;
/** @var WPML_Translation_Element_Factory $element_factory */
private $element_factory;
/** @var WPML_ST_Package_Factory|null $st_package_factory */
private $st_package_factory;
/** @var array $packages_to_refresh */
private $packages_to_refresh = array();
public function __construct(
WPML_TM_Word_Count_Single_Process_Factory $single_process_factory,
WPML_Translation_Element_Factory $element_factory,
WPML_ST_Package_Factory $st_package_factory = null
) {
$this->single_process_factory = $single_process_factory;
$this->element_factory = $element_factory;
$this->st_package_factory = $st_package_factory;
}
public function add_hooks() {
add_action( 'save_post', array( $this, 'refresh_post_word_count' ) );
if ( $this->st_package_factory ) {
add_action( 'wpml_register_string', array( $this, 'register_string_action' ), 10, 3 );
add_action( 'wpml_set_translated_strings', array( $this, 'set_translated_strings_action' ), 10, 2 );
add_action( 'wpml_delete_unused_package_strings', array( $this, 'delete_unused_package_strings_action' ) );
}
}
/** @param int $post_id */
public function refresh_post_word_count( $post_id ) {
$post_element = $this->element_factory->create( $post_id, 'post' );
if ( ! $post_element->is_translatable() ) {
return;
}
if ( $post_element->get_source_element() ) {
$post_id = $post_element->get_source_element()->get_id();
}
$this->single_process_factory->create()->process( 'post', $post_id );
}
/**
* @param string $string_value
* @param string $string_name
* @param array $package
*/
public function register_string_action( $string_value, $string_name, $package ) {
$this->defer_package_refresh( $package );
}
/** @param array $package */
public function delete_unused_package_strings_action( $package ) {
$this->defer_package_refresh( $package );
}
/**
* @param array $translations
* @param array $package
*/
public function set_translated_strings_action( $translations, $package ) {
$this->defer_package_refresh( $package );
}
/** @param array $package_array */
private function defer_package_refresh( $package_array ) {
$st_package = $this->st_package_factory->create( $package_array );
if ( $st_package->ID && ! $st_package->post_id && ! in_array( $st_package->ID, $this->packages_to_refresh, true ) ) {
$this->packages_to_refresh[] = $st_package->ID;
if ( ! has_action( 'shutdown', array( $this, 'refresh_packages_word_count' ) ) ) {
add_action( 'shutdown', array( $this, 'refresh_packages_word_count' ) );
}
}
}
public function refresh_packages_word_count() {
foreach ( $this->packages_to_refresh as $package_to_refresh ) {
$this->single_process_factory->create()->process( 'package', $package_to_refresh );
}
}
}