capability,
'advanced-copy-settings',
[$this, 'settings_page_html']
);
}
public function register_settings() {
register_setting($this->option_group, 'advacoco_enabled', [
'default' => 1,
'sanitize_callback' => 'intval'
]);
register_setting($this->option_group, 'advacoco_snippet_length', [
'default' => 100,
'sanitize_callback' => [$this, 'sanitize_snippet_length']
]);
register_setting($this->option_group, 'advacoco_prefix_text', [
'default' => 'This excerpt was copied from',
'sanitize_callback' => 'sanitize_text_field'
]);
register_setting($this->option_group, 'advacoco_suffix_text', [
'default' => 'learn more at:',
'sanitize_callback' => 'sanitize_text_field'
]);
register_setting($this->option_group, 'advacoco_domain_text', [
'default' => '',
'sanitize_callback' => 'sanitize_text_field'
]);
register_setting($this->option_group, 'advacoco_exclude_pages', [
'default' => '',
'sanitize_callback' => 'sanitize_textarea_field'
]);
register_setting($this->option_group, 'advacoco_min_chars', [
'default' => 20,
'sanitize_callback' => [$this, 'sanitize_min_chars']
]);
register_setting($this->option_group, 'advacoco_ignore_code_blocks', [
'default' => 1,
'sanitize_callback' => 'intval'
]);
}
public function sanitize_snippet_length($value) {
$value = intval($value);
return max(10, min(500, $value));
}
public function sanitize_min_chars($value) {
$value = intval($value);
return max(1, min(100, $value));
}
public function settings_page_html() {
if (!current_user_can($this->capability)) {
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'advanced-content-copy'));
}
?>
should_exclude_page()) {
return;
}
// Register and enqueue the main script
wp_register_script(
'advacoco-frontend',
ADVACOCO_PLUGIN_URL . 'assets/js/advanced-content-copy.js',
[],
ADVACOCO_VERSION,
true
);
// Prepare settings for JavaScript
$settings = [
'snippetLength' => intval(get_option('advacoco_snippet_length', 100)),
'minChars' => intval(get_option('advacoco_min_chars', 20)),
'prefix' => get_option('advacoco_prefix_text', 'This excerpt was copied from'),
'suffix' => get_option('advacoco_suffix_text', 'learn more at:'),
'domain' => get_option('advacoco_domain_text', '') ?: get_bloginfo('name'),
'postUrl' => $this->get_appropriate_url(),
'ignoreCodeBlocks' => intval(get_option('advacoco_ignore_code_blocks', 1)),
'bypassSelectors' => $this->get_bypass_selectors()
];
// Localize the script with settings
wp_localize_script('advacoco-frontend', 'advacocoSettings', $settings);
// Enqueue the script
wp_enqueue_script('advacoco-frontend');
}
public function should_exclude_page() {
$exclude_pages = trim(get_option('advacoco_exclude_pages', ''));
if (empty($exclude_pages)) {
return false;
}
$current_id = get_the_ID();
$current_slug = get_post_field('post_name', $current_id);
$excluded = array_map('trim', explode(',', $exclude_pages));
return in_array($current_id, $excluded) || in_array($current_slug, $excluded);
}
/**
* Get the list of CSS selectors whose content should bypass copy
* modification entirely (e.g. code and script blocks).
*
* Only applied when the "Ignore Code Blocks" setting is enabled.
* Developers can add, remove, or override selectors with:
*
* add_filter('advacoco_bypass_selectors', function($selectors) {
* $selectors[] = '.my-custom-code-class';
* return $selectors;
* });
*
* @return array Array of CSS selector strings.
*/
public function get_bypass_selectors() {
$selectors = ['pre', 'code'];
/**
* Filters the CSS selectors exempted from copy modification.
*
* @param array $selectors Default: ['pre', 'code'].
*/
return apply_filters('advacoco_bypass_selectors', $selectors);
}
/**
* Get the appropriate URL based on the current page type
*
* @return string The URL to use for attribution
*/
public function get_appropriate_url() {
// Check if we're on a single post/page
if (is_singular()) {
return get_permalink();
}
// Check if we're on a category page
if (is_category()) {
$category = get_queried_object();
return get_category_link($category->term_id);
}
// Check if we're on a tag page
if (is_tag()) {
$tag = get_queried_object();
return get_tag_link($tag->term_id);
}
// Check if we're on a custom taxonomy archive
if (is_tax()) {
$term = get_queried_object();
return get_term_link($term);
}
// Check if we're on an author page
if (is_author()) {
$author_id = get_queried_object_id();
return get_author_posts_url($author_id);
}
// Check if we're on a date archive
if (is_date()) {
if (is_year()) {
return get_year_link(get_query_var('year'));
} elseif (is_month()) {
return get_month_link(get_query_var('year'), get_query_var('monthnum'));
} elseif (is_day()) {
return get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day'));
}
}
// Check if we're on the blog posts page
if (is_home() && !is_front_page()) {
return get_permalink(get_option('page_for_posts'));
}
// Check if we're on a search results page
if (is_search()) {
return home_url('/?s=' . urlencode(get_search_query()));
}
// Fallback to current page URL
return home_url(add_query_arg(null, null));
}
}
}
new ADVACOCO_Plugin();