forked from tkelley/MangoBlog-metaTags
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cfc
More file actions
134 lines (108 loc) · 5.33 KB
/
plugin.cfc
File metadata and controls
134 lines (108 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<cfcomponent extends="BasePlugin">
<cfset this.events = [ { 'name' = 'beforeHtmlHeadEnd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforeAdminPostFormDisplay', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforeAdminPostFormEnd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforePostAdd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforePostUpdate', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'afterPostAdd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'afterPostUpdate', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforeAdminPageFormDisplay', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforeAdminPageFormEnd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforePageAdd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'beforePageUpdate', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'afterPageAdd', 'type' = 'sync', 'priority' = '5' },
{ 'name' = 'afterPageUpdate', 'type' = 'sync', 'priority' = '5' }
] />
<cffunction name="init" access="public" output="false" returntype="any">
<cfargument name="mainManager" type="any" required="true">
<cfargument name="preferences" type="any" required="true">
<cfscript>
setManager(arguments.mainManager);
setPreferencesManager(arguments.preferences);
setPackage("com.tysonkelley.plugins.metaTags");
variables.metaTags = structNew();
// Map our meta tags
variables.metaTags["metaTags_description"] = "";
variables.metaTags["metaTags_keywords"] = "";
return this;
</cfscript>
</cffunction>
<cffunction name="processEvent" hint="Synchronous event handling" access="public" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfscript>
var local = StructNew();
// Run event handler
switch(arguments.event.name){
case "beforeHtmlHeadEnd"://check that we aren;t in the admin
return displayMetaTags(arguments.event);
case "beforeAdminPostFormDisplay":
case "beforeAdminPageFormDisplay":
return removeMetaTagsFromCustomFields(arguments.event);
case "beforeAdminPostFormEnd":
case "beforeAdminPageFormEnd":
return displayMetaTagFields(arguments.event);
case "beforePostAdd":
case "beforePageAdd":
case "beforePageUpdate":
case "beforePostUpdate": return addMetaTagsToCustomFields(arguments.event);
}
return arguments.event;
</cfscript>
</cffunction>
<cffunction name="removeMetaTagsFromCustomFields" hint="Take our meta tags out of the custom fields (we'll use a custom display instead)" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfloop collection="#variables.metaTags#" item="local.key">
<cfif arguments.event.item.customFieldExists(local.key)>
<cfset variables.metaTags[local.key] = arguments.event.item.getCustomField(local.key).value>
<cfset arguments.event.item.removeCustomField(local.key)>
</cfif>
</cfloop>
<cfreturn arguments.event>
</cffunction>
<cffunction name="displayMetaTagFields" hint="Use a custom display for the meta tags" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfset local.metaKeywords = variables.metaTags.metaTags_keywords>
<cfset local.metaDescription = variables.metaTags.metaTags_description>
<cfsavecontent variable="contentForDisplay"><cfinclude template="form.cfm"></cfsavecontent>
<cfset arguments.event.setOutputData(arguments.event.getOutputData() & contentForDisplay)>
<cfset variables.metaTags.metaTags_keywords = '' />
<cfset variables.metaTags.metaTags_description = '' />
<cfreturn arguments.event>
</cffunction>
<cffunction name="displayMetaTags" hint="Output the actual meta tags" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfset local.metaTags = StructNew()>
<cfset var context = arguments.event.getContextData() />
<!--- check if it is an entry event.getContextData().currentEntry --->
<cfif structKeyExists( context, 'currentEntry' ) >
<cfset local.currentPost = context.currentEntry />
<cfloop collection="#variables.metaTags#" item="local.key">
<cfif local.currentPost.customFieldExists(local.key)>
<cfset local.metaTags[local.key] = local.currentPost.getCustomField(local.key).value>
</cfif>
</cfloop>
<cfsavecontent variable="local.contentForDisplay"><cfinclude template="display.cfm"></cfsavecontent>
<cfset arguments.event.setOutputData(arguments.event.getOutputData() & local.contentForDisplay)>
</cfif>
<cfreturn arguments.event>
</cffunction>
<cffunction name="addMetaTagsToCustomFields" hint="Add the meta tags back into custom fields" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfset var entry = {} />
<cfif structKeyExists( arguments.event.data, 'post' )>
<cfset entry = event.data.post />
<cfelse>
<cfset entry = event.data.page />
</cfif>
<cfloop collection="#variables.metaTags#" item="local.key">
<cfif structKeyExists(arguments.event.data.rawData, local.key)>
<cfset entry.setCustomField( local.key, local.key, arguments.event.data.rawdata[local.key] )>
</cfif>
</cfloop>
<cfreturn arguments.event>
</cffunction>
</cfcomponent>