Skip to content

Commit b6b1abd

Browse files
committed
Add class CI SEO Onpage
1 parent 26312cc commit b6b1abd

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

system/libraries/Seo_onpage.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Project codeigniter-framework
4+
* Created by PhpStorm
5+
* User: 713uk13m <dev@nguyenanhung.com>
6+
* Copyright: 713uk13m <dev@nguyenanhung.com>
7+
* Date: 10/03/2023
8+
* Time: 14:51
9+
*/
10+
defined('BASEPATH') or exit('No direct script access allowed');
11+
12+
/**
13+
* Class CI_Seo_onpage
14+
*
15+
* @author 713uk13m <dev@nguyenanhung.com>
16+
* @copyright 713uk13m <dev@nguyenanhung.com>
17+
*/
18+
class CI_Seo_onpage
19+
{
20+
protected $CI;
21+
22+
public function __construct()
23+
{
24+
$this->CI =& get_instance();
25+
$this->CI->load->helper('url');
26+
}
27+
28+
public function set_tags($title = "", $description = "", $image = "")
29+
{
30+
$current_url = current_url();
31+
echo "<meta property='og:url' content='$current_url' />\n";
32+
33+
$this->set_title_tags($title);
34+
$this->set_description_tags($description);
35+
$this->set_image_tags($image);
36+
$this->set_twitter_tags();
37+
$this->set_facebook_tags();
38+
$this->set_canonical();
39+
}
40+
41+
private function set_title_tags($title)
42+
{
43+
if ($title !== "") {
44+
echo "<title>$title</title>\n";
45+
echo "<meta property='og:title' content='$title' />\n";
46+
echo "<meta name='twitter:title' content='$title' />\n";
47+
} elseif ($this->CI->config->item('site_title') !== "") {
48+
echo "<title>" . $this->CI->config->item('site_title') . "</title>\n";
49+
echo "<meta property='og:title' content='" . $this->CI->config->item('site_title') . "' />\n";
50+
echo "<meta property='og:site_name' content='" . $this->CI->config->item('site_title') . "' />\n";
51+
echo "<meta name='twitter:title' content='" . $this->CI->config->item('site_title') . "' />\n";
52+
}
53+
54+
}
55+
56+
private function set_description_tags($description)
57+
{
58+
if ($description !== "") {
59+
echo "<meta name='description' content='$description'/>\n";
60+
echo "<meta property='og:description' content='$description' />\n";
61+
echo "<meta name='twitter:description' content='$description' />\n";
62+
} elseif ($this->CI->config->item('site_description') !== "") {
63+
echo "<meta name='description' content='" . $this->CI->config->item('site_description') . "'/>\n";
64+
echo "<meta property='og:description' content='" . $this->CI->config->item('site_description') . "' />\n";
65+
echo "<meta name='twitter:description' content='" . $this->CI->config->item('site_description') . "' />\n";
66+
}
67+
}
68+
69+
private function set_image_tags($image)
70+
{
71+
$image_path = null;
72+
73+
if ($image !== "") {
74+
$image_path = $this->format_image_tags($image);
75+
} elseif ($this->CI->config->item('site_image') !== "") {
76+
$image_path = $this->format_image_tags($this->CI->config->item('site_image'));
77+
}
78+
79+
if ($image_path) {
80+
list($width, $height) = getimagesize($image_path);
81+
echo "<meta property='og:image' content='$image_path' />\n";
82+
echo "<meta property='og:image:secure_url' content='$image_path' />\n";
83+
echo "<meta name='twitter:image' content='$image_path' />\n";
84+
echo "<meta name='twitter:card' content='summary_large_image' />\n";
85+
echo "<meta property='og:image:width' content='$width' />\n<meta property='og:image:height' content='$height' />\n";
86+
}
87+
}
88+
89+
private function set_twitter_tags()
90+
{
91+
if ($this->CI->config->item('twitter_user') !== "") {
92+
echo "<meta name='twitter:site' content='" . $this->CI->config->item('twitter_user') . "' />\n";
93+
}
94+
}
95+
96+
private function set_facebook_tags()
97+
{
98+
if ($this->CI->config->item('fb_page_id') !== "") {
99+
echo "<meta property='fb:pages' content='" . $this->CI->config->item('fb_page_id') . "' />\n";
100+
}
101+
102+
if ($this->CI->config->item('fb_app_id') !== "") {
103+
echo "<meta property='fb:app_id' content='" . $this->CI->config->item('fb_app_id') . "' />\n";
104+
}
105+
}
106+
107+
private function set_canonical()
108+
{
109+
if ($this->CI->config->item('canonical_url') !== "") {
110+
echo "<link rel='canonical' href='" . $this->CI->config->item('canonical_url') . "' />\n";
111+
}
112+
}
113+
114+
private function format_image_tags($image_path)
115+
{
116+
$path = parse_url($image_path);
117+
118+
if ($path['scheme'] === "http" || $path['scheme'] === "https") {
119+
return $image_path;
120+
}
121+
122+
return base_url($image_path);
123+
}
124+
}

0 commit comments

Comments
 (0)